Star

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 rule chain

image

             var msg = {
             	temperature: (20 + 5 * Math.random()).toFixed(1)
             };
             
             return {
             	msg: msg,
             	metadata: {
             		deviceType: "indoor thermometer"
             	},
             	msgType: "POST_TELEMETRY_REQUEST"
             };
             
             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.


Root rule chain

image


Delta Temperature rule chain

image

        function nextRelation(metadata, msg) {
        	if (metadata.deviceType === 'indoor thermometer') {
        		return ['indoor'];
        	} else if (metadata.deviceType === 'outdoor thermometer')
        		return ['outdoor'];
        }
        
        return nextRelation(metadata, msg);
        

image

         var newMsg = {};
               
         newMsg.outdoorTemperature = msg.temperature;
               
         return {
            msg: newMsg,
           	metadata: metadata,
           	msgType: msgType
         };
         
         var newMsg = {};
          
         newMsg.indoorTemperature = msg.temperature;
          
         return {
          	msg: newMsg,
          	metadata: metadata,
          	msgType: msgType
         };
         

image

image

        var newMsg = {};
        
        newMsg.deltaTemperature = parseFloat(Math.abs(metadata.indoorTemperature - metadata.outdoorTemperature).toFixed(2));
        
        return {
        	msg: newMsg,
        	metadata: metadata,
        	msgType: msgType
        };
        

image


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