Important note Please note that this guide is for ThingsBoard versions prior v2.0. Old rules and plugins functionality is replaced by new rule engine components (rule chains and rule nodes). Please review new rule engine documentation to learn how to adopt new functionality. We are doing our best to modify this guide to v2.0 components. Contributions are welcome. |
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.
We assume you have already configured email plugin that will distribute generated alarms to recipients. You can follow previous tutorial to do this.
We will provision simple rule that filters incoming data using:
Let’s create a Device and provision certain server-side attributes: ZoneId and ZoneType.
Navigate to devices page and click on big red “+” button. Populate device name and description and click “Add” button.
Open device card that you have created. Navigate to “Attributes” tab and select “Server” attributes scope.
Click on the highlighted “+” button. Add two attributes “ZoneId” and “ZoneType” as shown below. We will use them later in the rule filters.
Navigate to rules page and click on big red “+” button. Populate rule name and description first.
Our rule will contain three filters as described in “how it works” section.
Add filter based on message type (see image below).
Add filter based on the server-side attributes (see image below).
typeof ss.ZoneType !== 'undefined' && ss.ZoneType === 'Server Room'
(
typeof temperature !== 'undefined'
&& (temperature <= 10 || temperature >= 25)
)
||
(
typeof humidity !== 'undefined'
&& (humidity <= 40 || humidity >= 60)
)
Add filter based on the sensor reading (see image below).
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 (%)!
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.
Select “SendGrid Email Plugin” from previous tutorial and click on “Create” button. Don’t forget to replace “thingsboard@gmail.com” with your email address.
Once a rule is saved successfully, don’t forget to activate it by clicking on “Activate” button (see image below).
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.
mosquitto_pub -d -h "demo.thingsboard.io" -t "v1/devices/me/telemetry" -u "$YOUR_ACCESS_TOKEN" -m "{'temperature':42, 'humidity':74}"
If you have configured something wrong, you should see errors logged on the corresponding tab:
If there is no error in the rule, but you can’t see the email - check errors in the target plugin.