Skip to content
Stand with Ukraine flag

Telemetry delta calculation

Calculate the difference between telemetry values over a rolling time window and use the result to automatically trigger and clear alarms. This guide uses importable configurations so you can examine the logic and adapt it to your own scenario.

Assume a device with a temperature sensor periodically sends telemetry to ThingsBoard. You need to:

  • Monitor temperature changes over a fixed time window (15 minutes).
  • Trigger an alarm when the temperature change exceeds a defined threshold (5 °C).
  • Automatically clear the alarm when the value returns to normal.

This pattern is common in real-time monitoring and anomaly detection and can be extended to support more advanced scenarios.

Review the following documentation before proceeding:

  • Calculated fields — learn how to compute new telemetry values based on incoming data.
  • Alarm rules — learn how to define alarm trigger conditions to respond to abnormal conditions.

Add a device that publishes temperature telemetry. It serves as the data source for the calculated field and alarm rule.

  1. Navigate to Entities ⇾ Devices.
  2. Click + Add device ⇾ Add new device, and set:
    • Device name: Thermometer
    • Device profile: thermostat
  3. Click Add.

The calculated field computes the temperature delta over the last 15 minutes and stores the result as a new telemetry key: deltaTemperature.

  1. Download the configuration file: telemetry_delta_calculation_cf.json
  2. Navigate to Calculated fields.
  3. Click + Add calculated field ⇾ Import calculated field.
  4. Upload the downloaded file and click Import.
  5. In the Calculated field dialog, set Device profile to thermostat so the field applies to all devices using this profile.
  6. Click Add.

The script collects up to 2 temperature values from the last 15-minute rolling window, takes the difference between the last and first values, and stores the absolute result as deltaTemperature:

var delta = 0;
if (temperature.values.size() >= 2) {
delta = temperature.last - temperature.first;
}
return {
"deltaTemperature": Math.abs(toInt(delta))
}

Configure the alarm rule that reacts to changes in deltaTemperature:

  • Create alarm when deltaTemperature ≥ 5
  • Clear alarm when deltaTemperature < 5
  1. Download the configuration file: temperature_deviation_alarm_rule.json
  2. Navigate to Alarms ⇾ Alarm rules.
  3. Click + Add alarm rule ⇾ Import alarm rule.
  4. Upload the downloaded file and click Import.
  5. Select the thermostat device profile as the target entity.
  6. Click Add.

Once imported, the alarm lifecycle is managed automatically by ThingsBoard.

Publish two temperature values within a 15-minute interval. Use Check connectivity or the commands below.

Publish an initial value (e.g. 25 °C):

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"temperature": 25}' \
http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetry

Open the device Latest telemetry tab. You should see:

  • temperature = 25
  • deltaTemperature = 0

Publish a second value within 15 minutes (e.g. 32 °C):

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"temperature": 32}' \
http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetry

After the second update, deltaTemperature = 7 — the threshold is exceeded and the alarm is triggered. Open the device Alarms tab to confirm the alarm status.

As soon as deltaTemperature drops below 5, the alarm is cleared automatically.