Stand with Ukraine flag
Pricing Try it now
Cloud
North America
Data function based on telemetry from 2 devices
Getting Started Documentation Guides API FAQ
On this page

Data function based on telemetry from 2 devices

This guide demonstrates how to calculate a telemetry delta using data from two different devices in ThingsBoard and store the result as asset telemetry.

As an example, we calculate the temperature difference between indoor and outdoor thermometers installed in a warehouse. The calculated value is stored as a new telemetry key and can be used for monitoring, visualization, or alerting.

This guide is introductory and focuses on demonstrating the core capabilities of the platform, rather than building configurations from scratch.
For this reason, predefined calculated field configuration are provided and imported during the setup.

After importing these configurations into your ThingsBoard instance, you can examine their structure, logic, and behavior, and then adapt them to suit your own use cases.


Use case

Assume you have a warehouse equipped with two temperature sensors:

  • Indoor Thermometer
  • Outdoor Thermometer

Your objective is to:

  • Collect telemetry from both devices
  • Calculate the absolute temperature difference between indoor and outdoor readings
  • Store the result as telemetry on the warehouse asset
  • Visualize and monitor this value in real time

This example demonstrates cross-entity data aggregation, where telemetry from multiple devices is processed at the asset level.


Prerequisites

Before proceeding, it is recommended to review the ThingsBoard Calculated fields documentation.

This topic provides the necessary foundation for understanding entity relationships and data processing mechanisms used in this example.


1. Provision asset and devices

In ThingsBoard, an asset is an abstract entity used to represent logical objects such as buildings, warehouses, or production lines.
In this example, the asset represents a warehouse and is used to store aggregated telemetry from multiple devices.

Create the asset:

  1. Navigate to Entities Assets.
  2. Click the + (Add) button in the top-right corner, select Add new asset and create:
     • Asset name: Warehouse A
     • Asset profile: warehouse

Create two devices:

  1. Download the CSV file containing the device configuration:
    thermometers_device_data.csv
  2. Navigate to Entities Devices.
  3. Click the + (Add) button in the top-right corner and select Import device.
  4. Upload the CSV file and follow the import wizard instructions.

CSV configuration details:

  • CSV delimiter: ,
  • Column mapping:
    • Name: Indoor Thermometer, Outdoor Thermometer
    • Type: thermometer

2. Emulate telemetry using Rule Engine

Since this example uses virtual devices, telemetry must be generated artificially.

Use Rule Engine generator nodes to periodically publish random temperature values.

For simplicity, the generators are added to the Root Rule Chain, although production setups should use a dedicated rule chain.

Indoor Thermometer emulator:

1
2
3
4
5
var temperature = toFixed(Math.random()*10 + 18, 2);
var msg = { temperature: temperature };
var metadata = { data: 40 };
var msgType = "POST_TELEMETRY_REQUEST";
return { msg: msg, metadata: metadata, msgType: msgType };

This generator simulates indoor temperatures in the range of approximately 18–28 °C.

Outdoor Thermometer emulator:

1
2
3
4
5
var temperature = toFixed(Math.random()*10 + 16, 2);
var msg = { temperature: temperature };
var metadata = { data: 40 };
var msgType = "POST_TELEMETRY_REQUEST";
return { msg: msg, metadata: metadata, msgType: msgType };

This generator simulates outdoor temperatures in the range of approximately 16–26 °C.

Verification

After the generators start running:

  1. Open Entities ⇾ Devices ⇾ Indoor Thermometer ⇾ Latest telemetry.
  2. Open Entities ⇾ Devices ⇾ Outdoor Thermometer ⇾ Latest telemetry.

You should see continuously updated temperature values.


3. Import a calculated field on the asset

Calculate the temperature difference between the two thermometers and store it as asset telemetry.

The calculated field is created on the Warehouse A asset.

Actions

  1. Download the calculated field configuration file:
    temperature_delta_based_on_2_devices_cf.json.
  2. Navigate to the Calculated fields page.
  3. Click the + (Add) button in the top-right corner and select Import calculated field .
  4. Upload the calculated field configuration file.
  5. Select the warehouse asset profile as the target entity so the calculated field is applied automatically to all relevant assets.
  6. Click Add to complete the import.

Calculation logic

1
abs(indoorTemperature - outdoorTemperature)

This calculates the absolute temperature difference regardless of which value is higher.

Once saved, the asset automatically receives updated deltaTemperature telemetry.


4. Verify the configuration

  1. Open Entities ⇾ Assets ⇾ Warehouse A ⇾ Latest telemetry.
  2. Confirm that the deltaTemperature key is present and updates in real time.

This confirms that:

  • Telemetry is received from both devices
  • The calculated field executes correctly
  • The result is stored on the asset

5. Visualize data using a dashboard (optional)

To monitor temperature differences visually:

The dashboard should display a real-time chart and table showing the temperature difference between the indoor and outdoor thermometers.


Next steps