Electricity counter item changes/resets unexpectedly

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:


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 "When energy channel 1 is updated, update the energy totals"
   Item energymonitor_channel1_millisecondssincelast received update
  //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

  //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)
    else {
    Newlow = Oldlow + ( Meterincrement * 0.085)

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?


BTW this a typical chart of one of the kWh sums from today:
at 22:47h the number suddenly changes from 5.21 to 3.63 (which suspiciously was a value earlier in the day), then at 23:47 it suddenly jumps up to 9.61 before it eventually gets reset to 0 at midnight

Oooookay, I finally figured it out. It turns out that variables were spilling over between my different electricity counts, since they shared the same same rules file.
I thought that could never happen since they’ll trigger more than half a minute apart and only take a second to execute. But as it turns out every now and then Openhab’s rule execution sucks so horribly that they’ll overlap and one’s count gets mixed up with another’s.

Hi ,i try to to calculate the dayly production of solar panel , i have actually only a realtime Wh reader and i try to store this data on KWh/per day from midnight to now. So i try the “deltaSince(now.withTimeAtStartOfDay” but is not correct is not a “Sum” .
I see you setup this :

Number energymonitor_channel1_millisecondssincelast "Total Power Time since Last Reading[%d mS]" <energy> (gPower,noPersist)

Can you explain ? thanks in advance

Yeah, your case is different in that your meter doesn’t provide a timestamp. Mine is a DIY-project and so i actually send both the current wattage and the milliseconds since the last reading cycle as part of the MQTT message.
That’s what I made this Number item in OH for.

In your case i guess the only timebase you have is the system time when your rule is triggered, and when it last was.
In your case do you read momentary power (W) or energy (Wh)? And how often do you get these readings?
If it’s a fixed interval then you can just use that

thanks for your reply :slight_smile:

mine is a torus and read realtime wh , the rule is triggered at every change of the Item .
for exemple : Item ProdSolaire received update …that mean is very variable .
si i test actually this :

Item ProdSolaireKWH received update
 logInfo("increment", "ProdSolaireKWH raw data " + ProdSolaireKWH.state.toString)


ps i need to divide by 1000 to be near the real number “i can see on inverter” but i don’t understand really why , because the “ProdSolaireKWH” is converted to KWh “realtime” so i guess the /1000 is a chance…, difficult to find a clear way to convert realtime read to perday KWh/Day