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

I have made a driver that implements Virtual Energy & Power Device and power & energy calculation.
Power & energy calculation is implemented using Energy & Power Device(s) and supporting Rules API rules.


You will need the following hardware: SmartThings Hub (V2 or V3 or Station) or Aeotec Hub

Channel enrollment and driver installation

Enroll your hub into my Smartthings channel

Invitation link: Channel
Then install the Edge Driver called Virtual Energy Power Switch Device Tx.

Push ACCEPT button

Push Enroll button

Install Driver Available Drivers

Push Available Drivers button

Install Virtual Energy Power Switch Device Tx

Add first device to Smartthings

Open the SmartThings app then select Add device > Scan nearby.

Configure the device settings

Device features and operation

Using Set Energy is possible to set Energy Meter value.
Using Set Power is possible to set Power Meter value.

Using Create New Device Button is possible to add more Virtual Energy & Power Devices.

Total calculation will be implemented using Rules API rule.
Total calculation can be used for Total Energy or Total Power calculation.

Total Energy Calculation

Write using Rules API rule energy measuring device’s Energy Meter value to this device’s Add Value. Each write’s value is added to Total’s previous value ( Like Calculator’s M+ ).

Total Energy calculation Rules API rule’s actions (principle)

Write 1. device's energyMeter value to Add Value		-- Each write's value is added to Total
Sleep 3s
Write 2. device's energyMeter value to Add Value		-- Each write's value is added to Total
Sleep 3s
...
Write N. device's energyMeter value to Add Value		-- Each write's value is added to Total
Sleep 3s
Write Total value to Set Energy ( Energy meter )

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.

Dozens of energy measuring devices can be used in Total calculation.

Total Power Calculation

Write using Rules API rule power measuring device’s Power Meter value to Add Value. Each write’s value is added to Total’s previous value ( Like Calculator’s M+ ).

Total Power calculation Rules API rule’s actions (principle)

Write 1. device's powerMeter value to Add Value		-- Each write's value is added to Total
Sleep 3s
Write 2. device's powerMeter value to Add Value		-- Each write's value is added to Total
Sleep 3s
...
Write N. device's powerMeter value to Add Value		-- Each write's value is added to Total
Sleep 3s
Write Total value to Set Power ( Power meter )

Dozens of power measuring devices can be used in Total calculation.

If Setting Calculate Energy from Set Power is selected then from Set Power value is calculated Energy meter value using scaling.

Scaling Power to Energy calculation Setting:

Scaling depends on Rules API rule execution interval. Scaling idea is to get correct energy value when different rule execution intervals are used.
If rule is executed every 6 min use scaling 0.1 to get correct energy value.
If rule is executed every 3 min use scaling 0.05 to get correct energy value.

Using without any calculation

The Virtual Energy & Power Devices can also be used as virtual devices without any calculation.

The device’s switch does not affect the device’s operation. Only the Switch & Power Meter (Text) display changes according to the status of the switch.

Profiles (Dashboard Types)

Energy Meter

SmartThings Edge Driver Virtual Energy & Power Device  ST App Profile Energy

Power Meter

SmartThings Edge Driver Virtual Energy & Power Device  ST App Profile Power

Switch & Power Meter (Text)

SmartThings Edge Driver Virtual Energy & Power Device  ST App Dashboard Composite Small (State Off)

SmartThings Edge Driver Virtual Energy & Power Device  ST App Dashboard Composite Small (State On)

Switch Energy Power Meter

@Johnnybegoode thanks for the great help with the alpha testing earlier driver version.

@TAustin drivers in GitHub actually helped me a lot to understand how virtual devices (LAN) work.

I also find some Lua code examples from @Mariano_Colmenarejo drivers in GitHub.

7 Likes

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

Example Energy calculation from Power Meters values

Total Power and Energy Calculation

This example implements the summation of 4 power measurements into a Total Power (Total) and also from Total Power Energy Consumption calculation.

{
  "name": "4 x Power to 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 Power Measuring Device 1 --"
						],
						"component": "main",
						"capability": "powerMeter",
						"attribute": "power"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"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 Power Measuring Device 2 --"
						],
						"component": "main",
						"capability": "powerMeter",
						"attribute": "power"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"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 Power Measuring Device 3 --"
						],
						"component": "main",
						"capability": "powerMeter",
						"attribute": "power"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"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 Power Measuring Device 4 --"
						],
						"component": "main",
						"capability": "powerMeter",
						"attribute": "power"
					  }
					}
				  ]
				}
			  ]
			}
		  },
		  {
			"sleep": {
				"duration": {
					"value": {
						"integer": 3
					},
					"unit": "Second"
				}
			}
		  },
		  {
			"command": {
			  "devices": [
				"-- ID Energy-Power Device --"
			  ],
			  "commands": [
				{
				  "component": "main",
				  "capability": "islandtheme02751.setPower",
				  "command": "setPower",
				  "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 }]
				}
			  ]
			}
		  }	  
        ]
      }
    }
  ]
}

Using this Rules API rule it´s possible to turn virtual switch on every 6 minutes.

{
  "name": "Every 6 min Calculate Power -> Total Energy",
  "actions": [
      {
		"every": {
			"interval": {
				"value": {
					"integer": 6
				},
				"unit": "Minute"
			},
		  "actions": [
				{
					"command": {
						"devices": [
							"-- ID Virtual Switch Device --"
						],
						"commands": [
							{
								"component": "main",
								"capability": "switch",
								"command": "on"
							}
						]
					}
				}
		   ]
	    }
	   }
	]
}

