Device tags
Device tags are custom identifiers that you add to devices in IoT Platform. You can use tags to manage products, devices, and groups.
Function introduction
A device tag is a key-value pair in the format Key:Value.
You can add tags to devices based on their attributes to simplify management. For example, you can tag a smart meter in room 201 with room:201.
SDK usage
Update tags
| API prototype | device#postTags(params, [callback]) |
| Function description | Report or update device tags. |
| Parameter description | - params: An array of property objects. Example format: [ {attrKey:'xxx',attrValue:'xxx'},{}...] - callback - res: The content of the reply message from the server-side. |
The following sample code adds a tag with the key "Temperature" and the value "36.8" to the cloud:
const tags = [
{
"attrKey": "Temperature",
"attrValue": "36.8"
}
]
device.postTags(
tags,
(res) => {
console.log(`Tag added successfully. Response ID: ${res.id}`);
done()
}
);
Note: A device can add multiple tags to the cloud.
Remove tags
| API prototype | device#deleteTags(tags) |
| Function description | Remove device tags. |
| Parameter description | - The tags parameter is an array of strings in the format `[ 'string','string',....]`, where each string is a tag name. |
Code example:
device.deleteTags(['tagA','tagB']);
The preceding sample code removes the tags with the keys "tagA" and "tagB".
For more information, see the complete code example.