Adding Energy Monitoring to a device that does not have it

Hi all!

I'm trying to add the Energy attribute to devices that offer power monitoring to get an idea of what their total consumption was.

So far my idea for the approach was to store the previously reported power and when it changes, use the previous value and the time difference to set the added energy. Then add a RM like this:

I've put this into a driver and am now testing, this is my code so far (pardon the newbieness):

Any feedback, am I doing this right, is there a better way ?

1 Like

Yes, this approach works! I am doing the same for my Tuya Metering Plug driver, although this option is not quite ready yet.

It may need some more complicated logic. Sample scenario: there was a constant power consumption of 1000 watts for 60 minutes, then the plug is switched off and it’s last report is 0 watts. The expected Energy consumption calculation is 1 kW/h

Hi @kkossev ! Love your Tuya driver as I use it myself at home and at the office :smiley:

The scenario you suggested should be covered as well, see this piece of code:

def updateEnergy(float power) {
    r = (now() - device.currentValue("lastPowerUpdate")) / 1000
    p = device.currentValue("lastPowerValue")
    energy = device.currentValue("energyExact")
    newEnergy = (energy + (p/1000/60/60*r))
    sendEvent(name: "energyExact", value: newEnergy)
    sendEvent(name: "energy", value: Math.round(newEnergy*100)/100)
    sendEvent(name: "lastPowerValue", value: power)
    sendEvent(name: "lastPowerUpdate", value: now())

It uses the previously reported value to calculate, and updates it for the next report.

The only hiccup I have discovered so far is that I need power reports to come in more often for the calculation to be precise, but it would spam the network and I don't want that.

Still testing to see if anything better comes to mind, maybe I can use an App to handle this instead of the RM part. WIP...

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.