Open-source IoT Platform

Device management, data collection, processing and visualization
for your IoT solution
Learn about Thingsboard

Wednesday, December 21, 2016

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.

Lets 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 recepients. 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 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).

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

image

Step 6. Telemetry filter

(
    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:

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

Alarm Body:

[$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. Email will be sent once per alarm.

In our case we use timestamp that is truncated to minutes to make sure that we will send 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 “[email protected]” with your mail address.

image

Step 9. Save and Activate Rule

Once 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 on the first step.

mosquitto_pub -d -h "demo.thingsboard.io" -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.

Your feedback

If you found this article interesting, please leave your feedback in the comments section, post questions or feature requests on the forum and “star” our project on the github in order to stay tuned for new releases and tutorials.

No comments :

Post a Comment