[SOLVED] Mapdb always getting the same value back instead previous

I have enabled the mapdb persistence service.
When I call item.previousState().state I get always the same value as the current one back.

My mapdb.persist file looks like this:

Strategies {
    default = everyChange
}

Items {
    Bathroom_Temperature_tado : strategy = everyChange
}

My rule:
rule “Bathroom Temperature is lower than Target Temperature”
when
Item Bathroom_Temperature_tado received update
then

logInfo("Bathroom", "Temperature was changed, checking....")

logInfo("Bathroom", "Previous value was " + Bathroom_Temperature_tado.previousState(true).state)
logInfo("Bathroom", "Current value is " + Bathroom_Temperature_tado.state as Number)

end

The log file:
2019-03-28 10:48:36.648 [INFO ] [ipse.smarthome.model.script.Bathroom] - Temperature was changed, checking…
2019-03-28 10:48:36.701 [INFO ] [ipse.smarthome.model.script.Bathroom] - Previous value was 22.68
2019-03-28 10:48:36.702 [INFO ] [ipse.smarthome.model.script.Bathroom] - Current value is 22.68 °C

To prove your case, you could show two runs of the rule? Or compare with implicit previousState?

This thread looks related

Log of two runs of the rule:
2019-03-28 10:46:06.211 [INFO ] [ipse.smarthome.model.script.Bathroom] - Nothing to do.
2019-03-28 10:46:06.212 [INFO ] [ipse.smarthome.model.script.Bathroom] - Previous value was 22.44
2019-03-28 10:46:06.213 [INFO ] [ipse.smarthome.model.script.Bathroom] - Current value is 22.44 °C
2019-03-28 10:46:36.322 [INFO ] [ipse.smarthome.model.script.Bathroom] - Temperature was changed, checking…
2019-03-28 10:46:36.324 [INFO ] [ipse.smarthome.model.script.Bathroom] - Nothing to do.
2019-03-28 10:46:36.325 [INFO ] [ipse.smarthome.model.script.Bathroom] - Previous value was 22.68
2019-03-28 10:46:36.326 [INFO ] [ipse.smarthome.model.script.Bathroom] - Current value is 22.68 °C

The linked thread solution was to use the implicit variable previouseState but in my case this does not solve it. I get the same values as before.

logInfo("Bathroom", "Previous State: " + previousState(Bathroom_Temperature_tado).state);
logInfo("Bathroom", "Previous State2: " + Bathroom_Temperature_tado.previousState.state);
logInfo("Bathrooom", Bathroom_Temperature_tado.previousState.toString);
logInfo("Bathroom", "implicit " + previousState.toString)

The implicit state belongs to the rule, not the Item.

However I notice in your case it’s not going to work anyway. implicit previousState only exists for rules with changed triggers, and yours isn’t.

Which is the solution I missed of course … your rule runs on Item update, and Items can receive updates to the same value.

If you want your rule to run when the Item changes, trigger it from Item xx changed

So I have changed the trigger to “changed” and added the implicit variable.

Now it is working.
Great, thank you very much!!

But I have a side question:
Do I need the persistence service mapdp for this?

No persistence is needed for any of the implicit variables.
You might well want it for other reasons (restore on startup for mapdb)

EDIT - when you make your real rule, account for previousState being NULL for the first update after booting system.

2 Likes

Another point:

mapdb persistence will only keep the current state, not the previous state. If you want historic data, you’ll have to use another persistence service such as rrd4j, influxdb, mysql…

3 Likes