[SOLVED] Retain State when setting Item state in rule file

Hi all,

Can someone advise how to retain the state of a contact when I am setting the state in the rules file. I will explain here what I mean. I have a Sonoff RF Bridge with Tasmota and a whole lot of different 433 MHz sensors, Water, Smoke and door sensors. Using JSONPATH I extract the ‘Data’ value from the JSON and use it in the rules file to set the state of the door. My door sensors send an OPEN and CLOSE code.


My rule looks like this

 rule "RF Code"
     Item RFBridge changed
     var rfData = RFBridge.state.toString
         case "1234E": {
             FrontDoor.state = OPEN

This all works, but if Openhab restarts or occasionally I think jetty just restarts itself the the Basic UI does not show the state anymore.

To maintain the previous values after a restart you’ll need some persistence mechanism to store the values.

This topic explains it step by step: InfluxDB+Grafana persistence and graphing

Also I think it is better to set a new state with the sendCommand method instead of assigning a value to the state: FrontDoor.sendCommand(OPEN)

1 Like

For restoring the state of items after a restart of openHAB use the persistence service with mapDB, this database is build for something like that. It holds only one value (the last) for each persisted item. Setting it up with te restoreOnStartUp policy will do the job in case of a restart.
It doesn’t help for the jetty Restarts. However those restarts sound a bit odd, you’d better solve the cause of those instead using a work-around to overcome this issue.

Thank you for the quick response @opus and @avdlee.
I will have a look at implementing your suggestions tonight.

Although that appears to work, it is undocumented.

You should use FrontDoor.postUpdate(OPEN)

Not command - command is different from state, although it may well result in a state change eventually. And in any case you cannot send commands to a “read only” Contact type Item.

At startup, all Items are set to special state NULL, unless something else happens. That could be an update from a binding, or a rule, or as you want here, from the persistence restore action.
Items can also get set NULL when editing stuff, which is likely your other occasions.

Thank you @rossko57 much appreciated. I will change it tonight to a postUpdate. I have read on on the differences now.

Thanks to all for the advice today. So using mapdb worked like a charm, and changing to postUpdate as well.
Thank you