Summary of powerconsumption in openhab

Hi there!

I am trying to figure out how to count my powerconsumption of my heating pump.

I have installed a subcounter in front of my heatingpump which is sending via an S0 impulses to my Wago SPS.
For 1kwh it sends 500 impulses.
I read via Modbus every 30 seconds from my SPS the actual value of the powerconsumption and write it into an mysql database.
So i see how much energy the heatingpump is actually consuming.

Is there a way to count this values up anyhow?

I searched a lot but i can´t find a hint how to do this.

Thank you a lot and greetings!

Persistence service provides sumSince() and other functions that you can use in a rule e.g.

var today = yourItem.sumSince(now().with(LocalTime.MIDNIGHT))

But what you really want to do will depend on what your Item represents and how you recorded it… kW per hour? Instantaneous kW every ten minutes? Ever-increasing kWh counter?


i am using the jdbc persistence addon for writing data in to the mysql database.
Can i use this also with this addon?

I am getting the values what the heatingpump is consuming at the moment… no increasing counter :frowning:

I store the timestamp and the value every 60 seconds.

Okay, so you are storing like 30W each minute, say.

So a rule that runs each hour can add up all the records for the previous hour, divide by 60, and the result is average Wh for that hour.

A rule that runs at 23:55 can add up all records for the day using

var today = yourItem.sumSince(now().with(LocalTime.MIDNIGHT,"jdbc"))

divide by 60, divide by 1000, and give you daily kWh, which you can update to another Item and persist for charts etc.

These are just ideas depending on what you want. There are examples in this forum.

1 Like

yes… something like this… when the heating pump is not heating the warmwater or the house, then i have round about 120w for only the circulating pump.
When it starts heating, than the power consumption encreasing to round about 1500w and when finished than it will be going back to 120w.

I already searched in the forum and also google, but nothing leads mit to the solution… all topic i found have some different requirements then mine.

I added now an item:

Number Heatpump_total_kwh_daily    "Waermepumpe daily"

and a rule like this for testing:

rule "Daily Power Consumption"
    Item Waermepumpe_HT received update
    Heatpump_total_kwh_daily.postUpdate((Waermepumpe_HT.sumSince(now.minusDays(1), "jdbc").toString))
    val summary = Heatpump_total_kwh_daily.state as Number
    val kilowatt = Heatpump_total_kwh_daily.state as Number

i have to check if the sum value is correct or not and also when i would like to store the value to the mysql i will overwrite the values several times…

2021-02-11 14:36:57.830 [vent.ItemStateChangedEvent] - Heatpump_total_kwh_daily changed from 9495.51174316 to 569.73070459
2021-02-11 14:37:57.963 [vent.ItemStateChangedEvent] - Heatpump_total_kwh_daily changed from 569.73070459 to 570201.50820922851550
2021-02-11 14:37:57.969 [vent.ItemStateChangedEvent] - Heatpump_total_kwh_daily changed from 570201.50820922851550 to 9.49551174

That will not work properly.
postUpdate is asynchronous, the request is sent off to the Item but the rule does not stop and wait for it to take effect.
If you read the Item state in the next line you will most likely get the old value.

You don’t need to read the state, you already know what you just sent, use that.

val summary = Waermepumpe_HT.sumSince(now.minusDays(1), "jdbc")

Yes you are right!
i see in the logs that is are not consistent and mixed up often…

But now i think i get it… is it that simple?

rule "Daily Power Consumption"
    Item Waermepumpe_HT received update
    val summary = Waermepumpe_HT.sumSince(now.minusDays(1), "jdbc") /60 /100

so now… for this rule i have always the summary from now 24h back… correct?
If I let the rule run at 23:59 i will get the summary of the day…

Is there also a possibility to let the summary count up during the day?
to count only from midnight to now?

so… when the value for “Waermepumpe_HT” is stored every minute and I would like to get the power for the whole last 24 hours the rule have to be look like this right?

rule "Daily Power Consumption"
    Time cron “0 59 23 * * ?”
    val summary = Waermepumpe_HT.sumSince(now.minusDays(1), "jdbc") /60 /100

