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!