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.
Use case
Section titled “Use case”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.
Prerequisites
Section titled “Prerequisites”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.
Step 1. Add a demo device
Section titled “Step 1. Add a demo device”Add a device that publishes temperature telemetry. It serves as the data source for the calculated field and alarm rule.
- Navigate to Entities ⇾ Devices.
- Click + Add device ⇾ Add new device, and set:
- Device name:
Thermometer - Device profile:
thermostat
- Device name:
- Click Add.
Step 2. Import the calculated field
Section titled “Step 2. Import the calculated field”The calculated field computes the temperature delta over the last 15 minutes and stores the result as a new telemetry key: deltaTemperature.
- Download the configuration file: telemetry_delta_calculation_cf.json
- Navigate to Calculated fields.
- Click + Add calculated field ⇾ Import calculated field.
- Upload the downloaded file and click Import.
- In the Calculated field dialog, set Device profile to
thermostatso the field applies to all devices using this profile. - Click Add.
Calculation logic
Section titled “Calculation logic”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))}Step 3. Import the alarm rule
Section titled “Step 3. Import the alarm rule”Configure the alarm rule that reacts to changes in deltaTemperature:
- Create alarm when
deltaTemperature ≥ 5 - Clear alarm when
deltaTemperature < 5
- Download the configuration file: temperature_deviation_alarm_rule.json
- Navigate to Alarms ⇾ Alarm rules.
- Click + Add alarm rule ⇾ Import alarm rule.
- Upload the downloaded file and click Import.
- Select the
thermostatdevice profile as the target entity. - Click Add.
Once imported, the alarm lifecycle is managed automatically by ThingsBoard.
Step 4. Verify the configuration
Section titled “Step 4. Verify the configuration”Publish two temperature values within a 15-minute interval. Use Check connectivity or the commands below.
Publish an initial value (e.g. 25 °C):
curl -v -X POST \--header "Content-Type: application/json" \--data '{"temperature": 25}' \http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetryOpen the device Latest telemetry tab. You should see:
temperature = 25deltaTemperature = 0
Publish a second value within 15 minutes (e.g. 32 °C):
curl -v -X POST \--header "Content-Type: application/json" \--data '{"temperature": 32}' \http://$THINGSBOARD_HOST_NAME/api/v1/$ACCESS_TOKEN/telemetryAfter 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.
See also
Section titled “See also”- Calculated fields — full configuration reference
- Alarm rules — full configuration reference
- Alarm rule tutorials — step-by-step examples for thresholds, duration, schedules, and more