Rule for calculating sum of several Number:Energy Items not working correctly

Hiya,
I have created two rules to calculate the current and total energy flowing through a few Sonoff-POW’s. This is used for solar monitoring.

Here is the relevant part of the .items-file:

Switch Sonoff_SolarWest "Solar West" <solarplant> { mqtt=">[mosquitto:cmnd/Sonoff-SolarWest/POWER:command:*:default], <[mosquitto:stat/Sonoff-SolarWest/POWER1:state:default]" }
Number:Energy Sonoff_SolarWest_Power "Einspeisung West momentan [%.1f W]" <power> (Garten, Steckdosen) { mqtt="<[mosquitto:tele/Sonoff-SolarWest/SENSOR:state:JSONPATH($.ENERGY.Power)]" }
Number:Energy Sonoff_SolarWest_Power_Total "Einspeisung West total [%.2f kWh]" <power> (Garten, Steckdosen) { mqtt="<[mosquitto:tele/Sonoff-SolarWest/SENSOR:state:JSONPATH($.ENERGY.Total)]" }

Switch Sonoff_SolarOst "Solar Ost" <solarplant> { mqtt=">[mosquitto:cmnd/Sonoff-SolarOst/POWER:command:*:default], <[mosquitto:stat/Sonoff-SolarOst/POWER1:state:default]" }
Number:Energy Sonoff_SolarOst_Power "Einspeisung Ost momentan [%.1f W]" <power> (Gartenhaus, Steckdosen) { mqtt="<[mosquitto:tele/Sonoff-SolarOst/SENSOR:state:JSONPATH($.ENERGY.Power)]" }
Number:Energy Sonoff_SolarOst_Power_Total "Einspeisung Ost total [%.2f kWh]" <power> (Gartenhaus, Steckdosen) { mqtt="<[mosquitto:tele/Sonoff-SolarOst/SENSOR:state:JSONPATH($.ENERGY.Total)]" }

Switch Sonoff_SolarBOB "Solar BOB" <solarplant> { mqtt=">[mosquitto:cmnd/Sonoff-SolarBOB/POWER:command:*:default], <[mosquitto:stat/Sonoff-SolarBOB/POWER1:state:default]" }
Number:Energy Sonoff_SolarBOB_Momentan "Einspeisung BOB Momentan [%.1f W]" <power> (Garten, Steckdosen) { mqtt="<[mosquitto:tele/Sonoff-SolarBOB/SENSOR:state:JSONPATH($.ENERGY.Power)]" }
Number:Energy Sonoff_SolarBOB_Total "Einspeisung BOB total [%.2f kWh]" <power> (Garten, Steckdosen) { mqtt="<[mosquitto:tele/Sonoff-SolarBOB/SENSOR:state:JSONPATH($.ENERGY.Total)]" }

Here is the .rules file used for the calculation:

rule "Solar Momentan berechnen"
when
    Item Sonoff_SolarWest_Power changed or
    Item Sonoff_SolarOst_Power changed or
    Item Sonoff_SolarBOB_Momentan changed
then
    // Solarwerte Momentan Gartenhaus zusammenrechnen
    var Number num1 = Sonoff_SolarWest_Power.state as Number
    var Number num2 = Sonoff_SolarOst_Power.state as Number
    var Number num3 = Sonoff_SolarBOB_Momentan.state as Number
    var Number num4 = (num1 + num2 + num3)
    SolarSumme_Momentan.postUpdate(num4)
end

rule "Solar Total berechnen"
when
    Item Sonoff_SolarWest_Power_Total changed or
    Item Sonoff_SolarOst_Power_Total changed or
    Item Sonoff_SolarBOB_Total changed
then
    var Number SolarWest = Sonoff_SolarWest_Power_Total.state as Number
    var Number SolarOst = Sonoff_SolarOst_Power_Total.state as Number
    var Number SolarBOB = Sonoff_SolarBOB_Total.state as Number
    var Number SolarSUM = SolarWest + SolarOst + SolarBOB
    logInfo("SolarRechner", "West ist aktuell " + SolarWest )
    logInfo("SolarRechner", "Ost ist aktuell " + SolarOst )
    logInfo("SolarRechner", "BOB ist aktuell " + SolarBOB )
    logInfo("SolarRechner", "SUM ist aktuell " + SolarSUM )
    SolarSumme_Total.postUpdate(SolarSUM)
