Community Edition
ThingsBoard Documentation
Cloud Professional Edition Community Edition Edge IoT Gateway License Server Trendz Analytics
Try it now Pricing
Data function based on telemetry from 2 devices
Getting Started Documentation Guides Installation Architecture API FAQ

On this page

Data function based on telemetry from 2 devices

This tutorial will show how to calculate temperature delta based on the readings from the indoor outdoor warehouse thermometers.

Use case

Let’s assume you have a warehouse with two thermometers: indoor and outdoor. In this tutorial, we will configure ThingsBoard Rule Engine to automatically calculate the delta of temperatures inside and outside the warehouse based on the latest readings from temperature sensors. Please note that this is just a simple theoretical use case to demonstrate the capabilities of the platform. You can use this tutorial as a basis for much more complex scenarios.

Prerequisites

We assume you have completed the following guides and reviewed the articles listed below:

Model definition

We will create one asset that has name “Warehouse A” and type “warehouse”.

image

We will create two devices that have names “Inside Thermometer” and “Outside Thermometer” and accordingly with types “inside thermometer” and “outside thermometer”.

image image

We must also create the relation between asset “Warehouse A” and device “Inside Thermometer”. This relation will be used in the rule chain to change originator of the messages from the thermometer to the warehouse itself and also the relation from device “Inside Thermometer” to device “Outside Thermometer” to fetch the latest temperature from “Outside Thermometer”.

image


Note: Please review the following documentation page to learn how to create assets and relations.

Message Flow

In this section, we explain the purpose of each node in this tutorial. There will be three rule chains involved:

  • “Thermometer Emulators” - optional rule chain to simulate data from two temperature sensors;

  • “Root rule chain” - rule chain that actually saves telemetry from devices into the database, and filters messages by device type before redirecting it to “Delta Temperature” chain

  • “Delta Temperature” - rule chain that actually calculates delta temperature between thermometers in the warehouse and outside;

Thermometer Emulators rule chain

image

  • Nodes A and B: Generator nodes

    • Two similar nodes that periodically generate a very simple message with random temperature reading.
  • Node A: Indoor Thermometer emulator

1
2
3
4
5
6
7
8
9
10
11
12
             var msg = {
             	temperature: (20 + 5 * Math.random()).toFixed(1)
             };
             
             return {
             	msg: msg,
             	metadata: {
             		deviceType: "indoor thermometer"
             	},
             	msgType: "POST_TELEMETRY_REQUEST"
             };
             
  • Node B: Outdoor Thermometer emulator
1
2
3
4
5
6
7
8
9
10
11
12
             var msg = {
             	temperature: (18 + 5 * Math.random()).toFixed(1)
             };
             
             return {
             	msg: msg,
             	metadata: {
             		deviceType: "outdoor thermometer"
             	},
             	msgType: "POST_TELEMETRY_REQUEST"
             };
             

image

image


Note: in the real case, the device type is set to the message metadata by default.

  • Node C: Rule Chain node

    • Forwards all messages to default root rule chain.


Root rule chain

image

  • Nodes D: Rule Chain node

    • Forwards incoming Message to specified rule chain “Delta Temperature”.


Delta Temperature rule chain

image

  • Node E: Switch node.

    • Routes incoming messages by deviceType fetched from message metadata. If deviceType from incoming message is “indoor thermometer” switches to the chain via “indoor” relation type, else if deviceType from incoming message is “outdoor thermometer” switches to the chain via “outdoor” relation type.
1
2
3
4
5
6
7
8
9
        function nextRelation(metadata, msg) {
        	if (metadata.deviceType === 'indoor thermometer') {
        		return ['indoor'];
        	} else if (metadata.deviceType === 'outdoor thermometer')
        		return ['outdoor'];
        }
        
        return nextRelation(metadata, msg);
        

image

  • Nodes F and G: Transform script nodes

    • Two similar nodes that changes key names from message payload from “temperature” to “indoorTemperature” or “outdoorTemperature” depending on relation type from the previous node.

    • Creates a new outbound message in which it puts the new telemetry.

  • Node F: Change to Outdoor

1
2
3
4
5
6
7
8
9
10
         var newMsg = {};
               
         newMsg.outdoorTemperature = msg.temperature;
               
         return {
            msg: newMsg,
           	metadata: metadata,
           	msgType: msgType
         };
         
  • Node G: Change to Indoor
1
2
3
4
5
6
7
8
9
10
         var newMsg = {};
          
         newMsg.indoorTemperature = msg.temperature;
          
         return {
          	msg: newMsg,
          	metadata: metadata,
          	msgType: msgType
         };
         
  • Node H: Change originator node.

    • Changes the originator from “Inside Thermometer” to the related Asset “Warehouse A” and the submitted message will be processed as a message from the Asset.

image

  • Node I: Save Timeseries node.

    • Saves the TimeSeries data from the incoming Message payload into the database.
  • Node J: Originator attributes node.

    • Adds message originator latest telemetry values into message metadata.

image

  • Node K: Transform script node.

    • Creates a new outbound message in which it puts the new telemetry “deltaTemperature” that calculated like the absolute value between the difference of message metadata telemetry values namely, “indoorTemperature” and “outdoorTemperature”.
1
2
3
4
5
6
7
8
9
10
        var newMsg = {};
        
        newMsg.deltaTemperature = parseFloat(Math.abs(metadata.indoorTemperature - metadata.outdoorTemperature).toFixed(2));
        
        return {
        	msg: newMsg,
        	metadata: metadata,
        	msgType: msgType
        };
        

image

  • Node L: Save Timeseries node.

    • Saves the TimeSeries data from the incoming Message payload into the database.


Configuring the Rule Chains

Download and import attached emulators rule chain file as a new “Thermometer Emulators” rule chain, root rule chain file as a new “Root rule chain” and “Delta Temperature” file. Please note that some nodes have debug enabled.

Validating the flow

Download and import attached dashboard file as a new “Warehouse dashboard”.

image

Next steps