Energy rule

energy
rules
ruleengineexperim
Tags: #<Tag:0x00007fae02fb5088> #<Tag:0x00007fae02fb4d68> #<Tag:0x00007fae02fb48e0>

(Tommy Hagenes) #1

Hi,

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?


(Elias Hackradt) #2

Is habmin necessary ?
Why not InfluxDB with grafana?

You will not need extra items like this…


(Rich Koshak) #3

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"
when
    Time cron "0 0 0 * * ? *"
then
    LastDayPower.postUpdate(Power.state as Number)
    LastWeekPower.postUpdate(LastDayPower.sumSince(now.minusWeeks(1))
    LastMonthPower.postUpdate(LastDayPower.postUpdate(now.minusMonths(1)) // you can get more complex here is needed
    LastYearPower.postUpdate(LastDayPower.postUpdate(now.minusYears(1))
    
    // 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
end

(Tommy Hagenes) #4

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?


(Tommy Hagenes) #5

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?


(Elias Hackradt) #6

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


(Rich Koshak) #7

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.


(Tommy Hagenes) #8

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.


(Rich Koshak) #9

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.


(Tommy Hagenes) #10

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.


bilde

rule "Update power"
when
    Time cron "0 0 0 * * ? *"
then
    KleppeveienKontorOvnAmper.postUpdate(Power.state as Number)
    KleppeveienKontorOvnAmperDag.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusDays(1))
    KleppeveienKontorOvnAmperUke.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusWeeks(1))
    KleppeveienKontorOvnAmperManedpostUpdate(KleppeveienKontorOvnAmper.postUpdate(now.minusMonths(1))
    KleppeveienKontorOvnAmperAar.postUpdate(KleppeveienKontorOvnAmper.postUpdate(now.minusYears(1))
 )   
end

(Tommy Hagenes) #11

Can anyone please help?


(Andreas Imhof) #12

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…

Andreas


(Tommy Hagenes) #13

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

bilde

rule "Update power"
when
    Time cron "0 0 0 * * ? *"
then
    KleppeveienKontorOvnAmper.postUpdate(KleppeveienKontorOvnAmper.state as Number)
    KleppeveienKontorOvnAmperDag.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusDays(1)))
    KleppeveienKontorOvnAmperUke.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusWeeks(1)))
    KleppeveienKontorOvnAmperManed.postUpdate(KleppeveienKontorOvnAmper.postUpdate(now.minusMonths(1)))
    KleppeveienKontorOvnAmperAr.postUpdate(KleppeveienKontorOvnAmper.postUpdate(now.minusYears(1)))
    
end

(Tommy Hagenes) #14

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"
when
    Time cron "0 0 0 * * ? *"
then
    KleppeveienKontorOvnAmper.postUpdate(KleppeveienKontorOvnAmper.state as Number)
    KleppeveienKontorOvnAmperDag.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusDays(1)))
    KleppeveienKontorOvnAmperUke.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusWeeks(1)))
    KleppeveienKontorOvnAmperManed.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusMonths(1)))
    KleppeveienKontorOvnAmperAr.postUpdate(KleppeveienKontorOvnAmper.sumSince(now.minusYears(1)))
    
end

(Andreas Imhof) #15

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…

Andreas


(Rich Koshak) #16

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

https://docs.openhab.org/configuration/editors.html


(Tommy Hagenes) #17

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?


(Andreas Imhof) #18

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…

Andreas


(Tommy Hagenes) #19

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:


(Tommy Hagenes) #20

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"
when
    Time cron "0 0 0 * * ? *"
then
    BilVarme.postUpdate(BilVarme.state as Number)
    BilVarmeDag.postUpdate(BilVarme.deltaSince(now.minusDays(1)))
    BilVarmeUke.postUpdate(BilVarme.deltaSince(now.minusWeeks(1)))
    BilVarmeManed.postUpdate(BilVarme.deltaSince(now.minusMonths(1)))
    BilVarmeAr.postUpdate(BilVarme.deltaSince(now.minusYears(1)))
	BilVarmeReset.sendCommand(ON)
	BilVarmeReset.sendCommand (OFF)
	    
end