IoT Hub
IoT Hub Documentation
Alarms based on sensor readings
Getting Started Documentation Guides API FAQ

On this page

Alarms based on sensor readings

This tutorial will demonstrate how to configure Rule that will generate Alarm when certain device reports temperature or humidity that exceeds certain thresholds.

Let’s assume that we have devices that are able to report humidity and temperature values. We have one device per room (zone) in the building or other facility and we want to specify different rules based on zone type.

Assumptions

We assume you have already configured email plugin that will distribute generated alarms to recipients. You can follow previous tutorial to do this.

How it works?

We will provision simple rule that filters incoming data using:

  • “Message type” filter to react on telemetry data.
  • “Device Attributes” filter to process data from a device that has certain room type as a server side attribute.
  • “Device Telemetry” filter to detect humidity and temperature values that are out of pre-configured range.

Device provisioning

Let’s create a Device and provision certain server-side attributes: ZoneId and ZoneType.

Step 1. Create Device

Navigate to devices page and click on big red “+” button. Populate device name and description and click “Add” button.

image

Step 2. Provision ZoneID and ZoneType attributes

Open device card that you have created. Navigate to “Attributes” tab and select “Server” attributes scope.

image

Click on the highlighted “+” button. Add two attributes “ZoneId” and “ZoneType” as shown below. We will use them later in the rule filters.

image image

Rule configuration

Step 3. Create “Server Room Monitoring” Rule

Navigate to rules page and click on big red “+” button. Populate rule name and description first.

image

Our rule will contain three filters as described in “how it works” section.

Step 4. Message type filter

Add filter based on message type (see image below).

image

Step 5. Attributes filter

Add filter based on the server-side attributes (see image below).

1
typeof ss.ZoneType !== 'undefined' && ss.ZoneType === 'Server Room'

image

Step 6. Telemetry filter

1
2
3
4
5
6
7
8
9
(
    typeof temperature !== 'undefined' 
    && (temperature <= 10 || temperature >= 25)
)
|| 
(
    typeof humidity !== 'undefined' 
    && (humidity <= 40 || humidity >= 60)
)

Add filter based on the sensor reading (see image below).

image

Step 7. Alarm Processor

Let’s add simple processor that will generate and save alarm to the database based on templates below.

Alarm ID:

1
[$date.get('yyyy-MM-dd HH:mm')] $ss.get('ZoneId') HVAC malfunction detected!

Alarm Body:

1
2
3
[$date.get('yyyy-MM-dd HH:mm:ss')] $ss.get('ZoneId') HVAC malfunction detected. 
Temperature - $temperature.valueAsString (°C). 
Humidity - $humidity.valueAsString (%)!

image

NOTE Alarm Id is a unique identifier. If there will be multiple events that match filters, alarms will be de-duplicated based on the Alarm Id. An email will be sent once per alarm.

In our case, we use a timestamp that is truncated to minutes to make sure that we will send an email once per minute or less frequently.

Step 8. Rule Action

Select “SendGrid Email Plugin” from previous tutorial and click on “Create” button. Don’t forget to replace “thingsboard@gmail.com” with your email address.

image

Step 9. Save and Activate Rule

Once a rule is saved successfully, don’t forget to activate it by clicking on “Activate” button (see image below).

image

Dry run

Let’s check our configuration by publishing some telemetry data. We will use access token from the device that we have created in the first step.

1
mosquitto_pub -d -h "iothub.magenta.at" -t "v1/devices/me/telemetry" -u "$YOUR_ACCESS_TOKEN" -m "{'temperature':42, 'humidity':74}"

Troubleshooting

If you have configured something wrong, you should see errors logged on the corresponding tab:

image

If there is no error in the rule, but you can’t see the email - check errors in the target plugin.