Sum up several items representing energy consumption

I have several tasmota devices measuring power consumption and transferring that data via mqtt to my broker and openhab. On the other side, I have a PV module producing electricity.
I now want to have a graph with two lines, one representing consumption, one production. How can I create an item with the sum of all the consumption?

Check for sum aggregation function :wink:

Hi,
I did something similar with my openhab.
I use, among other things, 3 power meters for consumption and 1 for generating.

With a rule I calculate whether I‘ve got excess energy or not. A positive value at the end indicates that I generate more energy than I am using at the moment.
An another rule switches different things, depending on the items state, to use the excess energy for e.g. recharging my e-bike battery etc.


rule "power all instant"
when
   Item Power_instant1 received update or
   Item Power_instant2 received update or
   Item Power_instant3 received update or
   Item Power_solar received update 
   
then
	var Number Powerinstant1 = (Power_instant1.state as QuantityType<Number>).doubleValue
    var Number Powerinstant2 = (Power_instant2.state as QuantityType<Number>).doubleValue
    var Number Powerinstant3 = (Power_instant3.state as QuantityType<Number>).doubleValue
	var Number Powersolar = (Power_solar.state as QuantityType<Number>).doubleValue
	var Number Powerdiff = 0
	Powerdiff = (Powersolar-(Powerinstant1+Powerinstant2+Powerinstant3))
	Power_all_instant.postUpdate(Powerdiff)
end

Many thanks!

I created an item “Tasmota_Consumption_Sum” and a script. But the value Tasmota_Consumption_Sum remains NULL.

What kind of channel type are your items for the power?
Just a number item or a number:power?

Any logs when you trigger the rule?

FYI: You don’t set the Arbeitszimmer_Power as a trigger.

I think these are just number items:

Is that correct?

I get a lot of errors when trying to run, but have no clue about them:

10:29:21.768 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID '8d634ec5d3' failed:  ___ rule  ___ "Tasmota_Verbrauch_Gesamt"
when
   Item GenericMQTTThing_TKuehlschrank r ___ eceiv ___ ed update or
   Item GenericMQTTThing_Waschmaschine_Power received update or
   Item GenericMQTTThing_Spuelmaschine_Power received update or
   Item GenericMQTTThing_TasmotaGartenPV received update
   Item GenericMQTTThing_Arbeitszimmer_Power received update



then
        var Number Powerinstant1 = (GenericMQTTThing_TKuehlschrank.state as QuantityType<Number>).doubleValue
    var Number Powerinstant2 = (GenericMQTTThing_Waschmaschine_Power.state as QuantityType<Number>).doubleValue
    var Number Powerinstant3 = (GenericMQTTThing_Spuelmaschine_Power.state as QuantityType<Number>).doubleValue
    var Number Powerinstant4 = (GenericMQTTThing_Arbeitszimmer_Power.state as QuantityType<Number>).doubleValue
    var Number Powersolar = (GenericMQTTThing_TasmotaGartenPV.state as QuantityType<Number>).doubleValue
        var Number Powerdiff = 0
        Powerdiff = (Powersolar-(Powerinstant1+Powerinstant2+Powerinstant3+Powerinstant4))
    Powerdiff = 3
        Tasmota_Consumption_Sum.postUpdate(Powerdiff)
end


   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 32, length 4
   3. The method or field received is undefined; line 3, column 76, length 8
   4. The method or field update is undefined; line 3, column 85, length 6
   5. The method or field or is undefined; line 3, column 92, length 2
   6. The method or field received is undefined; line 4, column 140, length 8
   7. The method or field update is undefined; line 4, column 149, length 6
   8. The method or field or is undefined; line 4, column 156, length 2
   9. The method or field received is undefined; line 5, column 204, length 8
   10. The method or field update is undefined; line 5, column 213, length 6
   11. The method or field or is undefined; line 5, column 220, length 2
   12. The method or field received is undefined; line 6, column 264, length 8
   13. The method or field update is undefined; line 6, column 273, length 6
   14. The method or field received is undefined; line 7, column 326, length 8
   15. The method or field update is undefined; line 7, column 335, length 6
   16. The method or field then is undefined; line 11, column 352, length 4
   17. The method or field end is undefined; line 21, column 1076, length 3
   18. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 5, length 26
   19. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 40, length 4
   20. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 45, length 30
   21. This expression is not allowed in this context, since it doesn't cause any side effects.; line 4, column 98, length 4
   22. This expression is not allowed in this context, since it doesn't cause any side effects.; line 4, column 103, length 36
   23. This expression is not allowed in this context, since it doesn't cause any side effects.; line 5, column 162, length 4
   24. This expression is not allowed in this context, since it doesn't cause any side effects.; line 5, column 167, length 36
   25. This expression is not allowed in this context, since it doesn't cause any side effects.; line 6, column 226, length 4
   26. This expression is not allowed in this context, since it doesn't cause any side effects.; line 6, column 231, length 32
   27. This expression is not allowed in this context, since it doesn't cause any side effects.; line 7, column 284, length 4
   28. This expression is not allowed in this context, since it doesn't cause any side effects.; line 7, column 289, length 36

I made the rule much simpler, still an error:

