Device tag

Updated at:

A device tag in IoT Platform is a custom identifier for a device. You can use tags to flexibly manage your products, devices, and groups.

Prerequisites

You can use the device tag APIs only after the SDK initializes the Thing Model and triggers the on_thing_enable callback.

Overview

A device tag is a key-value pair in the format Key:Value.

You can add specific tags to a device based on its characteristics to simplify management. For example, you can assign the tag room:201 to a smart meter in room 201. You can manage device tags in the console or by using the Python SDK.

Use the SDK

  • Version requirements

    Requires Aliyun IoT Python SDK 1.0.1 or later.

  • Update tags

    Use the thing_update_tags API to add or update tags. The following code shows an example:

    tags = {
        "floor": "2f",
        "room": "201"
    }
    rc, request_id = linkkit.thing_update_tags(tags)
    if rc == 0:
       printf("success")
         

    This API asynchronously sends a request to IoT Platform to update tags and returns rc and request_id. An rc value of 0 indicates that the request was sent successfully. Use the returned request_id to retrieve the final result from the asynchronous callback.

    The result is returned asynchronously. To retrieve the result, set the on_thing_device_info_update callback. The following code shows an example:

    linkkit.on_thing_device_info_update = on_thing_device_info_update
    ...
    def on_thing_device_info_update(self, request_id, code, data, message, userdata):
            print("on_thing_device_info_update: request_id:%s, code:%s, data:%s, message:%s" % (request_id, code, data, message))
          

    After the tags are updated, you can view them in the console. On the Device Details page, in the Tag Information section, the added key-value pairs are displayed under Device Tags.

  • Delete tags

    Use the thing_remove_tags API to delete tags. The following code shows an example:

    tags = ["floor", "room"]
    rc, request_id = linkkit.thing_remove_tags(tags)
    if rc == 0:
       printf("success")

    This API asynchronously sends a request to IoT Platform to delete the specified tags and returns rc and request_id. An rc value of 0 indicates that the request was sent successfully. Use the returned request_id to retrieve the final result from the asynchronous callback.

    The result is returned asynchronously. To retrieve the result, set the on_thing_device_info_delete callback. The following code shows an example:

    linkkit.on_thing_device_info_delete = on_thing_device_info_delete
    ...
    def on_thing_device_info_delete(self, request_id, code, data, message, userdata):
            print("on_thing_device_info_delete: request_id:%s, code:%s, data:%s, message:%s" % (request_id, code, data, message))