It’s possible to add one action to end of main rule to turn virtual switch off.

		  {
			"command": {
				"devices": [
					"-- ID Virtual Switch Device --"
				],
				"commands": [
					{
						"component": "main",
						"capability": "switch",
						"command": "off"
					}
				]
			}
		  }

Other Rules API examples

This section will be added later

cool! Do these virtual devices work with SmartThings Energy?

No, but this driver is a poor man’s Total Energy calculation.

This driver can be used with energy / power measuring devices that do not work with SmartThings Energy. This driver can be used with existing devices and there is no need to buy new devices that work with SmartThings Energy.

2 Likes

The total Energy consumption counter is the simplest way to use this driver.
If you know the device’s fixed power consumption and the device’s On/Off status information is available to SmartThings,
you can make the total Energy consumption counter.

Use these settings

In my example fixed power consumption is 1000W.
Set your device’s fixed power consumption value to Total.

This Rules API rule calculates estimated total Energy consumption.

{
  "name": "Every 6 min Energy Consumption Calculation from Power",
  "actions": [
      {
		"every": {
			"interval": {
				"value": {
					"integer": 6
				},
				"unit": "Minute"
			},
		  "actions": [
				{
					"if": {
						"equals": {
							"left": {
								"device": {
									"devices": [
										"-- ID Monitored Device --"
									],
									"component": "main",
									"capability": "switch",
									"attribute": "switch",
									"trigger": "Always"
								}
							},
							"right": {
								"string": "on"
							}
						},
						"then": [
							  {
								"command": {
								  "devices": [
									"-- ID Energy-Power Device --"
								  ],
								  "commands": [
									{
									  "component": "main",
									  "capability": "islandtheme02751.setPower",
									  "command": "setPower",
									  "arguments": [
										{
										  "device": {
											"devices": [
											  "-- ID Energy-Power Device --"
											],
											"component": "main",
											"capability": "islandtheme02751.total",
											"attribute": "total"
										  }
										}
									  ]
									}
								  ]
								}
							  }
						]
					}
				}
		     ]
	      }
	   }
	]
}

When the device is on, the energy consumption according to the power is added to the total energy consumption.
When the device is off, the total energy consumption value remains unchanged.
The rule is executed every 6 minute. 6 minute execution interval corresponds to a scaling factor of 0.1

This Rules API rule calculates estimated total Energy consumption.
Use this rule if you are using AWA

      {
		"every": {
			"interval": {
				"value": {
					"integer": 6
				},
				"unit": "Minute"
			},
		  "actions": [
				{
					"if": {
						"equals": {
							"left": {
								"device": {
									"devices": [
										"-- ID Monitored Device --"
									],
									"component": "main",
									"capability": "switch",
									"attribute": "switch",
									"trigger": "Always"
								}
							},
							"right": {
								"string": "on"
							}
						},
						"then": [
							  {
								"command": {
								  "devices": [
									"-- ID Energy-Power Device --"
								  ],
								  "commands": [
									{
									  "component": "main",
									  "capability": "islandtheme02751.setPower",
									  "command": "setPower",
									  "arguments": [
										{
										  "device": {
											"devices": [
											  "-- ID Energy-Power Device --"
											],
											"component": "main",
											"capability": "islandtheme02751.total",
											"attribute": "total"
										  }
										}
									  ]
									}
								  ]
								}
							  }
						]
					}
				}
		     ]
	      }
	   }
2 Likes

I find the ST Energy app is well structured and visually attractive, but the limitation on devices makes it almost useless for controlling the total energy consumption of a home.
This driver by @TapioX lets me group any number of power devices, compatible or incompatible with ST Energy, to track energy consumption using API Rules. With a little thought, a series of groups, by zone, rooms or type can be created to map out the entire home energy consumption.
Then, with the same driver, in the case of devices that are more difficult to measure, e.g AC, we can also create individual virtual power devices that can be triggered by a simple routine, specifying the nominal set power on the device tile. These can be calculated in the same API Rule as a normal power device , but they must be bracketed with “IF switched on”.


I have here 3 groups, with 4 Aqara plugs (also controlled in ST Energy!), 2 AC’s and various Tuya and other outlets with another virtual device. The calculated value of my AC can be seen here in its group.
.
Using @Mariano_Colmenarejo 's NumberField & Calculator, plus his Virtual Calendar, I have a Rule that copies, at Saturady midnight, the energy consumption for each week to a different number field, and then resets, allowing use of the average calculation to show continually a rolling weekly average as seen here on the Energy Trackings on the left.

The sum of these weekly calculated values can be compared to the reading supplied by the electricity supplier (at least where I am we have here a simple web interface to compare). A final small variation could be compensated by creating another virtual power device, but as a “wildcard” value that could be entered manually and so can modified to make the values coincide with the electricity companies’ feed

Trying out now using Node-Red to have Alexa announce daily energy meters.

2 Likes

I added a new Dashboard selection to Driver Version 2024-08-31T15:50:15.551416591

Maybe at some point we’ll get more icons.

Now it is possible to make, for example, a dashboard like this.

@Andreas_Roedl Your wish has come true! Thanks for the tip!

4 Likes

Awesome! Thanks!

2 Likes