Power calculation from meter reading

Hi, I try to calculate the watt from my energy meter. I use NodeREDs smartmeter input plugin to get the values from my counter. The values are read every second. My function looks like this, but I know I miss something:

var counter_out=msg.payload["1-0:2.8.0*255"].values[0].value // in KWh
var counter_in=msg.payload["1-0:1.8.0*255"].values[0].value // in KWh

var now=new Date().getTime()

if (flow.get("last_changed"))
{

    var last_changed=flow.get("last_changed")
    var TimeDiffMs = now - last_changed
    var TimeDiff = TimeDiffMs / 1000
    var TimeDiffM = TimeDiff / 60
    var TimeDiffH = TimeDiff / 60 / 60

    flow.set("TimeDiff", TimeDiff)
    flow.set("TimeDiffM", TimeDiffM)
    flow.set("TimeDiffH", TimeDiffH)

    // Wh = W x Time
    // W = Wh / Time
    // counter_in / 1000 to get from KWh to Wh

    var power_in = (counter_in / 1000) / TimeDiffH
    flow.set("power_in", power_in)
    flow.set("power_in_real", msg.payload["1-0:16.7.0*255"].values[0].value)

    var power_out = (counter_out / 1000) / TimeDiffH 
    flow.set("power_out", power_out)
}

flow.set("counter_out", counter_out)
flow.set("counter_in", counter_in)
flow.set("last_changed", now)

return null

I primary want to calculate the power_out, to calculate a limit for my inverter.

I get the following results:

var val
TimeDiff 1.015
TimeDiffH 0.0002819444444444444
TimeDiffM 0.016916666666666667
counter_in 26021.4767
counter_out 7.3337
current_power 682
last_changed 1697384125268
power_in 92292.9222857143
power_in_real 682
power_out 26.01115270935961

Any math gurus who can help so solve my problem???

looks like it is to hard to solve…

I looked at this when you first posted but could not work out what you were asking.

A second look this morning and I might begin to see what is going on, and the issue…

Energy (as measured by utility meters) is normally kWh, which is the summed product of instantaneous power by a time period, usually worked out by integration (see the HA Reimann sum integration).

Integrate Power (over time) → Energy

The reverse is differentiation (derivative), so it would be possible to use the HA derivative integration as
Derivative (Energy) by time → Power.

I believe that you are trying to do this by hand, and thus the instantaneous slope of the energy curve is given by

Power = delta (energy) / delta (time)

In looking at your code I note that you capture the time difference between readings by storing the prior ‘last changed’ timestamp and comparing to the current ‘now’. This certainly gives you the (approximate) 1 second result we would expect for a 1 second repeating measurement.

The problem I am having is seeing where you are working out the energy difference between these two times. The ‘energy in’ (I assume that you have two utility meter readings, one for import energy, one for export energy) reading is the total energy (since the meter started) and hence you need to calculate the difference between the meter reading at the last reading and this current reading.

Your formula is correct W = Wh / h, except for the fact that you need the Wh difference over the time difference.

I would point out that HA keeps records in history, and getting any sensor value from history gives this current state, as well as the last state value and last changed time. This makes it very easy to get both the time difference and the state value difference between two consecutive readings.
This is the basis for the HA derivative integration.

https://www.home-assistant.io/integrations/derivative/

I have had mixed success with this, but it does work by sampling readings and calculating the difference over a period of time. The shorter the ‘sample’ period, the closer the result is to the instantaneous power, but the more sensitive the result is to changes. You may find this easier to use.