Energy rule


I have tried to search before posting but could not find a “best pratice” for this question.

I have 22 meeters that are just sending the kwh from 0 - 65565 (or something around that)
I want to use this to generate 4 items - ItemDaily, ItemWeekly, ItemMonthly, ItemYearly.
I want to show on habmin how much energy i’m using so far today, week, month year, i have made a logoritme that shows what energyclass im in at this moment.

My question is how is the best way to do this? With .rules or rule generator? Can i make a more global rule? Any suggestions to make this work, but also to do it the simplest way. Should this logic also be ready for when it hits maximum and goes to 0 again?

Is habmin necessary ?
Why not InfluxDB with grafana?

You will not need extra items like this…

I don’t know what you mean with “rule generator”. But yes, you will want to use Rules and Persistence.

What do you mean by “global”?

Most people do something like the following:

rule "Update power"
    Time cron "0 0 0 * * ? *"
    LastDayPower.postUpdate(Power.state as Number)
    LastMonthPower.postUpdate(LastDayPower.postUpdate(now.minusMonths(1)) // you can get more complex here is needed
    // Reset the reporter to start over at zero, most devices have a Channel to do this, if not you need to add some logic to handle this
1 Like

This seems very nice, i will have to look in to it.
Does the database use alot of ressources and MB? Is it easy to set up?

Sorry about my words, i ment rules in paper UI.
With the word global i ment that maybe it was possible to make one rule and tag alot of items? Instead making one rule per item?

About the rule, that seems like a good solution, but i still need to make one rule per item/meeter?

My RaspberryPi 3B can handle it pretty well. I had no problems so far.

There are a number of ways to make generic rules. Look Kat the design pattern postings, particularly the separation of behaviors, working with groups in rules, and associated items.

Hi again Rich,

I have tried to copy your rule completely, just different names.
Could you please verify that it looks ok? Because i get error missing ( and others.
Also how would you add the logic for Switch on/off for reset every night? I see that my device do have this feature.

I do have other meeters without this switch. Today i have some PLS that does the logic, but could openhab also do this? I mean if the meeter goes from 0-65556. In my mind it would be some kind of logic as with the week? (now-MinusDay(1))?

Im very new to this way of doing logic as i come from the dinosaur business of PLS and block programming.

I could if you post what you have exactly. Use code fences:

code goes here

Errors in openhab.log or elsewhere?

Assuming you are using a 2.x version binding, link the reset channel to a Switch Item and sendCommand(ON) to that Switch Item. If you don’t know how to do this then you would probably be best served reviewing the Beginner’s Tutorial and User’s Manual first and then coming back here with specific questions.

I don’t understand the question.

1 Like

The items that i want to make a rule like this does not support reset, so it will continue to count to 65556, then it will go back to 0. So i need to make another line for day, i have tried to do this, hope thats correct? Or will it show yesterday? KleppeveienKontorOvnAmper is my item that goes from 0 to 65556.

In Eclipse smarthome desginer.
I added a ) berfore the end, and the error disaper.
I still have some warings like the picture under.


rule "Update power"
    Time cron "0 0 0 * * ? *"
    KleppeveienKontorOvnAmper.postUpdate(Power.state as Number)

Can anyone please help?

in the last 4 lines there is a “)” missing at the end…
you need 3 pairs of brackets in each line:

  1. postUpdate
  2. sumSince
  3. minusDays

Then the additional bracket can be removed…


Thanks Andreas for the answer.
It looks better and i could remove the last bracket that way, i will post my updated rule under just to clearify that we im understand you correctly.

I still have some errors, dont know if they are important or if i need to do something more about my rule?

The two first one is:
The method sumSince(datetime) is undefined for the type NumberItem

The second two is:
Cannot convert from DateTime to Number
Cannot convert from Objekt to Number


rule "Update power"
    Time cron "0 0 0 * * ? *"
    KleppeveienKontorOvnAmper.postUpdate(KleppeveienKontorOvnAmper.state as Number)

I did some more editing, and now it looks like this, with the only error.
The method sumSince(datetime) is undefined for the type NumberItem

rule "Update power"
    Time cron "0 0 0 * * ? *"
    KleppeveienKontorOvnAmper.postUpdate(KleppeveienKontorOvnAmper.state as Number)

Hi Tommy,

to use “sumSince” you need persistence service (e.g. RRD4J or any other database) filled with values.
Did you set this up? If not have a look to the documentation or on other threads in this forum.

AND: The item must be a numeric item (not a string)…

In the first line you postUpdate “KleppeveienOvnAmper” with itself. Is this right? It doesn’t make sense…

Maybe give it a try, even if the designer got some Errors. And have a look at the log-Files…


Using VSCode will correctly identify all errors without false errors like Designer.

Im using RRD4J with a clean install of 2.2.0.
The item is a number item.
It seems to be semi-working, with that i mean something is happening, but its not at all correct.
I get very strange numbers…

Can it have something to do with the desimals at the end?

Hello Tommy,

is 1543,25 kWh the correct value for total counting?

If your base value is an upcounting value like a standard electricity or water meter and aou are storing the meter value to your database, you have to use “deltaSince(now.minus(days(1))”. With this expression, you get the difference between the to values from one day ago and now.

If your base value is the actual value per time slice (if you are using rrd4j you have to store the value every minute, for this your actual value should also represent the value per minute) then you can use the “sumSince(now.minus(days(1)))”. This function takes every value in the database and calculates the sum for the given time period.

I think the first case is yours…
But: you wrote, your counting value is from 0 to 65556 and then overflows to 0 again. For this configuration you run in troubles if the point of overflow is within the time you are lookin at. In this case it might be useful to use a dummy item to count up, even if an overflow occurs. This dummy item is not reset by an overflow and you can use this dummy item to do your calculations…


1 Like

Thanks for the good answer Andreas.
In this case its the first case, but as i have more then 50 meeters, i have some of them with Modbus that are counting, but i think i will do the logic for that in the PLC. Will search replace all sumSince with deltaSince and try.

Thanks alot! :slight_smile:

It works perfectly, thanks!

I see now that “Day” energy is actually yesterday, so i dont see how much I am currently using so far today.
A fix for this is using total and reset that at the same time.

I thought that just a simple add to my rule would fix this, but cant seem to make it work. Maybe i need to have some delay from switching it On to Off?
Is there any else way to do this also with SumDelta if devices dont have reset?

rule "Update power BilLading"
    Time cron "0 0 0 * * ? *"
    BilVarme.postUpdate(BilVarme.state as Number)
	BilVarmeReset.sendCommand (OFF)