Stand with Ukraine flag
Try it now Pricing
Edge
Documentation > Configuration Guides > Bidirectional Attribute Subscription and Update with Post-Processing
Getting Started
Devices Library Installation Architecture API FAQ
On this page

Bidirectional Attribute Subscription And Update with Post-Processing

Overview

In ThingsBoard, device attributes are key-value pairs associated with devices. They are used to store configuration settings, metadata, or other relevant information.

Subscribing to device attributes enables clients to receive updates when these attributes change. The subscription methods vary based on the attribute type and the communication protocol used.

Attributes are categorized into three types:

  • Server-side Attributes: Set and used exclusively by the server. Can be configured via User Interface (UI) or REST API.
  • Shared Attributes: Devices can subscribe to updates to receive real-time notifications when the server modifies them. Can be configured via UI or REST API.
  • Client-side Attributes: Set by the device and stored on the server. Can only be read via UI or REST API.

For more detailed information and examples, please refer to the ThingsBoard documentation on Working with IoT Device Attributes.

The following guide explains real-time, bidirectional communication between your device and the ThingsBoard Cloud via ThingsBoard Edge.

Prerequisites

  • The ThingsBoard Cloud or locally hosted Server.
  • The installed ThingsBoard Edge with network access.
  • A Device capable of connecting to ThingsBoard Edge via MQTT, HTTP, or CoAP.

Step 1. Configure a Rule Chain for the Edge

To automatically route messages (data or attributes) from the device to the ThingsBoard Cloud, configure the rule chain for the ThingsBoard Edge:

  • Log in to the ThingsBoard Cloud and go to the Edge management > Rule chain templates section and click on the Rule chain assigned to your Edge instance.
  • On the Rule Chain edit page, in the Node search bar find the "push to cloud" node. It pushes messages from Edge to Cloud. Drag and drop the node onto the Rule Chain sheet. Then, in the "Add rule node" pop-up window enter the node title and select the "Client attributes" option in the "Entity attributes scope" field. Click the "Add" button to proceed.
  • Connect the “save attributes” and “save time series” nodes to the "push to cloud" node and set the "Success" link label. Click the “Apply changes” button on the Rule Chain sheet.

Step 2. Configure a Rule Chain for the Cloud

To send back some attributes update message to the Edge, modify root rule chain on the cloud. Once the attribute update message will arrive to the edge you should see this message on the device.

  • Go to the Rule Chains section, to modify the Rule Chain.
  • On the Rule Chain edit page, in the Node search bar find the "script" node. It allows modification of message payload, metadata or message type with JavaScript. Drag and drop the node onto the Rule Chain sheet. Then, in the "Add rule node: script" pop-up window enter the node title and enter the script below. Click the "Add" button to proceed.
  • On the Rule Chain edit page, in the Node search bar find the "push to edge" node. It pushes messages from Cloud to Edge. Drag and drop the node onto the Rule Chain sheet. Then, in the "Add rule node" pop-up window enter the node title and select the "Shared attributes" option in the "Entity attributes scope" field. Click the "Add" button to proceed.
  • Connect the “save attributes” and “save time series” nodes to the "push to edge" node and set the "Success" link label. Click the “Apply changes” button on the Rule Chain sheet.

To rewrite and return received data back to Edge, add the following script into the “script” node:

1
2
3
4
5
// POST PROCESSING LOGIC
msg = {"attribute1_result": "value1_result"};
metadata = {};

return {msg: msg, metadata: metadata, msgType: msgType};

Step 3. Subscribe to Device Attributes Change And Publish Device Attributes Message

The procedure for subscribing to the Device Attribute Changes and subsequently publishing the Device Attributes Message depends on the protocol used. There are the most commonly used protocols for basic telemetry and attribute updates in IoT applications:

MQTT is a lightweight protocol commonly used for IoT communication.

To facilitate communication with ThingsBoard Edge using MQTT, we recommend installing the MQTT Broker. This allows the device to publish telemetry or attribute messages and subscribe to topics for attribute updates.

Subscribe to the Changes in Shared Device Attributes

To subscribe to shared device attribute changes, send the SUBSCRIBE message:

1
mosquitto_sub -d -h $THINGSBOARD_HOST_NAME -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN"
  • v1/devices/me/attributes: This is a topic on ThingsBoard Edge. It allows the device to listen for any updates related to its attributes from the cloud.
  • Replace the $THINGSBOARD_HOST_NAME with the actual hostname or IP address of your ThingsBoard Edge instance.
  • Replace the $ACCESS_TOKEN with the actual access token of the device. To find the device access token, go to the Entities > Devices section and click on the device. On the “Device details” page, you can copy the token by clicking the “Copy access token” button.

Publish Time-Series or Attribute Message.

To publish client-side device attributes to the ThingsBoard Edge, send a PUBLISH message.

You can publish the telemetry data:

