Updated on 2024-12-02 GMT+08:00

Configure Desired Properties in a Device Shadow

Function

This API is used by an application to configure properties in the desired section of a device shadow. When the device goes online or reports properties, the desired properties are delivered to the device.

Introduction:

A device shadow is a JSON file used to store and obtain device status.

  • Each device has only one device shadow, which is uniquely identified by the device ID.

  • A device shadow stores the properties (status) reported by a device and the properties (status) expected by an application.

  • You can obtain and set the device properties through the device shadow, regardless of whether the device is online.

  • After a device goes online or reports data, if the reported properties are different from the desired ones, the desired properties are delivered to the device. The desired properties can be delivered only when they have been defined in the product model and method is specified as W.

  • This API can be used to configure desired shadow data of a single device. For details about how to configure desired shadow data of multiple devices, see Creating a Batch Task.

Restrictions:

Keys in the device shadow JSON file cannot contain periods (.), dollar signs ($), and the null character (hexadecimal ASCII code 00). Otherwise, the device shadow file cannot be updated.

Debugging

You can debug this API through automatic authentication in API Explorer or use the SDK sample code generated by API Explorer.

URI

PUT /v5/iot/{project_id}/devices/{device_id}/shadow

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Parameter description: project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

device_id

Yes

String

Parameter description: device ID, which uniquely identifies a device. The value of this parameter is specified during device registration or allocated by the platform. If the value is allocated by the platform, the value is in the format of [product_id][node_id]. Value: The value can contain a maximum of 128 characters. Only letters, digits, underscores (), and hyphens (-) are allowed.

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

No

String

Parameter description: user token. You can obtain the token by calling the IAM API Obtaining a User Token Through Password Authentication. X-Subject-Token in the response header returned by the API is the desired user token. For details about how to obtain the token, see Token Authentication.

Instance-Id

No

String

Parameter description: instance ID. Unique identifier of each instance in the physical multi-tenant scenario. Mandatory for professional editions and recommended in other cases. Log in to the IoTDA console and choose Overview in the navigation pane to view the instance ID. For details, see Viewing Instance Details.

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

shadow

No

Array of UpdateDesired objects

Parameter description: data structure of the desired properties in the device shadow.

Table 4 UpdateDesired

Parameter

Mandatory

Type

Description

service_id

Yes

String

Parameter description: device service ID, which is defined in the product model associated with the device.

desired

Yes

Object

Parameter description: desired properties in the device shadow. The data is JSON data and is in the format of key-value pairs. Each key is a parameter name of a property (property_name) in the product model. Only one-layer structure is supported, as shown in the example request. To delete all desired properties, leave the value empty (for example, "desired":{}). To delete a desired property, set the value of the property to null (for example, {"temperature":null}).

version

No

Long

Parameter description: version of the device shadow. When this parameter is carried, the platform will check whether the value is the same as the current shadow version. The initial value is 0.

Response Parameters

Status code: 200

Table 5 Response body parameters

Parameter

Type

Description

device_id

String

Device ID, used to uniquely identify a device. The value of this parameter is specified during device registration or allocated by the platform. If the value is allocated by the platform, the value is in the format of [product_id]_[node_id].

shadow

Array of DeviceShadowData objects

Device shadow data structure.

Table 6 DeviceShadowData

Parameter

Type

Description

service_id

String

Device service ID, which is defined in the product model associated with the device.

desired

DeviceShadowProperties object

Latest desired properties delivered to the device. The data is JSON data and is in the format of key-value pairs. Each key is a parameter name of a property (property_name) in the product model.

reported

DeviceShadowProperties object

Latest properties reported by the device. The data is JSON data and is in the format of key-value pairs. Each key is a parameter name of a property (property_name) in the product model.

version

Long

Version of the device shadow. When this parameter is carried, the platform will check whether the value is the same as the current shadow version. The initial value is 0.

Table 7 DeviceShadowProperties

Parameter

Type

Description

properties

Object

Properties stored in the device shadow. The data is JSON data and is in the format of key-value pairs. Each key is a parameter name of a property (property_name) in the product model. Only one-layer structure is supported, as shown in the example.

Note: Keys in the JSON file cannot contain periods (.), dollar signs ($), and the null character (hexadecimal ASCII code 00). Keys that contain these special characters will cause device shadow update failures.

event_time

String

Time when the event occurred. The value is in the format of yyyyMMdd'T'HHmmss'Z', for example, 20151212T121212Z.

Example Requests

  • Configures the expected data of the device shadow. The expected value of temperature is 60.

    PUT https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/shadow
    
    {
      "shadow" : [ {
        "service_id" : "WaterMeter",
        "desired" : {
          "temperature" : "60"
        },
        "version" : 1
      } ]
    }
  • Delete the expected value of the temperature property from the device shadow.

    PUT https://{endpoint}/v5/iot/{project_id}/devices/{device_id}/shadow
    
    {
      "shadow" : [ {
        "service_id" : "WaterMeter",
        "desired" : {
          "temperature" : null
        },
        "version" : 2
      } ]
    }

Example Responses

Status code: 200

OK

{
  "device_id" : "40fe3542-f4cc-4b6a-98c3-61a49ba1acd4",
  "shadow" : [ {
    "service_id" : "WaterMeter",
    "desired" : {
      "properties" : {
        "temperature" : "60"
      },
      "event_time" : "20151212T121212Z"
    },
    "reported" : {
      "properties" : {
        "temperature" : "60"
      },
      "event_time" : "20151212T121212Z"
    },
    "version" : 2
  } ]
}

Status Codes

Status Code

Description

200

OK

400

Bad Request

401

Unauthorized

403

FORBIDDEN

404

Not Found

409

CONFLICT

500

INTERNAL SERVER ERROR

Error Codes

See Error Codes.