This tutorial is to show you how to create an alarm when the device is offline for a certain period of time using RuleEngine.
Let’s assume the following use case:
you have a device connected to ThingsBoard and this device has a temperature sensor to collect and push the telemetry data.
the temperature sensor may stop pushing the telemetry data due to any kind of faults.
Therefore, in this case, you will need to configure ThingsBoard Rule Engine to:
create an alarm if the device remains inactive for a certain period of time. This period of time can be defined in either of two ways:
The first way: by changing the global configuration parameter for the inactivity timeout.
This parameter is defined in thingsboard.yml (state.defaultInactivityTimeoutInSec) and by default it is set to 10 seconds.
The second way: by overwriting this parameter for a particular device by setting the “inactivityTimeout” server-side attribute (value is set in milliseconds).
This way will be described in the following sections.
clear the alarm if the device becomes active.
The ThingsBoard Device State service is responsible for monitoring the device connectivity state and triggering the device connectivity events that are pushed to Rule Engine.
ThingsBoard supports four types of events:
Event Type | Description |
Connect | triggered when the device connects to ThingsBoard. |
Disconnect | triggered when the device disconnects from ThingsBoard. |
Activity | triggered when the device pushes a telemetry, an attribute update or RPC command. |
Inactivity | triggered when the device is inactive for a certain period of time. |
This tutorial will explain in details the device Inactivity event and it will show you how to:
create Inactivity alarms using Rule Engine.
configure a parameter for the inactivity timeout.
Go to Devices -> Temperature device -> Attributes -> Server attributes and Press Add button;
Set “inactivityTimeout” attribute, for example, to a value equals 60000 milliseconds.
The following screenshot shows the initial Root Rule Chain. Please, note that the irrelevant rule nodes have been removed from the Root Rule Chain.
The default rule chain has been modified by adding the following two action nodes:
The Create alarm node: connected to the Message Type Switch node with a relationship type Inactivity Event;
The Clear alarm node: connected to the Message Type Switch node with a relationship type Activity Event.
The following screenshot shows how the final Rule Chain should look like:
Download the attached json file for the rule chain indicated above and import it.
Don’t forget to mark the new rule chain as “root”.
Also, you can create the new Rule Chain from scratch. The following section shows you how to create it.
Go to Rule Chains -> Add new Rule Chain
Enter the Name field as Tutorial of Inactivity Event, then click the ADD button.
The new Rule Chain is created. Don’t forget to mark it as “root”.
In this tutorial, you will create 5 nodes as it will be explained in the following sections:
Add the Message Type Switch node and connect it to the Input node.
This node will route the incoming messages according to the message type, namely:
POST_TELEMETRY_REQUEST;
POST_ATTRIBUTES_REQUEST;
ACTIVITY_EVENT;
INACTIVITY_EVENT.
Enter the Name field as Message Type Switch, then click the ADD button.
Add the Save TimeSeries node and connect it to the Message Type Switch node with a relationship type Post telemetry.
This node will store the TimeSeries data from the incoming Message payload into the database and link it to the Device that is identified by the Message Originator.
Enter the Name field as Save Time Series.
Add the Save Attributes node and connect it to the Message Type Switch node with a relationship type Post attributes.
This node will store attributes from the incoming Message payload into the database and link them to the Entity that is identified by the Message Originator.
Enter the Name field as Save Server Attributes.
Add the Create alarm node and connect it to the Message Type Switch node with a relationship type Inactivity Event.
This node tries to load the latest Alarm with the configured Alarm Type for the Message Originator. If Uncleared Alarm exists, then this Alarm will be updated, otherwise, a new Alarm will be created.
Add the Clear alarm node and connect it to the Message Type Switch node with a relationship type Activity Event.
This Node loads the latest Alarm with the configured Alarm Type for the Message Originator and Clear the Alarm if it exist.
This Rule chain is now ready and you need to save it.
Try to post temperature = 20. Alarm should be created a minute after the telemetry post:
Also, you can:
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 first two links under the See Also section to see how to do this.
Create & Clear Alarms: configure dashboard guide - to learn how to add an Alarm widget to the dashboard.
Send Email guide.
Device Connectivity Status guide.
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.