[ST Edge] Virtual Energy & Power Device Driver for calculating Total Energy Consumption

Example Energy calculation from Energy Meters values

Total Energy Calculation

This example implements the summation of 4 energy measurements into a Total Energy (Total) and from Total Energy to Energy Consumption conversion.

{
  "name": "4 x Energy using Energy Device Tx",
  "actions": [
    {
      "if": {
        "equals": {
          "right": {
            "device": {
              "devices": [
                "-- ID Virtual Switch Device --"
              ],
              "component": "main",
              "capability": "switch",
              "attribute": "switch"
            }
          },
          "left": {
            "string": "on"
          }
        },
        "then": [
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 1 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 2 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 3 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 4 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
		  {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.setEnergy",
				  "command": "setEnergy",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy-Power Device --"
						],
						"component": "main",
						"capability": "islandtheme02751.total",
						"attribute": "total"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
				"sleep": {
					"duration": {
						"value": {
							"integer": 20
						},
						"unit": "Second"
					}
				}
		  },
		  {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [{"decimal": 0.0 }]
				}
			  ]
			}
		  },
		  {
				"sleep": {
					"duration": {
						"value": {
							"integer": 2
						},
						"unit": "Second"
					}
				}
		  },
		  {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.total",
				  "command": "setTotal",
				  "arguments": [{"decimal": 0.0 }]
				}
			  ]
			}
		  }	  
        ]
      }
    }
  ]
}

Total Energy calculation is triggered using virtual switch. Total Energy calculation testing is easy when rule is triggered using virtual switch.

The purpose of sleep 3s is simply to spread the rule’s actions over a longer period of time. The aim is that the performance of the routine has as little impact as possible on the rest of the hub’s operations.

Rules API rule in AWA Rules section

Rules API rules can be added using AWA

Add only actions to rule (to yellow marked area) when you are using AWA Add a Rule

    {
      "if": {
        "equals": {
          "right": {
            "device": {
              "devices": [
                "-- ID Virtual Switch Device --"
              ],
              "component": "main",
              "capability": "switch",
              "attribute": "switch"
            }
          },
          "left": {
            "string": "on"
          }
        },
        "then": [
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 1 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 2 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 3 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
          {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy Measuring Device 4 --"
						],
						"component": "main",
						"capability": "energyMeter",
						"attribute": "energy"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
		  {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.setEnergy",
				  "command": "setEnergy",
				  "arguments": [
					{
					  "device": {
						"devices": [
						  "-- ID Energy-Power Device --"
						],
						"component": "main",
						"capability": "islandtheme02751.total",
						"attribute": "total"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
				"sleep": {
					"duration": {
						"value": {
							"integer": 20
						},
						"unit": "Second"
					}
				}
		  },
		  {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.addValue",
				  "command": "setAddValue",
				  "arguments": [{"decimal": 0.0 }]
				}
			  ]
			}
		  },
		  {
				"sleep": {
					"duration": {
						"value": {
							"integer": 2
						},
						"unit": "Second"
					}
				}
		  },
		  {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.total",
				  "command": "setTotal",
				  "arguments": [{"decimal": 0.0 }]
				}
			  ]
			}
		  }	  
        ]
      }
    }
2 Likes