This Tutorial is to show you how to send an Email to the customer using the Rule Engine.
This tutorial is based on the send email on alarm tutorial and it’s use case. We will reuse the rule chains from the above-mentioned tutorial and will add a few more rule nodes to send email to the customer of the assigned device.
Let’s assume your device is using DHT22 sensor to collect and push temperature readings to ThingsBoard. DHT22 sensor is good for -40 to 80°C temperature readings.We want to generate Alarms if temperature is out of good range and send the email when the alarm was created.
In this tutorial we will configure ThingsBoard Rule Engine to:
Send an email to the customer of the assigned device if the temperature was out of range, namely: less than -40 and more than 80 degrees.
Add message originator attributes to the message.
Add additional data to the email body using Script Transform node from the incoming message.
We assume you have completed the following guides and reviewed the articles listed below:
First of all, we need to create Customer and assign device to customer. The following screenshots show you how to do this:
Customer created. Now we need to assign device Thermostat Home(the creation of which was described in the Create & clear alarms tutorial) to the customer.
Go to Manage devices on Customer page and select our device
Next our customer should have server scope attribute email. Note that email will be sent to this email, so write your email for testing.
Also we need add server scope attribute - address to our device Thermostat Home:
Go to Devices -> Thermostat Home -> Attributes -> Server attributes and press + button to add address
In this section, we explain the purpose of each node that was added or modified to initial rule chains in this tutorial:
In this tutorial, we used Rule Chains from send email on alarm tutorial.
We modified Rule Chain Create/Clear Alarm & Send Email by adding nodes that was described above in the section Message flow
and renamed this rule chain to: Create/Clear Alarm & Send Email to Customer.
The following screenshots show how the above Rule Chains should look like:
Download the attached json file for the Create/Clear Alarm & Send Email to Customer: rule chain. Create Node D 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 modify this rule chain, specifically: add rule nodes A and B and modify node C.
In this rule chain, you will add 2 nodes and modify 1 node as it will be explained in the following sections:
Add the Customer attributes node and connect it to the Filter Script node with a relation type True.
This node will be used for taking email attribute of the customer and save it in
Message Metadata property customerEmail
Fill in the fields with the input data shown in the following table:
Field | Input Data |
Name | Get Customer Email |
Source attribute | |
Target attribute | customerEmail |
Add the Originator attributes node and pastes it between the nodes: Customer attributes and Create alarm with a relation type Success.
This node will be used for taking address server scope attribute of the originator (Thermostat Home). This attribute will be saved in the Message Metadata property ss_address.
Fill in the fields with the input data shown in the following table:
Field | Input Data |
Name | Get Device Address |
Server attributes | address |
Modify the To Email node. For this, we need change some fields in details of this node, namely:
To template.
Body tempalte.
Fill in the fields with the input data shown in the following table:
Field | Input Data |
To template | ${customerEmail} |
Body tempalte | Device ${deviceName} has unacceptable temperature: ${temperature}. Device address - ${ss_address} |
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 Thermostat Home.
Lets post temperature = 99. Mail should be sent:
You should understand that message won’t be sent to the email when the alarm was updated, only in the case when alarm will 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)
Also, you can see the more information about how to:
define other additional logic for alarm processing, for example, sending notification to Telegram App using Telegram Bot.
configure Alarm Details function in the Create and Clear Alarm nodes and configure the Dashboard by adding an alarm widget to visualize the alarms..
create alarm when device is offline.
Please refer to the links under the See Also section to see how to do this.
Notifications and Alarms on your smartphone using Telegram Bot guide
Create alarm with details 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.