Energy rule

Tags: #<Tag:0x00007fd32229d6a8> #<Tag:0x00007fd32229d4f0> #<Tag:0x00007fd32229d3b0>

(Andreas Imhof) #21

Hi Tommy,

your Item BilVarmeDag shows the value for the last 24 hours, because you use now.minusDays(1) it’s the difference between the actual value (now) and the value from the day before (minusDays(1)).

So if you want the difference to the value of midnight, you need to use “toDateTimeAtStartOfDay()”.
The Item should be:


For the values from beginning of the week or from beginning of the month I don’t know about a easy solution…


(Rich Koshak) #22


I have no idea why they use with instead of to.

(Tommy Hagenes) #23

Thanks! Updated rule now, and will see if it works.
Week and month is maybe OK as it is. But i’m using day every day to see my 40 meeters :slight_smile:

(Tommy Hagenes) #24

Hi again Andreas,

And thanks for the answer.
The complete line for this, should it be like this? I still need three bracket at the end to close the previosly?


I cant get it to work as intended. Or maybe i misunderstand. My goal is to show the value in realtime of how much i use today. So if i use 1kw/h, then it should say 0 at 00.00 and 1 at 01.00, then at 17.00 when i look at all my meeters this one shows 17kw/h.

(Josar) #25

@tomhag maybe you missed what @rlkoshak posted and yes you will need the closing brackets.

withTimeAtStartOfDay != toDateTimeAtStartOfDay

So it should look like this

BilVarmeDag.postUpdate( BilVarme.deltaSince( now.withTimeAtStartOfDay() ) )

(Tommy Hagenes) #26

Thanks Josar, i read what Rick was reading but then i started to copy/past and forgot :zipper_mouth_face:
Updated now and will see tonight if it works, thanks also for clearifying about the brackets.

(Andreas Imhof) #27

Hi Tommy,

sorry, you are right about the brackets. You Need for every opened bracket a closing one. So you need one for:

  • withTimeAtStartOfDay
  • deltaSince
  • postUpdate

The other problem about “realtime” values:
Your rule triggers every night at midnight (“0 0 0 * * ? *”), so your calculated items are updated once a day at midnight.
If you want to update the items more frequently, you have to change the trigger condition.
For the energy consumption for the current day I would suggest a second rule, which is triggered on every Change of BilVarme. So if your power meter updates the value, the consumption for the current day is also updated.


(Tommy Hagenes) #28

Hi again Andreas,

Ofcourse as calculation is done at midnight, so for this solution the “day” should be a standalone rule.
But is there a better way to do this? I do have the “total” counter and also a reset.
If i reset the total at midnight the “total” will only count that day.
But will that be a problem for week, month and year?

I tried with a simple rule at the end for this.

BilVarmeReset.sendCommand (OFF)

But that did not do it, dont know if my rule is wrong or if it happens so quick that it does not reset? Maybe there should be a “wait” for 5 seconds?

(Andreas Imhof) #29

Hi Tommy,

if you reset your total value each day you will have a problem showing your weekly/monthly/yearly values.
The assumption for this kind of usage is, that you have an energy Counter which is counting up without reset. If you reset the counter, you will get negative values for your weekly/monthly/yearly values which are indeed wrong in this situation.

I did not get the logic for resetting the counter and why you want to reset the counter.

You have energy meters which are:

  • counting up
  • overflow at 65556 (to start at 0 again)
  • some of them you can reset manually

I would use two items for each energy meter:

  1. the actual value of the energy meter
  2. the sum/total of the energy meter

The first item represents the value you get from the counter. Via rule you update the second item from the first item with respect to overflows. Therefore your second item has the actual value since you started your measurement. So if your energy meter had the third Overflow, the second item should have a value around 196.668

Your reporting is done with the second item, which has no overflows and shows the real values for week/month/year.

With this Approach you don’t need any manual reset of the energy meters…


(Tommy Hagenes) #30

