Would you expect the persistence data for an Item to lag behind the state change?

I have the state of my heating system as an Item (0 or 1)

I have a rule which triggers on:
Item Heating changed

I am trying to delay doing anything in the rule if the state has only just changed as I actually need a more complex rule than show below.

I am therefore inspecting the following


Cut down rule here:

rule "heating_state"
    Item Heating changed
        logInfo("heatingRule","Heating State: {}", Heating.state)    
        logInfo("heatingRule","Last heating change: {}", Heating.lastUpdate())
        logInfo("heatingRule","Previous heating state: {}", Heating.previousState().state)

Logs show that sometimes the Heating.state matches Heating.previousState().state but sometimes it doesn’t and has the previous value, and sometimes lastUpdate() corresponds to the time the rule was triggered, and sometimes it is the previous change.

2017-12-08 20:45:11.113 [INFO ] [e.smarthome.model.script.heatingRule] - Heating State: 1
2017-12-08 20:45:11.115 [INFO ] [e.smarthome.model.script.heatingRule] - Last heating change: 2017-12-08T20:45:11.077Z
2017-12-08 20:45:11.118 [INFO ] [e.smarthome.model.script.heatingRule] - Previous heating state: 1.0

2017-12-08 20:45:25.480 [INFO ] [e.smarthome.model.script.heatingRule] - Heating State: 0
2017-12-08 20:45:25.482 [INFO ] [e.smarthome.model.script.heatingRule] - Last heating change: 2017-12-08T20:45:11.077Z
2017-12-08 20:45:25.484 [INFO ] [e.smarthome.model.script.heatingRule] - Previous heating state: 1.0

2017-12-08 20:45:39.546 [INFO ] [e.smarthome.model.script.heatingRule] - Heating State: 1
2017-12-08 20:45:39.548 [INFO ] [e.smarthome.model.script.heatingRule] - Last heating change: 2017-12-08T20:45:25.480Z
2017-12-08 20:45:39.551 [INFO ] [e.smarthome.model.script.heatingRule] - Previous heating state: 0.0

2017-12-08 20:45:53.388 [INFO ] [e.smarthome.model.script.heatingRule] - Heating State: 0
2017-12-08 20:45:53.390 [INFO ] [e.smarthome.model.script.heatingRule] - Last heating change: 2017-12-08T20:45:39.546Z
2017-12-08 20:45:53.393 [INFO ] [e.smarthome.model.script.heatingRule] - Previous heating state: 1.0

It will take a few milliseconds for a state change to be written to persistent storage. If you look through example rules, you’ll find a Thread::sleep(100) is often used to delay the action so that persistence can finish up. Depending on the hardware that OH is running on, more time may be needed. Try your test rule with a sleep and see if that improves things.

Thank you very much for your response, I’ll try the sleep, and I’m sure
that will help from what I’ve observed.

I wanted to check that I wasn’t doing something fundamentally wrong.