Energy rules, sum of group to item help

Hi
I’m fairly new to OpenHab and I am trying to set up some rules for energy monitoring that I can’t get to work. I want to Chart the values based on Day, Weekly, Monthly and Yearly consumption.

What I want to do is fairly simple. Add all kWh values from the GEnergyLight group to the EnergyLight Item. The reason I want to this is so I can persist EnergyLigth and use it in the rules. As far as I understand Group items like GEnergyLight can’t be persisted and therefore the SUM in GEnergyLight is only useful in the actual sitemap?

So is there and easy way of doing this with group instead of listing all the items in GEnergyLight and combining them?

I’ve created the following Items and Groups (Not all items of the groups are included)

/* Groups energy */
Group:Number:SUM GLysWatt "Strøm forbruk: Lys [%.1f W]"         <energy> (GWatt)
Group:Number:SUM GEnergyLight "Energi forbruk lys: [%.1f kWh]"  <energy> (GEnergy)

Group:number GWatt "Strøm forbruk: Totalt [%.1f W]" (GAll)
Group GEnergy "Energi forbruk: Totalt [%.1f kWh]" (GAll)
Group GEnergyDay "Daglig energi forbruk [%.1f kWh]"(GEnergy)
Group GEnergyWeek "Ukentlig energi forbruk [%.1f kWh]"(GEnergy)
Group GEnergyMonth "Månedlig energi forbruk [%.1f kWh]"(GEnergy)
Group GEnergyYear "Årlig energi forbruk [%.1f kWh]"(GEnergy)

Number   EnergyLight       "Energi forbruk lys totalt: [%.1f kWh]" (GEnergy)
Number   EnergyLightDag   "Energi forbruk lys dag: [%.1f kWh]"    (GEnergyDay)
Number   EnergyLightWeek  "Energi forbruk lys uke: [%.1f kWh]"    (GEnergyWeek)
Number   EnergyLightMonth "Energi forbruk lys måned: [%.1f kWh]"  (GEnergyMonth)
Number   EnergyLightYear  "Energi forbruk lys år: [%.1f kWh]"     (GEnergyYear)

This is what I tried so far and did not work

//rule "EnergyLightToItem"
//when
//  Time cron "0 30 * * * ?"
//then
////Update group value to item
//  EnergyLight.postUpdate(GEnergyLight.state as DecimalType)
//	
//end

Any help is appriciated :slight_smile:

Hello Danonym1,
what are you really trying to achieve? What are the Day, Week etc. items for?
You should divide your idea into two parts I think.

  1. Items to store and display individual and aggregated consumptions for rules and your sitemap
  2. “Persisting” these items to a more powerful graphing solution. I use InfluxDB + Grafana (easy to setup). InfluxDB will store your data and Grafana will create nice graphs with all kinds of options. In the next step, you can export your graphs as images for your sitemap.

A group in openHAB contains one or more items. The value of the group (if using SUM) is the sum of these items, but not the sum over time. Where comes the data from? I can’t see any Item with an active binding. Let’s say there is an Item EnergyLight with continuous consumption measures. You have to persist this Item (e.g. everyMinute in rrd4j).
Then you can use a rule to set up proxy items for e.g. daily consumption:

rule "sum up"
when 
    Time cron "0 * * * * ?"
then
    var double mySum = EnergyLight.sumSince(now.withTimeAtStartOfDay).doubleValue / 60
    EnergyLightDag.postUpdate(mySum)
end

I did not test this in real, so maybe I have made some mistakes :slight_smile: but in theory, this would end up in an Item EnergyLightDag which will show up the used energy since midnight.

2 Likes

Sorry maybe I should have been a little more specific about what I am trying to achieve.

The main goal is to Graph my energy consumption. I want to have one graph for the daily energy consumption, one graph for weekly and so on. This is why I have divided the Groups into GEnergyDay/Week/Month/Year. So for each part I want to Graph I also have virtual/proxy items EnergyLightDay/Week…

I am not planning to use the OpenHab sitemap for displaying this as I think it does not look nice (sorry). I’m planning to use Dashing for all my smarthouse information, but will also look into Grafan now.

As for persistance I am using a mysql persist on all Energy related items (as recommended on some other post)

But the only thing I need help with now is making a rule that takes all the Items in the GEnergyLigth Group and sums them together and add to the virtual/proxi item EnergyLight (which is persisted with mysql)). I’m basically wonderig if this is possible using the .member and a for Each combination instead of listing all the items in the rule. Maybe I also should mention that the reason I want to do this is that I want to see energy consumption for lights under one and not divided for each light the house.

I can also mention that the items under GEnergyLight are always increasing (never reset)

You can also use the GEnergyLight’s sum value in your rules. But you are correct, you must persist it for charts. But you are in luck! According to the wiki, if you list a Group in your persistence file the same as you list an Item its sum value will be persisted as well. It is only if you use the “*” after the name that it only persists the members of the group and not the Group’s value. The usual warning about persisting with a once a minute strategy to chart with rrd4j applies.

Not too difficult.


    var Number sum = 0;
    for(NumberItem n : GEnergyLight.members as List<NumberItem>){
        sum = sum + n.state as DecimalType
    }

But like I said above, you don’t need to.

A normal for loop like above works. The problem with forEach is that you can’t pass it any variables that can be changed so there is no way to get the sum out of the forEach lambda. Use forEach when you want to do something to each Item in the list. Use a for loop like the above if you want to aggregate values.

But again, you can persist the Group directly so the whole conversation about for verses forEach at this point is strictly informative.

1 Like

Thanks a lot Rich. This was very informativ.

Aha, I see, I must have missed this reading through the wiki. I will test this later today. This was initially what I wanted. I’m using mysql for persisting all energy related items. So the once a minute strategy shouldn’t be applicable in this case. But thanks for the heads up.

As for the rest of your post. Thank you for taking your time and adressing each part of my post before. It is very educational and nice to get all ones questions and assumptions answered.

This worked like a charm. Thanks Rich