Stand with Ukraine flag
Try it now Pricing
Community Edition
Data function based on telemetry from 2 devices
Getting Started Documentation Devices Library Guides Installation Architecture API FAQ
On this page

Data function based on telemetry from 2 devices

Before proceeding, we recommend reviewing the Getting Started guide to become familiar with the basics of ThingsBoard. Additionally, it's useful to read the documentation on calculated fields.

This tutorial demonstrates how to calculate the temperature delta based on readings from indoor and outdoor thermometers in a warehouse.

Asset & devices provisioning

First, create a new asset and a corresponding asset profile for it. Name the asset Warehouse A, and the profile — warehouse.

Now create two devices named Indoor Thermometer and Outdoor Thermometer. Create a device profile called thermometer and assign it to these devices.

Create relationships between the Warehouse A asset and the Indoor Thermometer and Outdoor Thermometer devices.

Note: Please review the following documentation page to learn how to create relationships between entities.

Thermometer emulators node

Since we are using virtual devices, they do not send telemetry data to the ThingsBoard. However, we can simulate the transmission of such data in real time. To do this, we will use Rule Engine.

Let's add two generator nodes that will periodically produce messages with random temperature readings. Ideally, such use cases should have a dedicated Rule Chain, but for simplicity, we'll use the Root Rule Chain. Route the messages from these nodes to the device profile node.

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 };

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 };

After waiting for the period specified in the generator nodes, you will be able to see the telemetry on the “Latest telemetry” tab of your devices.

Create calculated field

The Calculated fields feature allows users to perform real-time calculations based on telemetry data and/or attributes. You can learn more about calculated fields here.

As part of our example, add a calculated field to the Warehouse A resource that calculates the difference between the temperature values of the two thermometers and stores the result as a new telemetry value with the key deltaTemperature.

  • Go back to еру asset Warehouse A, open its details, and navigate to the “Calculated fields” tab.
  • Click the “plus” icon button and select “Create new calculated field” from the dropdown menu.


General. The calculated field configuration window will open.

  • Enter a descriptive title for the calculated field.
  • Select the “Simple” calculated field type. This allows you to perform basic mathematical operations on the provided arguments.

[Optionally]. Use the Debug mode to track calculated field events, such as state changes and errors, for easier debugging and troubleshooting.


Arguments. Now you need to add two arguments: indoorTemperature and outdoorTemperature.

In the “Arguments” section, add the first argument:

  • Click the “Add argument” button.
  • Enter indoorTemperature as the name of the first argument.
  • Choose device Indoor Thermometer as the entity.
  • Leave the argument type set to “Latest telemetry”.
  • Set “temperature” as the time series key.
  • Click “Add”.


Add another argument:

  • Click “Add argument” button again.
  • Name it “outdoorTemperature”.
  • Choose device Outdoor Thermometer as the entity.
  • Leave the argument type set to “Latest telemetry”.
  • Set “temperature” as the time series key.
  • Finally, click “Add” button.


Expression. Now, enter the mathematical expression for the calculation using the variables defined in the “Arguments” section.

Copy this script and paste it to the function calculation window:

1
abs(indoorTemperature - outdoorTemperature)


Output. The calculated value is returned as a JSON object containing a key that represents the computed result. This key, along with its value, is then stored in the system.

  • Set the output type as “Time series” to store the calculation result as time series data.
  • Assign deltaTemperature as the name of the variable that will store the calculation result.
  • Optionally, set decimals by default to define how many decimal places the result should be rounded to. If not specified, the result will not be rounded.
  • To finish adding the calculated field, click “Add”.

The calculated field has been successfully added to your device.

If needed, you can download the JSON file with the calculated field configuration described above and import it into your instance.

Check configurations

To verify that your configuration is working correctly, go to the “Latest telemetry” tab of the Warehouse A asset. If everything is set up properly, you should see the deltaTemperature key and its value.


To monitor the temperature difference in real time, download and import this dashboard prepared specifically for this example.

After that, the dashboard should display the temperature delta data between the two thermometers of the Warehouse A asset.