Let’s assume your device is using DHT22 sensor to collect and push temperature readings to ThingsBoard. Sometimes temperature in the server room is to high and want to send emails to your customer if temperature > 60 °C.
In this tutorial we will configure ThingsBoard Rule Engine to
We assume you have completed the following guides and reviewed the articles listed below:
Here is how our initial Root Rule Chain should look like. Please note that we have removed irrelevant rule nodes from the root rule chain.
We will modify default rule chain and will add script filter node with temperature threshold check script. If temperature is higher then 60 °C, script will return True, otherwise False will be returned.
Script node should be connected with Save timeseries node with relation Success. After telemetry from device will be saved into the database, original message will be passed to our script node.
If published temperature is high (script node returns True) we want to create send an email to the customer of the device. For making it, we will need to find email of the customer.
We will add customer attributes enrichment node. And configure it to take email attribute of the customer and save it in Message Metadata property customerEmail
Customer attributes node should be connected with script node with relation True.
Also we want to add device address to the email body. It is a server scope attribute of the device.
We will add originator attributes enrichment node. And configure it to take address server scope attribute of the originator (device is an originator of the incoming message). This attribute will be saved in the Message Metadata property ss_address.
Originator attributes node should be connected with Customer attributes node with relation Success.
All email nodes works with properties that are defined in the Message Metadata. There is no direct access to the Message payload. If we want to add something into email from the Message payload, first we need to put it in Message Metadata.
We want to add current temperature to the email body. So we will use Script Transform node for saving current temperature in the Message Metadata.
Script Transform node should be connected with Customer attributes node with relation Success.
Now we are ready to define Email template. We should use To email node that builds actual email from configured template.
To email node should be connected with Script Transform node with relation Success.
Note 1 - you can use references to any fields that exists in Message Metadata Note 2 - this node does not send actual email. Only construct email from configured template.
Final configuration step as adding Send email node that will actual send email from inbound message. In node configuration we will not choose Use system SMTP settings option and make our own SMTP configuration for this node.
In the scope of this tutorial we will use SendGrid as SMTP provider and Thingsboard will send email using this provider. You can sign-up for trial using this link.
Once logged in into SendGrid open SMTP relay configuration page.
Here is how Send Email node should be configured for working with SendGrid
Send email node should be connected with To email node with relation Success.
Chain configuration is finished and we need to save it.
We need to create Device Thermostat Server Room:
Then our device should have server scope attribute address. Let’s add it:
Next we create Customer for our device:
Then our device should have server scope attribute email. Note that email ill be sent to this email, so write your email for testing.
Now we need to assign our thermostat to the customer. Go to Manage devices on Customer page and select our device
For posting device telemetry we will use Rest API (link). For this we will need to copy device access token from then device Thermostat Server Room.
*you need to replace $ACCESS_TOKEN with actual device token
Lets post temperature = 99. Alarm should be created:
Finally we can see that email was received with correct values. (Please check your spam folder if you did not receive any email)
Download and import attached rule chain json file with a rule chain from this tutorial. Don’t forget to mark new rule chain as “root”.
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