ThingsBoard Professional Edition with White-labeling and Platform Integrations Learn More

Send Email to Customer

This Tutorial is to show you how to send an Email to the customer using the Rule Engine.

Note:

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.


Use case

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:

Prerequisites

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

Create customer and assign device

First of all, we need to create Customer and assign device to customer. The following screenshots show you how to do this:

image


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

image


image


Next our customer should have server scope attribute email. Note that email will be sent to this email, so write your email for testing.

image


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

image


Message flow

In this section, we explain the purpose of each node that was added or modified to initial rule chains in this tutorial:


Configure Rule Chains

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:

image

image


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.

Modify Create & Clear Alarms with details:

Modify the required nodes

In this rule chain, you will add 2 nodes and modify 1 node as it will be explained in the following sections:

Node A: Customer attributes

Field Input Data
Name Get Customer Email
Source attribute email
Target attribute customerEmail

image

Node B: Originator attributes

Field Input Data
Name Get Device Address
Server attributes address

image

Node C: To Email

Field Input Data
To template ${customerEmail}
Body tempalte Device ${deviceName} has unacceptable temperature: ${temperature}. Device address - ${ss_address}

image

Post telemetry and verify

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.

image

Lets post temperature = 99. Mail should be sent:

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

**you need to replace $ACCESS_TOKEN with actual device token**

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)

image

Also, you can see the more information about how to:

Please refer to the links under the See Also section to see how to do this.



See Also

Next steps