Hello!
I have an item the gives me the power consumptionTotal (accumulated consumption since power loss on Shelly Device) and i’d like to store power consumption on every update of this item.
approach: Shelly_EM3_consumption = Shelly_EM3_accTotal - Shelly_EM3_accTotal.previousState
Here’s some info on my setup
- Platform information:
- Hardware: Rpi 4/4GB
- OS: raspian (buster) / openhabianpi
- openHAB version: 2.5-10
and here’s my code:
Number Shelly_EM3_accTotal "Shelly em3 Gesamtverbrauch" (gShelly) {channel="shelly:shellyem3:xxx:device#accumulatedWTotal"}
Number Shelly_EM3_consumption (gShelly)
rule "Power Consumption EM3"
when
Item Shelly_EM3_accTotal changed
then
/// some logs to check the previous state
logInfo("NEW", Shelly_EM3_accTotal.state.toString)
logInfo("PREVIOUS MAPDB", Shelly_EM3_accTotal.previousState(false,"mapdb").state.toString)
logInfo("PREVIOUS INFLUXDB", Shelly_EM3_accTotal.previousState(false,"influxdb").state.toString)
Shelly_EM3_consumption.postUpdate((Shelly_EM3_accTotal.state as Number) - (Shelly_EM3_accTotal.previousState(false,"mapdb").state as Number))
end
apparently there’s something wrong with my code because:
2020-12-23 15:18:01.081 [vent.ItemStateChangedEvent] - Shelly_EM3_accTotal changed from 225.478 to 225.501
2020-12-23 15:18:01.659 [INFO ] [g.eclipse.smarthome.model.script.NEW] - 225.501
2020-12-23 15:18:01.665 [INFO ] [marthome.model.script.PREVIOUS MAPDB] - 225.501
2020-12-23 15:18:01.684 [INFO ] [thome.model.script.PREVIOUS INFLUXDB] - 225.501
shouldn’t previousstate be 225.478?
tried different presistence services… but mapdb should be ok, right?
mapdb: * : strategy = everyChange,restoreOnStartup
influxdb: gShelly* : strategy = everyChange, everyMinute
later, unchanged config:
2020-12-23 15:20:30.880 [vent.ItemStateChangedEvent] - Shelly_EM3_accTotal changed from 225.524 to 225.547
2020-12-23 15:20:30.890 [INFO ] [g.eclipse.smarthome.model.script.NEW] - 225.547
2020-12-23 15:20:30.901 [INFO ] [marthome.model.script.PREVIOUS MAPDB] - 225.547
2020-12-23 15:20:30.927 [INFO ] [thome.model.script.PREVIOUS INFLUXDB] - 225.524
influxdb this time gives back the previous state, mapdb doesn’t?
Shelly_EM3_consumption is not calculated as it should be according to my rule.
later again, same config:
2020-12-23 15:22:03.045 [vent.ItemStateChangedEvent] - Shelly_EM3_accTotal changed from 225.569 to 225.592
2020-12-23 15:22:03.064 [INFO ] [g.eclipse.smarthome.model.script.NEW] - 225.592
2020-12-23 15:22:03.073 [INFO ] [marthome.model.script.PREVIOUS MAPDB] - 225.592
2020-12-23 15:22:03.094 [INFO ] [thome.model.script.PREVIOUS INFLUXDB] - 225.569
2020-12-23 15:22:03.131 [vent.ItemStateChangedEvent] - Shelly_EM3_consumption changed from 0.000 to 0.023
influxdb has the right “previous” state (mapdb still doesn’t) and the calculation is done according to the rule…
now i don’t even know where to begin as there seem to be 2 problems:
- i’m not able to get the correct previousstate of my persisted item.
- there’s something wrong with my calculation.
any help would be appriciated because i’ve already lost (invested?) 3 hours for this (apparently not so) simple math problem…
best regards
Peter