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.