Manage Alarms and RPC Requests on Edge Devices
A warehouse has two devices connected to ThingsBoard Edge: a DHT22 temperature sensor and an Air Conditioner. When the DHT22 reports a temperature above 50 °C, Edge raises an alarm and immediately sends an RPC command to turn on the Air Conditioner. The alarm rule and the rule chain run on the Edge, so this works without a server connection.
Prerequisites
Section titled “Prerequisites”- Familiar with Alarm rules and Device profiles.
- Familiar with Edge rule chain templates.
- ThingsBoard server 4.4 and ThingsBoard Edge 4.3.0 or later. The Edge must be connected to the server while you configure it, so that the profile, the alarm rule, and the rule chain are synchronized to the Edge. After that, the flow also works offline.
- Node.js installed on the machine that will simulate the Air Conditioner.
Create the device profile and alarm rule
Section titled “Create the device profile and alarm rule”Create a device profile named edge thermostat on the server, then an alarm rule for this profile that fires when temperature > 50.
- Log in to the ThingsBoard server. Go to Devices & assets > Device profiles and click +.
- Enter the name edge thermostat. Keep the defaults on the Transport configuration and Device provisioning steps, and click Add.
- Go to Monitor > Alarms and open the Alarm rules tab. Click + and select Create new alarm rule.
- In the General section, set Alarm type to High temperature, Entity type to Device profile, and select edge thermostat.
- In Arguments, click Add argument. Set Entity type to Current entity, Argument type to Latest telemetry, Time series key to
temperature, and Argument name totemperature. - In Trigger conditions, click Add trigger condition and select the Critical severity. Click Add condition, then Add argument filter, and set Argument to
temperature, Value type to Numeric, Operation to greater than, Value source to Static, and Value to50. Click Add, then Save. - Click Add.
The device profile and the alarm rule are sent to every Edge. To check this, log in to the Edge and open Devices & assets > Device profiles: the edge thermostat profile is in the list. Then open Monitor > Alarms, go to the Alarm rules tab, and check that High temperature is in the list.
Provision devices
Section titled “Provision devices”Create the DHT22 and Air Conditioner devices on the Edge and add a Manages relation from DHT22 to Air Conditioner. The rule chain uses this relation to find the Air Conditioner when DHT22 raises the alarm.
- Log in to the Edge and go to Devices & assets > Devices. Click + and select Add new device.
- Enter the name DHT22, select edge thermostat as the device profile, and click Add.
- Click + and select Add new device again. Enter the name Air Conditioner (the default profile is fine) and click Add.
- Open the DHT22 device, go to the Relations tab, and click +.
- Set Relation type to Manages. Under To entity, set Type to Device, select Air Conditioner, and click Add.
Both devices and the relation are sent to the server. To check this, log in to the server and go to Devices & assets > Devices. Open DHT22 and go to the Relations tab: it shows the Manages relation to Air Conditioner.
Configure the Edge rule chain
Section titled “Configure the Edge rule chain”When the alarm rule creates the alarm on Edge, it sends an Alarm Created message to the rule chain of the device: the Edge Root Rule Chain, because the profile has no default rule chain. Extend this chain so that the message becomes an RPC call to the Air Conditioner:
Message Type Switch ─ Alarm Created ─> Enable Air Conditioner (script) ─ Success ─> Switch to Air Conditioner (change originator) ─ Success ─> RPC Call RequestEdit the Edge Root Rule Chain template on the server; the change is sent to the Edge. On Edge 4.0 and later, you can make the same change directly on the Edge under Data processing > Rule chains.
- Log in to the ThingsBoard server. Go to Edge management > Templates, open the Rule chain tab, and open Edge Root Rule Chain.
- In the node search field, enter script and drag the script node from the Transformation section onto the canvas. Enter the name Enable Air Conditioner and replace the script with the code below. Click Add.
var newMsg = {};newMsg.method = "enabled_air_conditioner";newMsg.params = {"speed": 1.0};return { msg: newMsg, metadata: metadata, msgType: msgType };
- Drag a connection from the Message Type Switch node to Enable Air Conditioner. In the link dialog, select Alarm Created and click Add.
- In the node search field, enter change and drag the change originator node from the Transformation section onto the canvas. Configure it as in the table below: the node opens with one filter row of relation type Contains, so change that row instead of adding a new one. Click Add.
- Drag a connection from Enable Air Conditioner to Switch to Air Conditioner and select Success. Then drag a connection from Switch to Air Conditioner to the existing RPC Call Request node and select Success.
- Click Apply changes.
Settings of the change originator node:
| Setting | Value |
|---|---|
| Name | Switch to Air Conditioner |
| New originator | Related entity |
| Direction | From originator |
| Max relation level | 1 |
| Relation type | Manages |
| Entity types | Device |
To check that the change reached the Edge, log in to the Edge, go to Data processing > Rule chains, and open Edge Root Rule Chain. It contains the two new nodes and their connections.
Subscribe Air Conditioner to RPC commands
Section titled “Subscribe Air Conditioner to RPC commands”Get the Air Conditioner access token from the Edge, then run the subscriber script.
- On the Edge, go to Devices & assets > Devices.
- Click Air Conditioner to open the device details.
- Click Copy access token and save it.
Download the two scripts below, update the placeholders in mqtt-js.sh, then run them.
# Set ThingsBoard Edge hostexport TB_EDGE_HOST=YOUR_TB_EDGE_HOST # e.g. localhost# Set ThingsBoard Edge MQTT portexport TB_EDGE_MQTT_PORT=YOUR_TB_EDGE_MQTT_PORT # e.g. 11883 or 1883# Set Air Conditioner device tokenexport ACCESS_TOKEN=YOUR_ACCESS_TOKEN# Run cooler.js scriptnode cooler.jsvar mqtt = require('mqtt');
console.log('Connecting to: %s:%s using access token: %s', process.env.TB_EDGE_HOST, process.env.TB_EDGE_MQTT_PORT, process.env.ACCESS_TOKEN);
var client = mqtt.connect( 'mqtt://' + process.env.TB_EDGE_HOST + ':' + process.env.TB_EDGE_MQTT_PORT, { username: process.env.ACCESS_TOKEN });
client.on('connect', function () { client.subscribe('v1/devices/me/rpc/request/+'); console.log('Cooler is connected!');});
client.on('message', function (topic, message) { var tmp = JSON.parse(message.toString()); console.log('Received RPC command from edge!'); console.log('Method:', tmp.method); console.log('Speed params:', tmp.params.speed);});
process.on('SIGINT', function () { console.log('Disconnecting...'); client.end(); process.exit(2);});Install the mqtt package and start the subscriber:
npm install mqtt --savechmod +x mqtt-js.shbash mqtt-js.shYou should see:
Connecting to: localhost:1883 using access token: <your_token>Cooler is connected!Post telemetry to trigger the alarm
Section titled “Post telemetry to trigger the alarm”Get the DHT22 access token from the Edge.
- On the Edge, go to Devices & assets > Devices.
- Click DHT22 to open the device details.
- Click Copy access token and save it.
Publish a temperature reading above the threshold to trigger the High temperature alarm. Replace $ACCESS_TOKEN with the DHT22 token and $EDGE_HOST with your Edge host.
curl -v -X POST \ -d '{"temperature": 51}' \ http://$EDGE_HOST:8080/api/v1/$ACCESS_TOKEN/telemetry \ --header "Content-Type:application/json"mosquitto_pub -d -q 1 \ -h "$EDGE_HOST" -p 1883 \ -t "v1/devices/me/telemetry" \ -u "$ACCESS_TOKEN" \ -m '{"temperature": 51}'echo -n '{"temperature": 51}' | coap post coap://$EDGE_HOST:5683/api/v1/$ACCESS_TOKEN/telemetryOn the Edge, open the DHT22 device and select the Alarms tab. The High temperature alarm is listed with the severity Critical and the status Active Unacknowledged. The alarm is not sent to the server: the Edge Root Rule Chain does not route alarm messages to the Push to cloud node. To change that, see Push alarms from Edge to the server.
Verify the RPC command
Section titled “Verify the RPC command”Switch back to the terminal running mqtt-js.sh. You should see the RPC command received by the Air Conditioner:
Received RPC command from edge!Method: enabled_air_conditionerSpeed params: 1The Edge evaluated the alarm rule, built the RPC command in the rule chain, switched the originator to the Air Conditioner through the Manages relation, and delivered the command, all without a server connection.
Was this helpful?