Hello-World example
Let's assume your device is using DHT22 sensor to collect and push temperature to the ThingsBoard. DHT22 sensor can measure temperature from -40°C to +80°C.
In this tutorial we will configure ThingsBoard Rule Engine to store all temperature within -40 to 80°C range and log all other readings to the system log.
Adding temperature validation node
In Thingsboard UI go to Rule Chains page and open Root Rule Chain.
Drag and Drop Script Filter rule node to the chain. Node configuration window will be opened.
One can use either TBEL (ThingsBoard expression language) or JavaScript to develop user defined functions. We recommend utilizing TBEL as it’s execution in ThingsBoard is much more efficient compared to JS.
If temperature property not defined or temperature is valid - script will return True, otherwise it will return False. If script returns True incoming message will be routed to the next nodes that are connected with True relation.
Now we want that all telemetry requests pass through this validation script. We need to remove the existing Post Telemetry relation between Message Type Switch node and Save Telemetry node:
And connect Message Type Switch node with Script Filter node using Post Telemetry relation:
Next, we need to connect Script Filter node with Save Telemetry node using True relation. So all valid telemetry will be saved:
Also, we will connect Script Filter node with Log Other node using False relation. So that all not valid telemetry will be logged in the system log:
Press Save button to apply changes.
Validate results
For validating results we will need to create Device and submit telemetry to the Thingsboard. So go to Devices section and create new Device:
For posting device telemetry we will use Rest API. To do this this we will need to copy device access token from the device DHT22.
Use terminal for will send a message with temperature readings = 99. Replace $ACCESS_TOKEN with actual device token.
1
curl -v -X POST -d '{"temperature":99}' http://localhost:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"
We will see that telemetry was not added in Device Latest Telemetry section:
Now send a message with temperature readings = 24.
1
curl -v -X POST -d '{"temperature":24}' http://localhost:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"
We will see that telemetry was saved successfully.
See Also
You can use the next links for learning more about Thingsboard Rule Engine:
- Rule Engine Overview
- Debug Node Execution
- Validate incoming telemetry
- Transform incoming telemetry
- Transform telemetry using previous record
- Create & clear alarms
- Send email on alarm
- Create alarm when the device is offline
- Check Relation between Entities
- RPC Request to Related Device
- Add & remove devices to group dynamically
- Aggregate incoming data stream
Next steps
-
Installation guides - Learn how to set up 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 on how to configure complex ThingsBoard dashboards.
-
Data processing & actions - Learn how to use ThingsBoard Rule Engine.
-
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.