ThingsBoard Professional Edition with White-labeling and Platform Integrations Learn More

Telemetry delta calculation

Use case

Let’s assume we have a device that uses a temperature sensor to collect and read temperature readings in the ThingsBoard. In addition, let’s assume that we need to generate the alarm when the delta between the last five-minutes temperature readings and the latest temperature reading does exceed 5 degrees. Please note that this is just a simple theoretical use case to demonstrate the capabilities of the platform. You can use this tutorial as a basis for much more complex scenarios.


We assume you have completed the following guides and reviewed the articles listed below:

Adding the device

Add Device entity in ThingsBoard. Its name is Thermometer and its type is temperature sensor.


Message flow

In this section, we explain the purpose of each node in this tutorial. There will be two rule chains involved:

The following screenshots show how the above Rule Chains should look like:



Download the attached json file for the Temperature delta validation rule chain.

Create Node G as shown on the image above in the root rule chain to forward telemetry to the imported rule chain.

The following section shows you how to create this rule chain from scratch.

Create new Rule Chain (Temperature delta validation)

Go to Rule Chains -> Add new Rule Chain



New Rule Chain is created. Press Edit button and configure Chain.

Adding the required nodes

In this rule chain, you will create 6 nodes as it will be explained in the following sections:

Node A: Originator telemetry

The rule node has three fetch modes:

We will use fetch mode: LAST with the time range from 24 hours ago till 5 minutes.


Fetch Mode ALL

Originator telemetry node also supports ability to fetch all telemetry from the particular time range. We will not use this ability in our tutorial, but it may be useful in the cases if you need to calculate variance for a particular key or to predict further change of telemetry depending on telemetry changes in the selected time range.

In this case, you need to select the fetch mode ALL. It will force rule node to fetch all telemetry from the specified time range and add it to the message metadata as an array. This array will contain JSON objects with the timestamp and value.

    "temperature": "[{\"ts\":1540892498884,\"value\":22.4},{\"ts\":1540892528847,\"value\":20.45},{\"ts\":1540892558845,\"value\":22.3}]"
  var temperatureArray = JSON.parse(metadata.temperature);
      "temperatureArray": [{
          "ts": 1540892498884,
          "value": 22.4
      }, {
          "ts": 1540892528847,
          "value": 20.45
      }, {
          "ts": 1540892558845,
          "value": 22.3
Node B: Script Transformation

This node will calculate the delta between the temperature reading from message payload and the five-minute old temperature reading from the message metadata using the following script:

   var newMsg = {};
   newMsg.deltaTemperature = parseFloat(Math.abs(msg.temperature - JSON.parse(metadata.temperature)).toFixed(2));
   return {msg: newMsg, metadata: metadata, msgType: msgType};


Node C: Save Timeseries


Node D: Filter Script
   return msg.deltaTemperature > 5;


Node E: Create alarm


Node F: Clear Alarm


Modify Root Rule Chain

The initial root Rule Chain has been modified by adding the following node:

Node G: Rule Chain


The following screenshot shows how the final Root Rule Chain should look like:


How to verify the Rule Chain and Post telemetry

For posting device telemetry we will use the Rest APIs, Telemetry upload APIs. For this we will need to copy device access token from then device Thermometer.


**you need to replace $ACCESS_TOKEN with actual device token**

To validate that rule chains works as expected, we need to post telemetry twice for the same device, with an interval, not less than 5 minutes and not more than 24 hours.
Also, let’s pushed debug mode button in Create Alarm node to verify that alarm will be created after the second post telemetry request.


sent temperature = 20.

curl -v -X POST -d '{"temperature":20}' http://localhost:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"


After 5 minutes delay let’s sent e.g temperature = 26

curl -v -X POST -d '{"temperature":26}' http://localhost:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"


Alarm should be created:


Also, you can:

Please refer to the links from the second to the fourth under the See Also section to see how to do this.

See Also

Next steps