end

The first rule is working fine, the values are added together and the calculated sum is showing correctly:

2021-03-17 08:18:08.093 [vent.ItemStateChangedEvent] - Sonoff_SolarOst_Power changed from 16.0 W to 3.0 W
2021-03-17 08:18:08.094 [vent.ItemStateChangedEvent] - SolarSumme_Momentan changed from 19.0 to 6.0

but the second rule is completely off when calculating. I added the Info-Logging to get the numbers that it is working with:

2021-03-17 08:11:46.722 [INFO ] [.smarthome.model.script.SolarRechner] - West ist aktuell 0.002 kWh
2021-03-17 08:11:46.722 [INFO ] [.smarthome.model.script.SolarRechner] - Ost ist aktuell 0.002 kWh
2021-03-17 08:11:46.722 [INFO ] [.smarthome.model.script.SolarRechner] - BOB ist aktuell 0.001 kWh
2021-03-17 08:11:46.722 [INFO ] [.smarthome.model.script.SolarRechner] - SUM ist aktuell 18000.000

I was expecting SUM to be “0.005”. Shouldn’t the math be the same for both rules?

  • Platform information:
    • openHAB version: 2.5.12
    • Java Runtime Environment: Zulu11.45+27-CA

Thanks in advance for any hints / tips on what I am doing wrong!

What type of Item ar your totals? It is important.

I’d do the maths in Quantities (with units) rather than Numbers.

What it comes down to is that you’ve described your total-holding variable as a Number (without units) but your variables to be added are nasty hybrid types - declared as Number but with units given.
The rules interpreter doesn’t really know what you mean by all this, I think what really goes wrong is where kW gets involved, with its k multiplier, instead of a “root” unit W.

If you get all your input states as Quantity types, the maths works and takes account of any conversions needed. And returns a Quantity type as the sum, ready to post to a Quantity type Item.

A somewhat related question you may know off the top of your head. How do quantity types work with Groups and aggregation? Could OP define a Group:Number:Energy:SUM ?

Famous last words, but SUM should work as expected - providing everything is in same Quantity type e.g.members are all Number:Energy too. Actual units (kWh, Wh, Joules) shouldn’t matter.

2 Likes

Thanks for your replies!

I forgot to include the two SUM-items, here they are:

Number:Energy SolarSumme_Momentan "Einspeisung Total Momentan [%.1f W]" (solar)
Number:Energy SolarSumme_Total "Einspeisung Total [%.2f kWh]" (solar)

After some trial and error (managing to crash my openhab with loads java exception errors :rofl:), it looks like it is working with the following:

rule "Solar Total berechnen"
when
    Item Sonoff_SolarWest_Power_Total changed or
    Item Sonoff_SolarOst_Power_Total changed or
    Item Sonoff_SolarBOB_Total changed
then
    var Number SolarWest = (Sonoff_SolarWest_Power_Total.state as QuantityType<Number>).doubleValue
    var Number SolarOst = (Sonoff_SolarOst_Power_Total.state as QuantityType<Number>).doubleValue
    var Number SolarBOB = (Sonoff_SolarBOB_Total.state as QuantityType<Number>).doubleValue
    var Number SolarSUM = SolarWest + SolarOst + SolarBOB
    logInfo("SolarRechner", "West ist aktuell " + SolarWest )
    logInfo("SolarRechner", "Ost ist aktuell " + SolarOst )
    logInfo("SolarRechner", "BOB ist aktuell " + SolarBOB )
    logInfo("SolarRechner", "SUM ist aktuell " + SolarSUM )
    SolarSumme_Total.postUpdate(SolarSUM)
end

This is the output:

2021-03-17 22:51:49.140 [INFO ] [.smarthome.model.script.SolarRechner] - West ist aktuell 0.0
2021-03-17 22:51:49.141 [INFO ] [.smarthome.model.script.SolarRechner] - Ost ist aktuell 0.009
2021-03-17 22:51:49.144 [INFO ] [.smarthome.model.script.SolarRechner] - BOB ist aktuell 0.017
2021-03-17 22:51:49.144 [INFO ] [.smarthome.model.script.SolarRechner] - SUM ist aktuell 0.026

Thanks again for your help!

1 Like