Skip to content
Stand with Ukraine flag

Send Alarm Email to Customer

Send a temperature alarm email to a customer using the Rule Engine. The email recipient and device address are read dynamically from entity attributes — no hardcoded addresses required.

Thermostat Home sends temperature telemetry. The device is assigned to Customer A, who has an email server attribute.

When the temperature goes outside the acceptable range (below −40 °C or above 80 °C):

  • ThingsBoard fetches the customer’s email from their attributes
  • Fetches the device’s physical address from the device’s server attributes
  • Creates an alarm
  • Sends an email to the customer with the device name, temperature value, and address

Go to Customers ⇾ Customer A (Create if it doesn't exist) ⇾ Manage customer devices and create an device under Customer A:

  • Name: Thermostat A1
  • Device profile: thermostat

Open Building A ⇾ Attributes tab and add the following Server attribute:

  • Key: address
  • Type: String
  • Value: the physical address of the device (e.g. Main Street 12, Floor 3)

Open Customer AAttributes tab and add a server attribute:

  • Key: email
  • Type: String
  • Value: the customer’s email address

You can import a ready-made rule chain or build it manually.

Option 1. Import rule chain

  • Download the Send Alarm Email to Customer rule chain as a JSON file and import it into your instance.

Option 2. Create manually

Add a Filter Script node connected to the Input node.

Passes the message only when the temperature is outside the acceptable range.

  • Name: Check Temperature Range

  • Add the following script:

    return msg.temperature < -15 || msg.temperature > 30;

Add a Customer Attributes node connected to the Check Temperature Range node with relation type True.

Loads the customer’s email attribute into message metadata as customerEmail.

  • Name: Get Customer Email
  • Mapping of customer’s: Attributes
  • Attributes mapping:
    • Source attribute key: email
    • Target key: customerEmail

Add an Originator Attributes node connected to the Get Customer Email node with relation type Success.

Loads the device’s address server attribute into message metadata as ss_address.

  • Name: Get Device Address
  • Server attributes: address

Add the Transformation Script node and place it after the Get Device Address node with a relation type Success.

This node will use for saving current temperature from Message Data to the Message Metadata using the following script:

  • Name: Add Temperature to Metadata

  • Add the following script:

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

Add a Create Alarm node connected to the Add Temperature to Metadata node with relation type Success.

Creates an alarm when the temperature is out of range.

  • Name: Create Temperature Alarm
  • Alarm type: High Temperature
  • Alarm severity: Critical

Add a To Email node connected to the Create Temperature Alarm node with relation type Created.

Transforms the message into an email using metadata variables populated by the earlier enrichment nodes.

  • Name: Build Email
  • Email sender:
  • Recipients:
    • To: ${customerEmail}
  • Message subject and content:
    • Subject template:

      Temperature alert on device ${deviceName}
    • Mail body type: Plain text

    • Body template:

      Device ${deviceName} has an unacceptable temperature: ${temperature}°C.
      Device address: ${ss_address}

Add a Send Email node connected to the Build Email node with relation type Success.

Sends the email via the mail server configured in ThingsBoard system settings.

  • Name: Send Email
  • Use system SMTP settings: enabled
  • Input ⇾ Check Temperature Range
  • Check Temperature Range ⇾ True ⇾ Get Customer Email
  • Get Customer Email ⇾ Success ⇾ Get Device Address
  • Get Device Address ⇾ Success ⇾ Add Temperature to Metadata
  • Add Temperature to Metadata ⇾ Success ⇾ Create Temperature Alarm
  • Create Temperature Alarm ⇾ Created ⇾ Build Email
  • Build Email ⇾ Success ⇾ Send Email

Save the rule chain.

In the Root Rule Chain, add a Rule Chain node connected to the Message Type Switch node with relation type Post telemetry:

  • Name: Send Alarm Email to Customer
  • Rule Chain: Send Alarm Email to Customer

Save the rule chain.

Post a temperature value that exceeds 30 °C. Replace $ACCESS_TOKEN with the access token of Thermostat A1.

Terminal window
curl -v -X POST \
--header "Content-Type: application/json" \
--data '{"temperature": 32}' \
https://$THINGSBOARD_HOST/api/v1/$ACCESS_TOKEN/telemetry

Expected result:

A High Temperature Critical alarm is created on Thermometer A1 and appears in the Alarms list.

The customer receives an email at the address stored in their email attribute. The email subject shows the device name and the body includes the temperature value and device address.