Calculate kWh out of item with kW on irregular basis

Hi experts,

I was reading a lot but can´t find a solution for my use case.

I want to have the kWh from my smart meter (huawei sun2000) item which gets updated every 5 seconds. In first step it´s the item Huawei_Grid = energy I have to buy from my provider.

The item is persisted in my influxdb (version XX?) and with the following script I get the following result:

from(bucket: "openhab_db")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Huawei_Grid")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

In InfluxDB Web I get the following result:

So in this view I´m getting a value every 10 seconds.
In my thing definition for the modbus update interval I entered the value 5000ms.

Bridge poller slavepowermeter [ start=37113, length=2, refresh=5000, type="holding" ]{
        Thing data Powermeter                        [ readStart="37113", readValueType="int32",updateUnchangedValuesEveryMillis="5000000" ]

So how on earth do I calculate the correct kWh value for this item?
Can I do it directly in Openhab or do I have so move the logic to InfluxDB?

Many thanks in advance!

From what I understand you face a similar situation than me. Here‘s how I solved it.

1 Like

Thanks for your reply I will have a look into.

Fun fact: I asked Chat GPT to help me calculate my watt item to kwH item and I got a pretty good answer? What do you think?

From what I can see this does not bring you any further.

Yes you are right. Chat GPT is having problems with different types of Items and calculating them.

I did set up your script but we have different situation: you are using cumulative Power Item as my Item is showing the current Watt consumption.

Any idea how to use your script with non cumulated watt item?

Thank you!

You could write a rule that…

  • is triggered every time your “current Watt consumption” changes
  • that then calculates the time between this update and the previous update
  • and that that then calculates the Watts over that tim, to come up with a accumulated watt item.

Haven’t done this myself, but should be pretty straight forward.

Okay I created my cumulated Watt Item now by just filling up the values in between the Huawei_Grid updates from persistence with previousState.
This item gets reset at midnight every day.

rule "energy"
Time cron "* * * * * ?"
Huawei_Grid_cumulated.postUpdate(Huawei_Grid_cumulated.state as Number + Huawei_Grid.previousState.state as Number)

I don´t like to spam my openhab with a Time cron job every second so I´m trying to find a better solution and give your suggestion a try. Anyhow the kwH values we´re pretty accurate after a whole day. I checked it against my Fusion Solar app of my inverter.

I´m struggeling with getting a timestamp from my Huawei_Grid item and compare it with the next timestamp update from the same item.
How to do this? I didn´t find any solutions for me that fits…this is the code i´m having so far (I know it´s not much…):

rule "calculate energy"


Item Huawei_Grid changed 


var update = Huawei_Grid.lastUpdate()


How to get two lastUpdates status from the same item and how to calculate with TimeDate variables?

Thanks a lot

There is no kWh-reading from your smart meter available? that would be the goto-version. Converting kW/interval is prone for errors like if you miss a beat or similar you won’t get the exact kWh out of it. If you’ve really only got kW-readings, you can go the linked route.

That’s one issue, if you only have kW delivered. You can either assume there’s a strict 5second interval and just use the delta between two item states and hope for the best.
or you can read out the timestamps like this and still hope for the
or this:

Thanks for support.

Yes, I only have Watt Meter reading out of my DTSU666 via modbus and no kWH calculated. The kwH value is only available in the Fusion solar app from Huawei.

Modbus gets the updates every ~ 5 seconds. I can decrease interval but I´m not sure if its running safe then.
So I´m looking for a way to calculate it somehow in the best accurate way.