Skip to content
Stand with Ukraine flag

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.

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.

Review the Calculated fields documentation before proceeding — it provides the necessary foundation for understanding entity relationships and data processing mechanisms used in this guide.

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:

  1. Navigate to Entities ⇾ Assets.
  2. Click + Add asset ⇾ Add new asset, and set:
    • Asset name: Warehouse A
    • Asset profile: warehouse
  3. Click Add.

Create two devices:

  1. Download the CSV file: thermometers_device_data.csv
  2. Navigate to Entities ⇾ Devices.
  3. Click + Add device ⇾ Import device.
  4. 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.

  1. Navigate to Rule chains ⇾ Root Rule Chain.

  2. Drag and drop a Generator node onto the canvas.

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

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

  5. Connect both Generator nodes to the Entity type filter node using the Success relation.

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

  1. Download the configuration file: temperature_delta_based_on_2_devices_cf.json
  2. Navigate to Calculated fields.
  3. Click + Add calculated field ⇾ Import calculated field.
  4. Upload the downloaded file.
  5. Select the warehouse asset profile as the target entity so the calculated field applies to all relevant assets.
  6. 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
  7. Click Add.
abs(indoorTemperature - outdoorTemperature)

This calculates the absolute temperature difference regardless of which value is higher. Once saved, the asset automatically receives updated deltaTemperature telemetry.

  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, 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:

  1. Download the prepared dashboard: warehouse_dashboard.json
  2. Import it via Dashboards ⇾ Import dashboard.
  3. 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.