Let’s assume we have a device that uses a temperature sensor to collect and read temperature readings in the ThingsBoard. In addition, let’s assume that we need to generate the alarm when the delta between the last five-minutes temperature readings and the latest temperature reading does exceed 5 degrees. Please note that this is just a simple theoretical use case to demonstrate the capabilities of the platform. You can use this tutorial as a basis for much more complex scenarios.
We assume you have completed the following guides and reviewed the articles listed below:
Add Device entity in ThingsBoard. Its name is Thermometer and its type is temperature sensor.
In this section, we explain the purpose of each node in this tutorial. There will be two rule chains involved:
Root rule chain - rule chain that actually saves telemetry from devices into the database, and redirect the messages to Temperature delta validation chain
Temperature delta validation - rule chain that actually calculates the delta between the last five-minutes temperature and latest temperature readings.
As a result, if delta value exceeds 5 degrees, the alarm will be created/updated, otherwise, the alarm will be cleared.
The following screenshots show how the above Rule Chains should look like:
Download the attached json file for the Temperature delta validation rule chain.
Create Node G as shown on the image above in the root rule chain to forward telemetry to the imported rule chain.
The following section shows you how to create this rule chain from scratch.
Go to Rule Chains -> Add new Rule Chain
New Rule Chain is created. Press Edit button and configure Chain.
In this rule chain, you will create 6 nodes as it will be explained in the following sections:
The rule node has three fetch modes:
FIRST: retrieves telemetry from the database that is closest to the beginning of the time range
LAST: retrieves telemetry from the database that is closest to the end of the time range
ALL: retrieves all telemetry from the database, which is in the specified time range.
We will use fetch mode: LAST with the time range from 24 hours ago till 5 minutes.
Originator telemetry node also supports ability to fetch all telemetry from the particular time range. We will not use this ability in our tutorial, but it may be useful in the cases if you need to calculate variance for a particular key or to predict further change of telemetry depending on telemetry changes in the selected time range.
In this case, you need to select the fetch mode ALL. It will force rule node to fetch all telemetry from the specified time range and add it to the message metadata as an array. This array will contain JSON objects with the timestamp and value.
This node will calculate the delta between the temperature reading from message payload and the five-minute old temperature reading from the message metadata using the following script:
Add the Save TimeSeries node and connect it to the Script Transformation node with a relationship type Success.
This node will save the TimeSeries data from the incoming Message payload into the database and link it to the Device that is identified as the Message Originator.
Enter the Name field as Save Time Series.
Add the Create alarm node and connect it to the Filter Script node with a relation type True.
This node loads the latest Alarm with configured Alarm Type for Message Originator, namely Thermometer
if the published delta temperature is not at expected range (filter script node returns True).
Enter the Name field as Create alarm and the Alarm type as General Alarm.
Add the Clear Alarm node and connect it to the Filter Script node with a relation type False.
This node loads the latest Alarm with configured Alarm Type for Message Originator Thermometer
and Clears alarm if it exists in case if the published temperature delta is in expected range (script node returns False).
Enter the Name field as Clear Alarm and the Alarm type as General Alarm.
The initial root Rule Chain has been modified by adding the following node:
Add the Rule Chain node and connect it to the Save Timeseries node with a relation type Success.
This node forwards incoming Message to specified Rule Chain Temperature delta validation.
Select the Rule Chain field: Temperature delta validation.
The following screenshot shows how the final Root Rule Chain should look like:
For posting device telemetry we will use the Rest APIs, Telemetry upload APIs. For this we will need to copy device access token from then device Thermometer.
To validate that rule chains works as expected, we need to post telemetry twice for the same device, with an interval, not less than 5 minutes and not more than 24 hours.
Also, let’s pushed debug mode button in Create Alarm node to verify that alarm will be created after the second post telemetry request.
sent temperature = 20.
After 5 minutes delay let’s sent e.g temperature = 26
Alarm should be created:
Also, you can:
configure Alarm Details function in the Create and Clear Alarm nodes.
configure the Dashboard by adding an alarm widget to visualize the alarms.
define an additional logic for alarm processing, for example, sending an email.
Please refer to the links from the second to the fourth under the See Also section to see how to do this.
Validate incoming telemetry tutorial - for more information about how to validate an incoming telemetry using the Script Filter node.
Create & Clear Alarms: alarm details: guide - to learn how to configure Alarm Details function in Alarm nodes.
Create & Clear Alarms: configure dashboard guide - to learn how to add an Alarm widget to the dashboard.
Send Email tutorial.
Getting started guides - These guides provide quick overview of main ThingsBoard features. Designed to be completed in 15-30 minutes.
Installation guides - Learn how to setup ThingsBoard on various available operating systems.
Connect your device - Learn how to connect devices based on your connectivity technology or solution.
Data visualization - These guides contain instructions how to configure complex ThingsBoard dashboards.
IoT Data analytics - Learn how to use rule engine to perform basic analytics tasks.
Hardware samples - Learn how to connect various hardware platforms to ThingsBoard.
Advanced features - Learn about advanced ThingsBoard features.
Contribution and Development - Learn about contribution and development in ThingsBoard.