ThingsBoard Documentation

Documentation for using ThingsBoard IoT Platform. Open documentation for releases before 2.0
ThingsBoard Professional Edition with White-labeling and Platform Integrations Learn More

Send Email Workflow

Use case

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

Prerequisites

We assume you have completed the following guides and reviewed the articles listed below:

Step 1: Adding temperature threshold check field

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.

image

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.

return msg.temperature > 60;

image

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.

image

Step 2: Find email of the Customer

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

image

Customer attributes node should be connected with script node with relation True.

image

Step 3: Find Device Location

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.

image

Originator attributes node should be connected with Customer attributes node with relation Success.

image

Step 4: Put Message payload into Metadata

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.

metadata.temperature = msg.temperature
return {msg: msg, metadata: metadata, msgType: msgType};

image

Script Transform node should be connected with Customer attributes node with relation Success.

image

Step 5: Build Email Message

Now we are ready to define Email template. We should use To email node that builds actual email from configured template.

Configuration notes:

image

To email node should be connected with Script Transform node with relation Success.

image

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.

Step 5: Add Send message node

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.

image

Here is how Send Email node should be configured for working with SendGrid

image

Send email node should be connected with To email node with relation Success.

image

Chain configuration is finished and we need to save it.

Step 6: Verify results

Create device and assign it to customer

We need to create Device Thermostat Server Room:

image

Then our device should have server scope attribute address. Let’s add it:

image

Next we create Customer for our device:

image

Then our device should have server scope attribute email. Note that email ill be sent to this email, so write your email for testing.

image

Now we need to assign our thermostat to the customer. Go to Manage devices on Customer page and select our device

image

image

Post telemetry and verify

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.

image

*you need to replace $ACCESS_TOKEN with actual device token

Lets post temperature = 99. Alarm should be created:

curl -v -X POST -d '{"temperature":99}' http://localhost:8080/api/v1/$ACCESS_TOKEN/telemetry --header "Content-Type:application/json"

Finally we can see that email was received with correct values. (Please check your spam folder if you did not receive any email)

image


TL;DR

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”.

image