Skip to content
Stand with Ukraine flag

Enrich Alarms with Details

This tutorial shows how to enrich alarm details with custom data using the Rule Engine — tracking the triggering temperature reading and a running alarm count that increments with each occurrence.

A Thermostat device publishes temperature readings. When temperature goes outside the acceptable range, ThingsBoard:

  • Creates an alarm and stores in its details:
    • temperature — the value that triggered the alarm
    • count — how many times the alarm has fired since it was last cleared
  • Clears the alarm when temperature returns to normal, appending clearedTemperature to the existing details

All three fields can be displayed as columns in an alarm widget on a dashboard.

You need a device to test with. Use an existing device or create one:

  • Name: Thermometer A1
  • Type: thermostat

Create a new rule chain named Create & Clear Alarms with Details, open it, and add the following nodes:

Add a Script filter node and connect it to the Input node.

This node returns true when the temperature is outside the valid sensor range, routing the message to the Create Alarm branch; otherwise it returns false and routes to the Clear Alarm branch.

  • Name: Temperature Range Filter
  • Add the following script:
return msg.temperature < -25 || msg.temperature > 15;

Add a Create Alarm node and connect it to the Temperature Range Filter node with relation type True.

Fill in the fields:

  • Name: Create Temperature Alarm
  • Alarm type: Temperature Out of Range
  • Alarm severity: Critical

Enter the following Details function:

details = {};
details.temperature = msg.temperature;
if (metadata.containsKey("prevAlarmDetails") && metadata.prevAlarmDetails != null) {
prevDetails = JSON.parse(metadata.prevAlarmDetails);
details.count = prevDetails.count + 1;
} else {
details.count = 1;
}
return details;

The function stores the triggering temperature and increments count on each alarm update. On first trigger, count is set to 1.

Add a Clear Alarm node and connect it to the Temperature Range Filter node with relation type False.

Fill in the fields:

  • Name: Clear Temperature Alarm
  • Alarm type: Temperature Out of Range

Enter the following Alarm Details function:

details = {};
if (metadata.containsKey("prevAlarmDetails") && metadata.prevAlarmDetails != null) {
details = JSON.parse(metadata.prevAlarmDetails);
}
details.clearedTemperature = msg.temperature;
return details;

The function preserves the existing alarm details (retaining temperature and count) and appends the temperature reading at the moment of clearing.

  • Input ⇾ Temperature Range Filter
  • Temperature Range Filter ⇾ True ⇾ Create Temperature Alarm
  • Temperature Range Filter ⇾ False ⇾ Clear Temperature Alarm

Save the rule chain.

In the Root Rule Chain, add a Rule Chain node connected to the Save Timeseries node with relation type Success:

  • Name: Create & Clear Alarms with Details
  • Rule Chain: Create & Clear Alarms with Details

Save the rule chain.

To display alarm details in the widget, download the attached JSON file for the dashboard and import it.

Use curl to publish telemetry to Thermostat A1. Replace $ACCESS_TOKEN with the device access token.

Trigger the alarm — publish a temperature above 15 °C:

Terminal window
curl -v -X POST -d '{"temperature":25}' https://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

The alarm is created with details.temperature = 25 and details.count = 1.

Update the alarm — publish another out-of-range value:

Terminal window
curl -v -X POST -d '{"temperature":28}' https://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

The alarm is updated: details.temperature = 28, details.count = 2.

Clear the alarm — publish a value within the normal range:

Terminal window
curl -v -X POST -d '{"temperature":12}' https://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

The alarm is cleared with details.clearedTemperature = 12 appended to the existing details.