Function Based on Telemetry from Two Devices
Calculate the temperature difference between telemetry from two separate devices and store the result as asset telemetry. This guide uses importable configurations so you can examine the logic and adapt it to your own scenario.
Use case
Section titled “Use case”Assume you have a warehouse equipped with two temperature sensors:
- Indoor Thermometer — measures temperature inside the warehouse.
- Outdoor Thermometer — measures temperature outside the warehouse.
Your objective is to:
- Collect telemetry from both devices.
- Calculate the absolute temperature difference between the 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
Section titled “Prerequisites”Review the Calculated fields documentation before proceeding — it provides the necessary foundation for understanding entity relationships and data processing mechanisms used in this guide.
Step 1. Provision asset and devices
Section titled “Step 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 stores aggregated telemetry from multiple devices.
Create the asset:
- Navigate to Entities ⇾ Assets.
- Click + Add asset ⇾ Add new asset, and set:
- Asset name:
Warehouse A - Asset profile:
warehouse
- Asset name:
- Click Add.
Create two devices:
- Download the CSV file: thermometers_device_data.csv
- Navigate to Entities ⇾ Devices.
- Click + Add device ⇾ Import device.
- Upload the CSV file and follow the import wizard instructions.
CSV configuration:
- Delimiter:
, - Columns: Name (
Indoor Thermometer,Outdoor Thermometer), Type (thermometer)
Step 2. Emulate telemetry using Rule Engine
Section titled “Step 2. Emulate telemetry using Rule Engine”Since this example uses virtual devices, telemetry must be generated artificially using the Rule Engine.
-
Navigate to Rule chains ⇾ Root Rule Chain.
-
Drag and drop a Generator node onto the canvas.
-
Select Indoor Thermometer as the originator and replace the generator function with the following script:
var indoorTemperature = toFixed(Math.random()*10 + 18, 2);var msg = { indoorTemperature: indoorTemperature };var metadata = { data: 40 };var msgType = "POST_TELEMETRY_REQUEST";return { msg: msg, metadata: metadata, msgType: msgType };This simulates indoor temperatures in the range of approximately 18–28 °C.
-
Add a second Generator node for Outdoor Thermometer and use the following script:
var outdoorTemperature = toFixed(Math.random()*10 + 16, 2);var msg = { outdoorTemperature: outdoorTemperature };var metadata = { data: 40 };var msgType = "POST_TELEMETRY_REQUEST";return { msg: msg, metadata: metadata, msgType: msgType };This simulates outdoor temperatures in the range of approximately 16–26 °C.
-
Connect both Generator nodes to the Entity type filter node using the Success relation.
-
Apply changes.
To verify, open Latest telemetry for each device — you should see continuously updated temperature values.
Step 3. Import a calculated field on the asset
Section titled “Step 3. Import a calculated field on the asset”The calculated field reads the latest telemetry from both thermometers and computes the absolute temperature difference, storing it as a new telemetry key on the asset.
- Download the configuration file: temperature_delta_based_on_2_devices_cf.json
- Navigate to Calculated fields.
- Click + Add calculated field ⇾ Import calculated field.
- Upload the downloaded file.
- Select the
warehouseasset profile as the target entity so the calculated field applies to all relevant assets. - Update the calculated field arguments:
- First argument: Entity type —
Device, Device —Indoor Thermometer, Argument type — Latest telemetry, Key —indoorTemperature - Second argument: Entity type —
Device, Device —Outdoor Thermometer, Argument type — Latest telemetry, Key —outdoorTemperature
- First argument: Entity type —
- Click Add.
Calculation logic
Section titled “Calculation logic”abs(indoorTemperature - outdoorTemperature)This calculates the absolute temperature difference regardless of which value is higher. Once saved, the asset automatically receives updated deltaTemperature telemetry.
Step 4. Verify the configuration
Section titled “Step 4. Verify the configuration”- Open Entities ⇾ Assets ⇾ Warehouse A ⇾ Latest telemetry.
- Confirm that the
deltaTemperaturekey is present and updates in real time.
This confirms that telemetry is received from both devices, the calculated field executes correctly, and the result is stored on the asset.
Step 5. Visualize data using a dashboard (optional)
Section titled “Step 5. Visualize data using a dashboard (optional)”To monitor temperature differences visually:
- Download the prepared dashboard: warehouse_dashboard.json
- Import it via Dashboards ⇾ Import dashboard.
- After the import, update the entity alias by setting Warehouse A as the target entity.
The dashboard displays a real-time chart and table showing the temperature difference between the indoor and outdoor thermometers.
See also
Section titled “See also”- Calculated fields — full configuration reference
- Telemetry delta calculation — calculate delta within a rolling time window for a single device