Attribute updates with post-processing
When a device publishes client attributes to Edge, Edge forwards them to the server. The server runs a transformation script and pushes the result back to Edge as shared attributes. The device receives the update by subscribing to the shared attribute topic on Edge.
For an introduction to attribute types, see Attributes.
Prerequisites
Section titled “Prerequisites”- A ThingsBoard server (ThingsBoard Cloud or self-hosted) is running and reachable from Edge.
- ThingsBoard Edge is installed and connected to the server.
- A device is connected to Edge via MQTT, HTTP, or CoAP.
Step 1: Route device attributes to the server
Section titled “Step 1: Route device attributes to the server”Configure the Edge rule chain to forward client attributes from the device to the server with the push to cloud node.
-
On the server, go to Edge management > Templates, open the Rule chain tab, and open the rule chain template assigned to your Edge. On Edge 4.0 or later, you can instead go to Data processing > Rule chains on the Edge.
-
Find the push to cloud node in the Search nodes field and drag it onto the rule chain sheet. In the Add rule node dialog, enter a name, select Client attributes in the Attributes scope field, and click Add.
-
Connect the save attributes and save time series nodes to the push to cloud node with the Success label. Click Apply changes.
In the default Edge Root Rule Chain template, a Push to cloud node is already connected to these two nodes with the Success label. In that case, do not add a second node: open the existing one, set Attributes scope to Client attributes, and click Apply changes.
Step 2: Process and return attributes to Edge
Section titled “Step 2: Process and return attributes to Edge”Configure the root rule chain of the server to transform the incoming attributes and push the result back to Edge as shared attributes. The script node sits between the save nodes and the push to edge node:
Use the following post-processing script in the script node. It works with both TBEL and JavaScript. Change the transformation logic as needed:
// Post-processing script, modify as neededmsg = {"attribute1_result": "value1_result"};metadata = {};
return {msg: msg, metadata: metadata, msgType: msgType};-
On the server, go to Data processing > Rule chains and open the root rule chain.
-
Find the script node in the Search nodes field (Transformation section) and drag it onto the sheet. Enter a name, paste the script above, and click Add.
-
Find the push to edge node and drag it onto the sheet. Enter a name, select Shared attributes in the Attributes scope field, and click Add.
-
Connect the save attributes and save time series nodes to the script node with the Success label. Connect the script node to the push to edge node with the Success label. Click Apply changes.
Step 3: Subscribe, then publish device attributes
Section titled “Step 3: Subscribe, then publish device attributes”Subscribe to shared attribute updates before publishing. This way the device is listening when the server pushes the processed result back.
To get the device access token, go to Devices & assets > Devices on the Edge, open the device, and click Copy access token on the Details tab.
Use the protocol your device supports:
Subscribe to shared attribute updates:
mosquitto_sub -d -h $THINGSBOARD_HOST_NAME -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN"$THINGSBOARD_HOST_NAMEis the hostname or IP address of your Edge.$ACCESS_TOKENis the access token copied above.
Publish client attributes:
mosquitto_pub -d -h $THINGSBOARD_HOST_NAME -t "v1/devices/me/attributes" -u "$ACCESS_TOKEN" -m '{"attribute1": "value1", "attribute2": true}'Subscribe to shared attribute updates:
curl -v -X GET "http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes/updates?timeout=20000"$THINGSBOARD_HOST_NAMEis the hostname or IP address of your Edge.$ACCESS_TOKENis the access token copied above.timeout=20000keeps the connection open for 20 seconds. If a shared attribute is updated in that time, the server responds immediately.
Publish client attributes:
curl -v -X POST \ --data '{"attribute1": "value1", "attribute2": true}' \ "http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes" \ --header "Content-Type: application/json"Subscribe to shared attribute updates:
coap get -o coap://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes$THINGSBOARD_HOST_NAMEis the hostname or IP address of your Edge.$ACCESS_TOKENis the access token copied above.
The client receives an update when a shared attribute changes on Edge.
Publish client attributes:
coap post coap://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/attributes --payload '{"attribute1": "value1", "attribute2": true}'Step 4: Verify the flow
Section titled “Step 4: Verify the flow”After the device publishes, confirm that the attributes arrive at each point.
- On the device. The subscribed client receives the processed shared attributes, for example
{"attribute1_result":"value1_result"}. - On Edge. Go to Devices & assets > Devices, open the device, select the Attributes tab, and choose Client attributes in the scope dropdown. The published attributes,
attribute1andattribute2, appear in the list. - In the Edge rule chain. Open the push to cloud node and select the Events tab (Event type is Debug). With debug mode enabled, you see an IN record (attributes received from the device) and an OUT record (attributes pushed to the server), both with the message type
POST_ATTRIBUTES_REQUEST. - In the server rule chain. Open the push to edge node and select the Events tab. You see an IN record (attributes received from Edge) and an OUT record (processed attributes pushed back to Edge).
Was this helpful?