[SOLVED] How to restore state in rules using persistence

Hi, I’m trying to retrieve a value from persistence using db4o in a rule but I have some problems.

I have an Item in a new binding and I want to restore it. From console I see that value is stored in db4o but I don’t know how read from db the last value.

Item PollerSwitch1 received command ON
logDebug(“REGOLA”, dimmer1.state.toString)
logDebug(“REGOLA1”, dimmer1.persist as String)
sendCommand(dimmer1, dimmer1.persist as DecimalType)

From debug I see:
dimmer1.persist is null
dimmer1.state is right value
dimmer1.lastUpdate I see the date of last update (correct)
dimmer1.previousState I see date and state

How can I send command and write dimmer1 state with persist value?

Thanks for your help

Take a look at the persistence extensions section in the wiki. However, if you set your persistence strategy to be everyChange then I would expect the last value in the persistence, and the current state of the item to be the same.


Hi Chris thanks for your reply, I see persistence extensions but why .persist value is null?
Sorry but I’m not a programmer so for me is not so easy to understand the problem.


The wiki says the following -:

<item>.persist - Persists the current state

So this is a command to allow you to persist a value into the database - not to read the last value. You probably want to look at using historicState() or previousState().

Exactly! So now I have understand the problem!

But sendCommand(dimmer1, dimmer1.previousState as DecimalType) is not correct, can you help me to extract only state value from previousState?

Thanks a lot

Note that previousState and historicState are methods - so they need to be called with a time -:




or something similar…

Thanks Chris, but I can’t able to find a solution to this problem…

I try to explain better the problem:

rule "memory test"
 	Item PollerSwitch1 received command ON
	logDebug("REGOLA", dimmer1.state.toString)
	logDebug("REGOLA1", dimmer1.historicState(now).toString)
	sendCommand(dimmer1, dimmer1.historicState(now).toString)

in the console I see:

11:20:48.613 [DEBUG] [rg.openhab.model.script.REGOLA:38   ] - 23
11:20:48.650 [DEBUG] [g.openhab.model.script.REGOLA1:38   ] - 29-ago-2015 11.00.00: dimmer1 -> 23

So I need to do this… sendCommand(dimmer1, 23)

dimmer1.historicState(now) give me a string and I can’t use as DecimalType. I’m sorry but I’m not a programmer and I don’t know ho to “extract” 23 from dimmer1.historicState(now) method.

Thanks a lot for you precious time.

Try -:

sendCommand(dimmer1, dimmer1.historicState(now).state)

Using OH Designer I see an error with incompatible type, so I didn’t try… So I have a X red on designer but works perfect on openhab console!!!

Thanks a lot!

Great :smile:

For simple rules like this, you might want to try HABmin block designer. I use it for most of my rules (which are generally relatively simple)…

produces the following rule -:

// This rule file is autogenerated by HABmin.
// Any changes made manually to this file will be overwritten next time HABmin rules are saved.

// Imports
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

// Global Variables

rule "New Rule"
  sendCommand(command, Item.historicState(now.minusSeconds(0)).state)

(note that there’s no when clause in the above since the Item doesn’t exist in my configuration, but normally this is added as well).

WOW Super!
I will start to use it!

Thanks a lot again.