10:32:42.891 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'GenericMQTTThing_TasmotaGartenPV' changed from 79 to 81
10:32:48.768 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID '8d634ec5d3' failed:  ___ rule  ___ "Tasmota_Verbrauch_Gesamt"
when
   Item GenericMQTTThing_TKuehlschrank r ___ eceiv ___ ed update or

then
        var Number Powerinstant1 = (GenericMQTTThing_TKuehlschrank.state as QuantityType<Number>).doubleValue
    Tasmota_Consumption_Sum.postUpdate(Powerinst1)
end


   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 32, length 4
   3. The method or field received is undefined; line 3, column 76, length 8
   4. The method or field update is undefined; line 3, column 85, length 6
   5. The method or field or is undefined; line 3, column 92, length 2
   6. The method or field then is undefined; line 5, column 96, length 4
   7. The method or field Powerinst1 is undefined; line 7, column 243, length 10
   8. The method or field end is undefined; line 8, column 255, length 3
   9. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 5, length 26
   10. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 40, length 4
   11. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 45, length 30

First change the item to be a number:power item.
Otherwise the as QuantityType<Number>).doubleValue would not be correct.
Then make sure there are no typos like the var Powerinstant1 and not Powerinst1.
If you use only one trigger you should remove the or.

sorry, corrected them already, but forgot to edit the post

11:09:27.529 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'GenericMQTT                                        Thing_TasmotaGartenPV' changed from 133 to 132
11:09:37.133 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution                                         of rule with UID '8d634ec5d3' failed:  ___ rule  ___ "Tasmota_Verbrauch_Gesamt"
when
   Item GenericMQTTThing_TKuehlschrank r ___ eceiv ___ ed update

then
        var Number tmp = 2
    Tasmota_Consumption_Sum.postUpdate(tmp)
end


   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 32, length 4
   3. The method or field received is undefined; line 3, column 76, length 8
   4. The method or field update is undefined; line 3, column 85, length 6
   5. The method or field then is undefined; line 5, column 93, length 4
   6. The method or field end is undefined; line 8, column 162, length 3
   7. This expression is not allowed in this context, since it doesn't cause any                                         side effects.; line 1, column 5, length 26
   8. This expression is not allowed in this context, since it doesn't cause any                                         side effects.; line 3, column 40, length 4
   9. This expression is not allowed in this context, since it doesn't cause any                                         side effects.; line 3, column 45, length 30

And I added the number:energy item, still same errors:


11:35:22.966 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID '8d634ec5d3' failed:  ___ rule  ___ "Tasmota_Verbrauch_Gesamt"
when
   Item GenericMQTTThing_TKuehlschrank_P ___ ower  ___ received update

then
        var Number Powerinstant1 = (GenericMQTTThing_TKuehlschrank_Power.state as QuantityType<Number>).doubleValue
    Tasmota_Consumption_Sum.postUpdate(Powerinstant1)
end


   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 32, length 4
   3. The method or field received is undefined; line 3, column 82, length 8
   4. The method or field update is undefined; line 3, column 91, length 6
   5. The method or field then is undefined; line 5, column 99, length 4
   6. The method or field end is undefined; line 8, column 269, length 3
   7. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 5, length 26
   8. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 40, length 4
   9. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 45, length 36
11:35:25.389 [ERROR] [.internal.handler.ScriptActionHandler] - Script execution of rule with UID '8d634ec5d3' failed:  ___ rule  ___ "Tasmota_Verbrauch_Gesamt"
when
   Item GenericMQTTThing_TKuehlschrank_P ___ ower  ___ received update

then
        var Number Powerinstant1 = (GenericMQTTThing_TKuehlschrank_Power.state as QuantityType<Number>).doubleValue
    Tasmota_Consumption_Sum.postUpdate(Powerinstant1)
end


   1. The method or field rule is undefined; line 1, column 0, length 4
   2. The method or field when is undefined; line 2, column 32, length 4
   3. The method or field received is undefined; line 3, column 82, length 8
   4. The method or field update is undefined; line 3, column 91, length 6
   5. The method or field then is undefined; line 5, column 99, length 4
   6. The method or field end is undefined; line 8, column 269, length 3
   7. This expression is not allowed in this context, since it doesn't cause any side effects.; line 1, column 5, length 26
   8. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 40, length 4
   9. This expression is not allowed in this context, since it doesn't cause any side effects.; line 3, column 45, length 36

when-then-end rule structure is for files. Don’t copy/paste them into GUI rule entry.

I dont get it. Where should I put that rule then?
or how should I adapt the structure?

Create a text file with .rules at the end.
E.g. Power.rules

Copy the code into it and save it to path $OPENHAB_CONF/rules of your openhab installation.

I think you should take a step back here.

a) You want to make charts, you need numbers.

b) If you want to add up a bunch of number Items, you don’t need rules. You just make your Items members of a Group and tell it to do the sum for you.

But you need Number type Items.

The screenshot you showed us earlier has lots of String type Items. Are these the ones you are interested in? Can’t tell.

I have searched the docs, but did not find a function to group Items. Can you please elaborate a bit?
There are strings, but from the strings (JSON) parts are taken and stored as numbers.

Seems you did not read the docs carefully enough

1 Like

You can’t chart or add up strings. I’m suggesting you might benefit from putting the data into Number type Items to begin with.