Rule for storing total consumption on power loss in proxy item

  • Platform information:
    • Hardware: intel
    • OS: ubuntu20.04 lts
    • Java Runtime Environment: Java™ SE Runtime Environment (build 1.8.0_201-b09)
    • openHAB version: 2.5.7

Hello there
:slight_smile:

persistence service is RRD4J

i implemented this rule as discussed in

2 items:

Number RotexHPSULwpTotalKWH
        "ROTEX HPSU LWP SHELLY Verbrauch [%.3f kWh]"
        <energy>
        (gRotexHPSU_Total)
        { channel="shelly:shellyem:xxxxxxxxxxxxxxx:meter1#totalKWH" }

Number RotexHPSULwpTotalKWH_persist
        "ROTEX HPSU LWP SHELLY Verbrauch PERSIST [%.3f kWh]"
        <energy>
        (gRotexHPSU_Total)

rule:

rule "ShellyEM rotex LWP PowerLoss"
when
    Item RotexHPSULwpTotalKWH changed
then

//    logInfo("ShellyEM rotex LWP PowerLoss", "RotexHPSULwpTotalKWH changed")
//  neu  0 kWh   :  alt 55.432 kWH

if (RotexHPSULwpTotalKWH_persist.state == NULL)  // persistent neu
{
    var Number newTotal = (RotexHPSULwpTotalKWH.state as DecimalType)
    // store the current value in "persist"
    RotexHPSULwpTotalKWH_persist.postUpdate( newTotal )
    logInfo("ShellyEM rotex LWP PowerLoss", "persistent missing totalKWH  " + (newTotal.toString) )
}
else
{
   // calc the delta and add it in "persist"

if ( RotexHPSULwpTotalKWH.previousState() != NULL)   // is there persistence to compare old and new?
{

logInfo("ShellyEM rotex LWP PowerLoss", "get delta prev LwpTotalKWH" )
logInfo("ShellyEM rotex LWP PowerLoss", "current "
   + String.format("%.4f",  (RotexHPSULwpTotalKWH.state as                 DecimalType).floatValue ) + " prev: "
   + String.format("%.4f",  (RotexHPSULwpTotalKWH.previousState().state as DecimalType).floatValue ) )
var float delta = (RotexHPSULwpTotalKWH.state as DecimalType).floatValue - (RotexHPSULwpTotalKWH.previousState().state as DecimalType).floatValue
logInfo("ShellyEM rotex LWP PowerLoss", "delta: " +    String.format("%.4f",  delta ) )
if ( delta < 0.0 ) {
    logInfo("ShellyEM rotex LWP PowerLoss", "power lost ! New RotexHPSULwpTotalKWH_persist is base : " + (RotexHPSULwpTotalKWH_persist.state as N
    delta = (RotexHPSULwpTotalKWH.state as DecimalType).floatValue
} else {
    logInfo("ShellyEM rotex LWP PowerLoss", "normal RotexHPSULwp-totalKWH  " + (RotexHPSULwpTotalKWH.state as DecimalType).toString + " persist o
}

RotexHPSULwpTotalKWH_persist.postUpdate( (RotexHPSULwpTotalKWH_persist.state as DecimalType).floatValue + delta)
    logInfo("ShellyEM rotex LWP PowerLoss", "persist new: " + (RotexHPSULwpTotalKWH_persist.state as Number).toString)

}  // RotexHPSULwpTotalKWH previous  NULL
else
{
    logInfo("ShellyEM rotex LWP PowerLoss ",  "RotexHPSULwpTotalKWH.previousState is NULL - no delta by now")
}
}

end

in general it is working:

  • if persistence of RotexHPSULwpTotalKWH_persist deleted and OH restarted then RotexHPSULwpTotalKWH_persist will be set to current value
  • if RotexHPSULwpTotalKWH reset in SHELLY device to zero then this will be noted

however, calculation of “delta” of current state to previousState().state is sometimes wrong by one unit “Wh”

delta is “0.000” instead of “0.001” or “0.004” instead of “0.003” e.g.

2020-08-27 14:27:01.276 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH changed from 0.013 to 0.014
2020-08-27 14:27:01.286 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH_persist changed from NULL to 0.014

2020-08-27 14:27:01.283 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - persistent fehlte totalKWH  0.014
2020-08-27 14:30:01.058 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH changed from 0.014 to 0.015
2020-08-27 14:30:01.063 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - get delta prev LwpTotalKWH
2020-08-27 14:30:01.066 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - delta: 0.001
2020-08-27 14:30:01.071 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH_persist changed from 0.014 to 0.015
2020-08-27 14:30:01.072 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - normal RotexHPSULwp-totalKWH  0.015 persist: 0.015
 
--> SAME OK


2020-08-27 14:31:00.412 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - get delta prev LwpTotalKWH
2020-08-27 14:31:00.413 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH changed from 0.015 to 0.018
2020-08-27 14:31:00.416 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - delta: 0.003999999
2020-08-27 14:31:00.421 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - normal RotexHPSULwp-totalKWH  0.018 persist: 0.015
2020-08-27 14:31:00.423 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH_persist changed from 0.015 to 0.018999998

--> STRANGE 0.004 instead of 0.003

2020-08-27 14:31:59.428 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH changed from 0.018 to 0.020
2020-08-27 14:31:59.439 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH_persist changed from 0.018999998 to 0.020999998
2020-08-27 14:31:59.432 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - get delta prev LwpTotalKWH
2020-08-27 14:31:59.435 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - delta: 0.0020000003
2020-08-27 14:31:59.439 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - normal RotexHPSULwp-totalKWH  0.020 persist: 0.020999998

--> DELTA  0.002 OK


2020-08-27 15:34:00.621 [vent.ItemStateChangedEvent] - RotexHPSULwpTotalKWH changed from 0.097 to 0.098
2020-08-27 15:34:00.792 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - delta: 0.0
--> STRANGE 0.000 delta shall be 0.001

2020-08-27 15:34:00.794 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - normal RotexHPSULwp-totalKWH  0.098 persist: 0.100999996

noting that the first time and many time in between it is working - i.e. proxy persistence item value and orignal value are same (if started from zero)

but why not working ? effect directly visible after 30mins reproducible
concurrency?
using wrong previousState( false/true)???

using RRD by now - wanting to use MySQL

thanks in advance,

BR/
Peter

chart-persist

See my reply in you original post.

Hello Rich,

thanks a lot for the comprehensive answer

will switch to BigDecimal. However, i think the mantissa is not long (number is not 0.123456789 but e.g. 0.98)

BR/
Peter

PS:
the other day i switched from RRD4J to MYSQL persistence service
there were no problems the first 3 hours (compared to RRD where after 30min there was already a huge difference)

2020-08-28 18:06:06.544 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - current 1,0700 prev: 1,0690
2020-08-28 18:06:06.549 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - delta: 0,0010
2020-08-28 18:06:06.551 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - normal RotexHPSULwp-totalKWH 1.070 persist old: 1.069
2020-08-28 18:06:06.553 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - persist new: 1.069
2020-08-28 18:10:00.334 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - get delta prev LwpTotalKWH
2020-08-28 18:10:00.900 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - current 1,0710 prev: 1,0710
2020-08-28 18:10:00.905 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - delta: 0,0000
2020-08-28 18:10:00.907 [INFO ] [.script.ShellyEM rotex LWP PowerLoss] - normal RotexHPSULwp-totalKWH 1.071 persist old: 1.07

so , there was an update (value changed) at 18:10:00.900 but the previous value is printed as 1,071 unstead of 1,070 (see 18:06:06) . in the result the delta is “0.000” and the persistence proxy item is one off.

So, will switch to BigDecimal.

i’m assuming some problems in the persistence service (or something with “previousState()” element. ?!?

BR/
Peter