The purpose of this tutorial is to show how the Check Relation node can be used to check the relation between Entities.
Let’s assume the following use case:
You have 2 devices:
Smoke Detector with the Smoke Sensor which sends data to ThingsBoard when it appears.
Fire Alarm System which provides a fire alarm, when the smoke is present.
However, there are different ways for the realization of this case, for example, it can be implemented using the Switch node that routes incoming Message to one or multiple output chains.
For more information about how to use the Switch node, please check the link to The article of Switch Node in the See Also section.
You need to read the following guides before you start this tutorial:
Add two Device entity in ThingsBoard:
Smoke Detector is represented as a Device. Its name is Smoke Detector and its type is Smoke Sensor.
Fire Alarm System is represented as a Device. Its name is Fire Alarm System and its type is Fire Alarm Device.
Create a relation of the type Uses:
The following screenshots show how to do this:
In this section, we explain the purpose of each node in this tutorial:
In this tutorial, we modified our Root Rule Chain and also created Rule Chain Related Fire Alarm System
The following screenshots show how the above Rule Chains should look like:
Download the attached json file for the Root Rule Chain. Don’t forget to mark this rule chain as root.
Also, you need to create Related Fire Alarm System Rule Chain or you can download the attached json file for this Chain and import it.
The following section shows you how to create it.
Go to Rule Chains -> Add new Rule Chain
Configuration:
New Rule Chain is created. Press Edit button and configure Chain.
In this rule chain, you will create 4 nodes as it will be explained in the following sections:
Add the Check Relation node and connect it to the Input node.
This node will check the relation from the Device, Fire Alarm System, to the originator of the message Smoke Detector using the type and direction of relation. If the relation exists, the message will be sent through the True chain.
Fill in the fields with the input data shown in the following table:
Field | Input Data |
Name | Check Relation |
Direction | To |
Type | Device |
Device | Fire Alarm System |
Relation type | Uses |
Add the Change Orignator node and connect it to the Check Relation node with a relation type True.
This node will change the originator from the Device Smoke Detector to the related Device Fire Alarm System and the submitted message will be processed as a message from another entity, namely Fire Alarm System.
Fill in the fields with the input data shown in the following table:
Field | Input Data |
Name | Change Originator |
Originator source | Related |
Direction | From |
Max relationship level | 1 |
Relation type | Uses |
Entity type | Device |
This node will transform an original message into RPC request message.
The RPC call will have 2 properties:
Property | Value |
method | ON |
params | {} |
In order to do this, add the following Script:
This Rule chain is ready and we should save it.
The initial Rule Chain has been modified by adding the following nodes:
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 Smoke Detector and Clears the Alarm if it exists.
Enter the Name field as Clear Smoke Alarm and the Alarm type as Smoke Alarm.
Add the Create alarm node and connect it to the Filter Script node with a relation type True.
This node tries to load the latest Alarm with configured Alarm Type for Message Originator, namely Smoke Detector.
Enter the Name field as Create Smoke Alarm and the Alarm type as Smoke Alarm.
Add the Rule Chain node and connect it to the Filter Script node with a relation type True.
This node forwards incoming Message to specified Rule Chain Related Fire Alarm System.
Enter the Name field as Related Fire Alarm System.
The following screenshot shows how the final Root Rule Chain should look like:
Use the following javascript code to emulate the Fire Alarm System device.
To run the script, you need to do the following steps:
Copy the Fire Alarm System device access token, then paste them in the script.
You can copy the access token from the Device page.
Use the Rest APIs, Telemetry upload APIs, for posting telemetry from the device Smoke Detector.
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 third and fourth links under the See Also section to see how to do this.
Switch Node guide - for more information about how to use Switch Node in Thignsboard.
Validate incoming telemetry tutorial - for more information about how to validate an incoming telemetry using the Script Filter node.
Create & Clear Alarms: configure dashboard guide - to learn how to add an Alarm widget to the dashboard.
Send Email tutorial.
RPC capabilities guide - for more information about how RPC works in Thignsboard, please refer to the RPC capabilities 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.