Skip to content

Cookie preferences

We use cookies for our own analytics, to see how our campaigns perform and, if you allow it, to load content from other services such as the Google site search. We never sell your data. Necessary cookies keep the site working and cannot be switched off. See our Cookie Policy for details and our Privacy Policy for how we handle personal data.

Security, load balancing and remembering your cookie choice.

Show us which pages people read, how they find the site and where they get stuck, so we can improve it (Google Analytics, Microsoft Clarity).

Show us which of our ad campaigns bring visitors to the site and remember the campaign or partner link you arrived from (Google Ads, LinkedIn, Meta, partner program). We do not use these cookies to build advertising profiles.

Load the site search from Google when you use it. Google sets its own cookies and shows ads in the search results.

© 2026 The ThingsBoard Authors
180 Try for free

ThingsBoard Cloud

Choose your data region

Your data stays in the region you choose, for residency and compliance. No credit card required.

Rather run it yourself? Install on your own servers

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.

  • 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 a device profile named edge thermostat on the server, then an alarm rule for this profile that fires when temperature > 50.

  1. Log in to the ThingsBoard server. Go to Devices & assets > Device profiles and click +.
  2. Enter the name edge thermostat. Keep the defaults on the Transport configuration and Device provisioning steps, and click Add.
  3. Go to Monitor > Alarms and open the Alarm rules tab. Click + and select Create new alarm rule.
  4. In the General section, set Alarm type to High temperature, Entity type to Device profile, and select edge thermostat.
  5. In Arguments, click Add argument. Set Entity type to Current entity, Argument type to Latest telemetry, Time series key to temperature, and Argument name to temperature.
  6. 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 to 50. Click Add, then Save.
  7. 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.

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.

  1. Log in to the Edge and go to Devices & assets > Devices. Click + and select Add new device.
  2. Enter the name DHT22, select edge thermostat as the device profile, and click Add.
  3. Click + and select Add new device again. Enter the name Air Conditioner (the default profile is fine) and click Add.
  4. Open the DHT22 device, go to the Relations tab, and click +.
  5. 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.

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 Request

Edit 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.

  1. Log in to the ThingsBoard server. Go to Edge management > Templates, open the Rule chain tab, and open Edge Root Rule Chain.
  2. 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 };
  3. Drag a connection from the Message Type Switch node to Enable Air Conditioner. In the link dialog, select Alarm Created and click Add.
  4. 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.
  5. 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.
  6. 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.

Get the Air Conditioner access token from the Edge, then run the subscriber script.

  1. On the Edge, go to Devices & assets > Devices.
  2. Click Air Conditioner to open the device details.
  3. Click Copy access token and save it.

Download the two scripts below, update the placeholders in mqtt-js.sh, then run them.

Terminal window
# Set ThingsBoard Edge host
export TB_EDGE_HOST=YOUR_TB_EDGE_HOST # e.g. localhost
# Set ThingsBoard Edge MQTT port
export TB_EDGE_MQTT_PORT=YOUR_TB_EDGE_MQTT_PORT # e.g. 11883 or 1883
# Set Air Conditioner device token
export ACCESS_TOKEN=YOUR_ACCESS_TOKEN
# Run cooler.js script
node cooler.js
var 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:

Terminal window
npm install mqtt --save
chmod +x mqtt-js.sh
bash mqtt-js.sh

You should see:

Connecting to: localhost:1883 using access token: <your_token>
Cooler is connected!

Get the DHT22 access token from the Edge.

  1. On the Edge, go to Devices & assets > Devices.
  2. Click DHT22 to open the device details.
  3. 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.

Terminal window
curl -v -X POST \
-d '{"temperature": 51}' \
http://$EDGE_HOST:8080/api/v1/$ACCESS_TOKEN/telemetry \
--header "Content-Type:application/json"

On 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.

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_conditioner
Speed params: 1

The 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.