And to let the daily value count up during the day? is this also possible?

Sure. You don’t have to use persistence for that. If your readings reliably come in every minute, just /60 and then add to a running Wh total. Decide when you are going to reset that total to 0. You might for instance do that in your “midnight” rule, and also persist the day to your jdbc (you can persist on demand in a rule).


To be honest, I have no idea how to build the rule for this counting value.

I tried a lot and searched a lot… but the combination with persistence and non persistence is confusing me.
I know letting me find the solution by myself is the best way to learn, but this is a little bit to hard for me to understand how the syntax should look like. I also can´t find any example in the forum which leads me to the point.

Well, your reports come every minute reliably?
Oh doh - look back at your original post and they come each half-minute, correct?
You might need to change the maths in your other rules.

Alright, so create somewhere to keep your running total. Almost certainly an Item, you’ll likely want to persist and chart it later.
Now create a rule to trigger from every update to your modbus counter, so every half-minute.
Do maths on the half-minute reading to convert to Wh or kWh or whatever you want your total in.
Add to existing total and post the result back to the running total.
Done, you’ve made a meter.

You will want refinements on top - you need to zero the running total before first use.
You may want to zero at other times depending on what you want this meter to represent - daily, monthly?

Choose persistence if needed - you may want to persist a daily total to your SQL just once at the end of the day, for example.

However - you will probably want to persist frequently and have restore-on-startup so that the meter reading survives a reboot in the middle of the day - I’d do that with a different database, MAPDB is made for that job.

I changed the transmission rate of the value from 30s to 60s to have the easier math… :wink:
So the Item Waermepumpe_HT is changing every 60s now.

i found a topic where someone is asking nearly the same, but the syntax is not clear for me and i am not able to replicate it for my purposes.

I did a typo in my rule for the daily count… so i don´t know if this rule is working properly… I will see it tomorrow morning.

rule "Daily Power Consumption"
    Time cron "0 59 23 ? * * *"
    //Item Waermepumpe_HT changed
    val summary = Waermepumpe_HT.sumSince(now.minusDays(1), "jdbc") /60 /100

I am afraid this counting function I have to let it go for now… because I have to learn the persistance function and the rule syntax much much more in detail to solve this.

Normally i can gather all needed informations from different threads and combine them for my purposes but here i am stuck.

You want to simply sum up all the pulses?
Do that in your Wago PLC. PLCs excel at that stuff.
Then read the result into OpenHAB.


My wago is already counting these impulses and calculating the live power consumption.
This “live” data is written every 60s do the mysql database.

My maingoal is to have a graph of the total consumption per day and also a counting graph during the day.

Next steps would be to have monthly and yearly consumption values.


I would need the aggregateion the other way round. My SolarEdge inverter has valus for day, week year, but waht is missung is the total. I think the best way would be, to take the daily values and add them up. Unfurtunatly I have no idea how this would look like.

In this Example I have the following Items:

Thank you


I am doing this with a rule.
I do a “summary” of values which are written to the DB.

rule "Daily Power Consumption"
    Time cron "0 59 23 ? * * *"
    //Item Waermepumpe_HT changed
    val summary = Waermepumpe_HT.sumSince(now.minusDays(1), "jdbc") /60 /1000

At the End of the Day i count the values wich are written every 60 seconds and divide it again with 1000 to get kW instead of W.

so it should be possible to to this with your values of day, week or year?
I hope this leads you to a solution.

thx for your fast reply.

I think the difference is, that my daily production or consumption is ret every day by the inverter.

Every fifteen Minutes i get the total kWh from the inverter startigg at 00:00 until 23:59 then is starts again from 0,00kWh.

Would your rule still work?


Comment - what data you persist, and when, is critical to understanding what results you get from something like sumSince()

atm i persist theItems every hour in influx and in rrd4j

i would suggest you pull the data shortly before the inverter resets the values and write them to the DB and then you can work with this data to sum up the values to a total sum.

I do the same… the rule runs at 23:59 and do a sum of the houly “live” values and i reset then the counter manually… (in your case it is done automaticaly by the inverter.
So i have a sum for every day…