I’m having a big problem with a rule I’m using to sum up how much electricity I’ve used throughout the day.
What I have:
Items:
Number energymonitor_channel1_watt "Total Power [%.0f W]" <energy> (gPower)
{ mqtt="<[TiziDomotica:energymonitor/channel1:state:JSONPATH($.W)]" }
Number energymonitor_channel1_millisecondssincelast "Total Power Time since Last Reading[%d mS]" <energy> (gPower,noPersist)
{ mqtt="<[TiziDomotica:energymonitor/channel1:state:JSONPATH($.msSinceLast)]" }
Number energymonitor_channel1_kWh "Total today [%.2f kWh]" <energy> (gPower)
Number energymonitor_channel1_cost_lowtariff "Total Night [%.2f €]" <energy> (gPower)
Number energymonitor_channel1_cost_hightariff "Total Day [%.2f €]" <energy> (gPower)
Rule:
rule "When energy channel 1 is updated, update the energy totals"
when
Item energymonitor_channel1_millisecondssincelast received update
then
//read current values
Oldcount = energymonitor_channel1_kWh.state
Oldlow = energymonitor_channel1_cost_lowtariff.state
Oldhigh = energymonitor_channel1_cost_hightariff.state
//calculate how many kWh have been spent since the last update
Wattage = energymonitor_channel1_watt.state
Timesincelast = energymonitor_channel1_millisecondssincelast.state
Meterincrement = (Wattage * Timesincelast) / 3600000 / 1000
Newcount = Oldcount + Meterincrement
energymonitor_channel1_kWh.postUpdate(Newcount)
//if we're in the expensive tariff hours, update that item and price, otherwise use the low tariff item and price
if(now.getHourOfDay() >=13 && now.getHourOfDay() <23) {
Newhigh = Oldhigh + ( Meterincrement * 0.185)
energymonitor_channel1_cost_hightariff.postUpdate(Newhigh)
}
else {
Newlow = Oldlow + ( Meterincrement * 0.085)
energymonitor_channel1_cost_lowtariff.postUpdate(Newlow)
}
end
What’s supposed to happen:
Every 40-50 seconds I receive a new MQTT message from my energy monitor, so I know the average wattage during the cycle and how many milliseconds this measurement covered. Receiving the message triggers my rule.
Multiplying the power and the time I know what amount of energy has been consumed and how much that costs at the current tariff.
So I first read the current number values and then add the increment
The values are persisted to mysql so i can later chart them.
There’s another rule resetting all counters (this is just one out of several like this) at midnight.
What actually happens:
All of this works like a charm most of the time, but at irregular intervals during the day the counter sum (be it power or cost) just resets to 0 or to some other previous value.
It looks as if the Number value were just lost altogether as if a system restart happened and that other value then gets restored back from persistence.
I am sure it’s not because of invalid/negative/null/outofrange readings coming from the measurement hardware. No strange values get logged at the times when the counters mess up.
Also the different counters mess up at different times. E.g. the total kWh count might reset at one time while the corresponding cost counter fails a few hours later. The times follow no pattern and are different every day.
Is anybody having experience with such problems?