1
mosquitto_pub -d -h $THINGSBOARD_HOST_NAME -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN" -m "{"attribute1": "value1", "attribute2": true}"
  • v1/devices/me/attributes: The topic to which the telemetry data is published.
  • $THINGSBOARD_HOST_NAME: The actual hostname or IP address of your ThingsBoard Edge instance
  • $ACCESS_TOKEN: The actual access token of the device.
  • {“attribute1”: “value1”, “attribute2”: true} The telemetry data.

You can also publish client-side attributes update using data from new-attributes-values.json file:

1
mosquitto_pub -d -h $THINGSBOARD_HOST_NAME -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN" -f "new-attributes-values.json"

HTTP is a general-purpose network protocol that can be used in IoT applications. HTTP protocol is TCP based and uses request-response model.

Subscribe to the Changes in Shared Device Attributes

To subscribe to changes in shared device attributes, send a GET request to the following URL, with an optional ‘timeout’ parameter:

1
http(s)://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes/updates
  • /attributes/updates The endpoint that is used to fetch the updates of device attributes.
  • Replace the $THINGSBOARD_HOST_NAME with the actual hostname or IP address of your ThingsBoard Edge instance.
  • Replace the $ACCESS_TOKEN with the actual access token of the device. To find the device access token, go to the Entities > Devices section and click on the device. On the “Device details” page, you can copy the token by clicking the “Copy access token” button.

Execute the command:

1
curl -v -X GET http(s)://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes/updates?timeout=20000
  • timeout=20000 The server will keep the connection open for 20 seconds. If a shared attribute for the device is updated within this timeout period, the server will respond immediately with the update.

Publish Time-Series or Attribute Message.

To publish client-side device attributes to the ThingsBoard Edge, send a POST request to the following URL:

1
http(s)://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes
  • $THINGSBOARD_HOST_NAME: The actual hostname or IP address of your ThingsBoard Edge instance
  • $ACCESS_TOKEN: The actual access token of the device.

Publish client-side attributes update

1
curl -v -X POST --data "{"attribute1": "value1", "attribute2":true, "attribute3": 43.0}" https:/$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes --header "Content-Type:application/json"

You can also publish client-side attributes update using data from new-attributes-values.json file:

1
curl -v -X POST -d @new-attributes-values.json https://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes --header "Content-Type:application/json"

CoAP is a light-weight IoT protocol for constrained devices. CoAP protocol is UDP based, but similar to HTTP it uses request-response model.

Subscribe to the Changes in Shared Device Attributes

To subscribe to changes in shared device attributes, send a GET request to the following URL:

1
coap://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes
  • Replace the $THINGSBOARD_HOST_NAME with the actual hostname or IP address of your ThingsBoard Edge instance.
  • Replace the $ACCESS_TOKEN with the actual access token of the device. To find the device access token, go to the Entities > Devices section and click on the device. On the “Device details” page, you can copy the token by clicking the “Copy access token” button.

Execute the command:

1
coap get -o coap://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes

Once shared attribute will be changed by one of the server-side components (REST API or Rule Chain) the client will receive the update.

Publish Time-Series or Attribute Message.

To publish client-side device attributes to the ThingsBoard Edge, send a POST request to the following URL:

1
coap://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes
  • $THINGSBOARD_HOST_NAME: The actual hostname or IP address of your ThingsBoard Edge instance
  • $ACCESS_TOKEN: The actual access token of the device.

Publish client-side attributes update using data from new-attributes-values.json file:

1
cat new-attributes-values.json | coap post coap://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes

Step 4. Confirm the Results

Confirm the received and pushed attributes:

  • Once you have subscribed to attribute updates, you will receive updates as the attributes change. We have used the MQTT protocol as an example. The updates were received after new attributes were published.
  • To confirm if the client-side attributes are received, go to the Entities > Devices section of your Edge and click on the device. On the "Device details" page, select the "Attributes" tab and select the "Client attributes" option in the "Entity attributes scope" drop-down menu.
  • To confirm if the attributes message is received and pushed to Cloud, go to the Rule chains section of your Edge instance and click on the Rule chain. Click on the "push to cloud" node.
  • On the "Push to cloud" page, select the "Events" tab. If you have debug mode enabled, select the "Debug" option in the "Event type" drop-down menu. You should see the new records - "IN" and "OUT", where the "IN" record defines the attributes received from the device, and the "OUT" record defines the attributes message pushed to the Cloud.
  • To confirm if the attributes message is received on the Cloud and pushed back to the Edge, go to the Rule chains section of your Cloud and click on the Rule chain. Click twice on the "push to edge" node.
  • On the "Push to edge" page, select the "Events" tab. If you have debug mode enabled, select the "Debug" option from the "Event type" drop-down menu. You should see two new records - "IN" and "OUT", where the "IN" record defines the attributes received from Edge, and the "OUT" record defines the attributes message pushed back to Edge.

Next Steps