Aplicaciones Virtuales Mc driver Applications: Number Fields and Basic Calculations

Rule to calculate the difference in temperatures. The result of the calculation is always positive ( The abs() (absolute value) function returns the non-negative magnitude of a number, regardless of its sign).

{
  "name": "Temperature measurements Changes > Calculate ABS T difference",
  "actions": [
	   {
		 "if": {
		  "or": [
		  {
			  "changes": {
				"operand": {
				  "device": {
					"devices": [  "-- ID Temperature Measurement Device 1 --" ],
					"component": "main",
					"capability": "temperatureMeasurement",
					"attribute": "temperature",
					"trigger": "Always"
				  }
				}
			  }
		  },
		  {
			  "changes": {
				"operand": {
				  "device": {
					"devices": [ "-- ID Temperature Measurement Device 2 --" ],
					"component": "main",
					"capability": "temperatureMeasurement",
					"attribute": "temperature",
					"trigger": "Always"
				  }
				}
			  }
		  }
		  ],
		   "then": [
		   		{
					"command": {
						"devices": ["-- ID Number Field Device --"],
					"commands": [
						{
							"component": "main",
							"capability": "legendabsolute60149.textFormula",
							"command": "setTextFormula",
							"arguments": [{"string": "-"}]
						}
					  ]
					}
				},
				{
					"sleep": {
						"duration": {
							"value": {
								"decimal": 2.0
							},
							"unit": "second"
						}
					}
				},			  
				{
					"command": {
						"devices": [ "-- ID Number Field Device --" ],
					"commands": [
						{
						  "component": "main",
						  "capability": "legendabsolute60149.numberFieldOne",
						  "command": "setNumberFieldOne",
						  "arguments": [
							{
							  "device": {
								"devices": [ "-- ID Temperature Measurement Device 1 --" ],
								"component": "main",
								"capability": "temperatureMeasurement",
								"attribute": "temperature"
							  }
							}
						  ]
						}					
				  	]
				  }
				},
				{
					"sleep": {
						"duration": {
							"value": {
								"decimal": 1.0
							},
							"unit": "second"
						}
					}
				},
				{
					"command": {
						"devices": [ "-- ID Number Field Device --" ],
					"commands": [
						{
						  "component": "main",
						  "capability": "legendabsolute60149.numberFieldTwo",
						  "command": "setNumberFieldTwo",
						  "arguments": [
							{
							  "device": {
								"devices": [ "-- ID Temperature Measurement Device 2 --" ],
								"component": "main",
								"capability": "temperatureMeasurement",
								"attribute": "temperature"
							  }
							}
						  ]
						}					
				  	]
				  }
				},
				{
					"sleep": {
						"duration": {
							"value": {
								"decimal": 1.0
							},
							"unit": "second"
						}
					}
				},
				{
				"if": {
				  "greaterThan": {
					"left": {
					"device": {
					  "devices": [
						"-- ID Temperature Measurement Device 1 --"
						],
						"component": "main",
						"capability": "temperatureMeasurement",
						"attribute": "temperature"
						}
					},
					"right": {
					"device": {
					  "devices": [
						"-- ID Temperature Measurement Device 2 --"
						],
						"component": "main",
						"capability": "temperatureMeasurement",
						"attribute": "temperature"
						}			
					}
				},			
				"then": [
					{
					"command": {
						"devices": ["-- ID Number Field Device --"],
					  "commands": [
						{
							"component": "main",
							"capability": "legendabsolute60149.textFormula",
							"command": "setTextFormula",
							"arguments": [{"string": "1-2"}]
						}
					  ]
					 }
					}
				 ],
				"else": [
					{
					"command": {
						"devices": ["-- ID Number Field Device --"],
					"commands": [
						{
							"component": "main",
							"capability": "legendabsolute60149.textFormula",
							"command": "setTextFormula",
							"arguments": [{"string": "2-1"}]
						}
					  ]
					 }
					}
				 ]
			   }
			  }
		   ]
		 }
		}
	]
}

The implementation is based on choosing a calculation formula depending on the sign of the difference between the measurement values.

Example calculation (App Number Field Device’s History)