# Attributes

## UpsertAttributes

Lưu attributes với **key**, **value** vào thiết bị với **id** tương ứng

<pre class="language-typescript"><code class="lang-typescript"><strong>Thing(id: string).UpsertAttributes(attrs: {[key: string]: any}, 
</strong><strong>{logged: boolean, ts: number})
</strong></code></pre>

{% hint style="info" %}

* **logged:** Nếu **true** thì sẽ lưu lịch sử thuộc tính
* **ts**: Xác định dấu thời gian (timestamp) của thuộc tính (unix milliseconds)
  {% endhint %}

<details>

<summary>Ví dụ</summary>

```typescript
var attrs = {};
let logged = false
attrs['Led'] = 'ON;
await Thing('036116ed-38fc-458b-b91f-b3f774706247').UpsertAttributes(attrs, {logged: logged, ts: 1716296846000});

```

</details>

## GetAttributesLatest

Lấy giá trị của các attributes với **keys** của thiết bị với **id** tương ứng, trả về [entitiesList](#entitieslist)

```typescript
GetAttributesLatest(req: {keys: string[], ids: string[], entityType: string})
```

<details>

<summary>Ví dụ</summary>

```typescript
let arrayKeys = ["led", "data"];
let arrayIDs = ["bba8dd8e-4932-11ee-be56-0242ac120002", "c6134e44-4932-11ee-be56-0242ac120002"]
let res = await Things().GetAttributesLatest({ ids: arrayIDs, entityType: "DEVICE", keys: arrayKeys });
if (res.success){
  // Lấy dữ liệu thành công
}else{
  // Lấy dữ liệu thất bại hoặc không tồn tại
}
```

</details>

## Type

### entitiesList

<pre class="language-typescript"><code class="lang-typescript"><strong>{
</strong><strong>    entitiesList: entity[];
</strong><strong>}
</strong></code></pre>

### entity

```typescript
{
    attributesList: attribute[];
    entityId: string;
    entityType: string;
}
```

### attribute

```typescript
{
    key: string;
    logged: boolean;
    ts: number;
    value: string | Uint8Array;
    valueAsString: string;
    valueType: string
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://innoway.gitbook.io/innoway/tai-lieu/sdk/attributes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