Thanks for all the input.
My solution ended up with this rule, this way i get the energy comsumption today(updating at every change), yesterday, week, month and year. I think the information you get from one sensor makes it valuable with this rule. Hope this can be usefull for someone else.

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

rule "Update power BilLadingNa"
		Item BilVarme changed
	BilVarme.postUpdate(BilVarme.state as Number)
    BilVarmeDagNa.postUpdate( BilVarme.deltaSince( now.withTimeAtStartOfDay() ) )

(Andreas Imhof) #31

Hello Tommy,

good to hear you got it working. As exlanation to others:

BilVarmeDag   : consumption yesterday
BilVarmeUke   : consumption since last week (from yesterday) / 1 week
BilVarmeManed : consumption since last month (from yesterday) / 1 month
BilVarmeAr    : consumption since last year (from yesterday) / 1 year
BilVarmeDagNa : consumption today

But can you tell me, why you are Setting
BilVarme.postUpdate(BilVarme.state as Number)
in both rules ?


(Tommy Hagenes) #32

For me it just makes sense to not to this at all, because in items its a Number Item.
But this was to input i got at the start from Rich, and also Eclipse Smart home designer i got an error that it was undefined.
Do i need it?

The reason i keep it in both rules is that some meeters will not change in days, so days will trigger before change of value, but other will trigger with change up to every minute.

(Andreas Imhof) #33

The idea from Rich was to store your (running) meter value to the “Last Day” item at midnight… The source and target items were different in this very early post…

I think, you don’t need this anymore…


(Tommy Hagenes) #34

Ok, will delete it and test it.

My next step now is to try to categorize the meeters in “Heat”, “Lightning” and “Others”.
Do you have an advice on how to do this? My first thought is to tag it somehow and then use a rule that sum all meeters that are tagged with “Heat”.

(Andreas Imhof) #35

You can use groups to manage this. Have a look here:


(Tommy Hagenes) #36

Thanks Andreas.
If you have any tips on how to understand rules better im always looking to learn as this way of doing it is new for me. I have been using block engineering for 10 years but not “script”.

(Andreas Imhof) #37

Hi Tommy,

to get used to writing rules I recommend to read as much of examples as possible. Just search for topics you are interested in and give it a go. Or look within the rules topics to read about different rules: what they are doing and how they are programmed.

If you have specific problems, just give it a go and ask the community.

Try to read about groups and sums. You will get a lot of information. Try to adapt the examples to your like. If it is not working the way you thougt it should, try to read more or ask…


(Tommy Hagenes) #38

Hi again,

I got values on week/month/year, but it did not make any sense and then it stopped.
I did not do any changes to the script, and also i have tried make a new .rule file with only this rule to make sure nothing else made this.
Database seems to be working and for day everything is perfect.

Any ideas?

(Andreas Imhof) #39

How is your continuous meter value (= Item BilVarme) in this time frame?
Can you check the values for BilVarme in the given time period?
If you have the values you can just calculate “by hand” which should be the correct value…

The graph shows that the difference between the value from now and one month ago is 124 kWh.

OR: the rule is not working / called any more and you get no more updates to your Item BilVarmeManed…

Just add a log entry at the beginning of the rule (first line after “then”):

logInfo('logEnergy', 'Rule Update power BilLading executing. BilVarme = {}', BilVarme.state)

Every time your rule is triggered, you get a log entry with the actual value of BilVarme. With these logs you can see what is going on… (if the rule is triggered and which actual value is used to calculate the difference)


(Tommy Hagenes) #40

Hi again Andreas,

Thanks again for a very detaled help.
The BilVarme is continuing to count, so thats not it.
I also made this rule for a seperate meeter, i see that today i got the “week”, maybe because it did not have enough datasampled for a week? Month and Year is still just “- kwh” (today it was 7 days since i added it)

Also the usage on BilVarme is almost the same every week so that could also explane why the log is also showing the same there. But for year it does not make sense.

Will try the loginfo and continue investigate.

Thanks again.