Restore items state after an openhab restart

My persistence service is mysql and openhab is 1.7
All values are stored in the datebase, i have checked this

If my light is “ON” and if I restart Openhab, my switch is not restored to “ON”.

Is there something to configure?

Yes, you have to use “restoreOnStartup”, take a look here.

Example:

Items {
    TestSwitch : strategy = everyMinute, restoreOnStartup
}

sorry, i forget to tell that i have done this allthoug, but it is not working

configurations/persistence/mysql.persist

Strategies {
default = everyChange
}
Items {

  • : strategy = default, restoreOnStartup
    }

Maybe you should try

Strategies {
    everyMinute    : "0 * * * * ?"
 }
Items {
   * : strategy = everyMinute, restoreOnStartup
}

and look what happens …

1 Like

ok greate this works, but now every Minute all values are written in the Database all day and night,

why is it not posible to only write down changes?

I don’t use mysql, but try exactly my example but replace everyMinute with everyChange and avoid the default strategy.
But this is just a guess…

like this?

Strategies {
everyChange : “0 * * * * ?”
}
Items {

  • : strategy = everyChange , restoreOnStartup
    }

Almost, but everyChange doesn’t need to be put in Strategies because it is already implemented by default, so I would try just a plain

Items {
   * : strategy = everyChange, restoreOnStartup
}

no, that is not working the same Problem as in the beginning

Sorry, then I have no more ideas …
I know when using rrd4j persistence you HAVE TO use everyMinute, but I don’t think that is the case with mysql …

is this working for everyone, our is no one using mysql?

Hi Stefan,

unfortunately I did not try mysql.

First I tried the logging persist to restore items on startup. This did not work for me because openHAB was not able to read values back. I don’t know if this is always the case or only on startup (there is some issue not in openHAB persist documentation: “Persistence services and rule engine are started in parallel. It may happen that rules are already executed using items that have not been persisted yet having an “undefined” state. Therefore, rules that rely on persisted information break during this time.”)

Now I use the rrd4j persistance and this is working for me as expected (persist on every change and restore on startup):

Strategies {
// for rrd charts, we need a cron strategy
everyMinute : “0 * * * * ?”
}
Items {
// store settings in rrd
gSettings* : strategy = everyChange, restoreOnStartup
}

Hope this helps!?

2 Likes

Wow! Carsten, I don’t understand why, but adding the everyMinute strategy to my mapdb.persist file fixed my restoreOnStartup problem.

Strategies {
    everyMinute : "0 * * * * ?"
    everyHour   : "0 0 * * * ?"
    everyDay    : "0 0 0 * * ?"

    default = everyChange
}
Items {
    PersistentValues* : strategy = everyChange, restoreOnStartup
}

I know this is an old thread but it seems the most appropriate. I’m using MYSQL database for persistence with the following settings:

// Persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {
	everyMinute	: "0 * * * * ?"

	// If no strategy is specified for an item entry below, the default list will be used.
	default = everyChange
}

/*
 * Each line in this section defines for which item(s) which strategy(ies) should be applied.
 * You can list single items, use "*" for all items or "groupitem*" for all members of a group
 * item (excl. the group item itself).
 */
Items {
	Presence* : strategy = default, restoreOnStartup
	Doors* : strategy = default, restoreOnStartup
	Battery* : strategy = default, restoreOnStartup
	Presist* : strategy = default, restoreOnStartup
	HVACRuntimeC : strategy = everyMinute
}

I checked the database and I’m getting plenty of values, but none of these are loaded when I restart. Is there a way to force a reload? I’m using 1.8.3.

I think there is a simple way to reload the value:
First of all I think your item definition isnt correct.

Should read

Items {
	Presence* : strategy = everyChange, restoreOnStartup
	Doors* : strategy = everyChange, restoreOnStartup
	Battery* : strategy = everyChange, restoreOnStartup
	Presist* : strategy = everyChange, restoreOnStartup
	HVACRuntimeC : strategy = everyMinute
}

or you change your default strategy to

default = everyChange, restoreOnStartup

and do not specify the strategy for these items…

but to come back to the restoreOnStartup Problem:
Add a strategy called everyDay (or week, month, year…) and put it into the items you want to restore:

So it would look like this:

// Persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {
	everyMinute	: "0 * * * * ?"
        everyDay        : "0 0 0 * * ?"

	// If no strategy is specified for an item entry below, the default list will be used.
	default = everyChange
}

/*
 * Each line in this section defines for which item(s) which strategy(ies) should be applied.
 * You can list single items, use "*" for all items or "groupitem*" for all members of a group
 * item (excl. the group item itself).
 */
Items {
	Presence* : strategy = everyChange, everyDay, restoreOnStartup
	Doors* : strategy = everyChange, everyDay, restoreOnStartup
	Battery* : strategy = everyChange, everyDay, restoreOnStartup
	Presist* : strategy = everyChange, everyDay, restoreOnStartup
	HVACRuntimeC : strategy = everyMinute
}

this additional “everyDay” seem to make the restoreOnStartup work.
So every day there will be an additional entry for the mentioned items in your database. But I guess thats ok. If not create a strategy like “everyYear”, then there will be only one additional entry per year.

br
Stefan

I have similar problem with MapDb. I have used restoreOnStartup since months without a problem. Since last few days I have configuring InfluxDB persistance and suddenly my mapdb stopped working :frowning:
I lost all evening reconfiguring all persistence as it was (in fact I just add influxdb binding and influxdb.persist), upgrading mapdb from 1.8.3 to 1.9.0. Deleting all other persistence and mapdb persist files and … nothing.
Then I just edited mapdb.persist file by comment default value and suddenly I got back all my items stage :slight_smile:
Then I did few more tries and each time when I update mapdb.persist I got data back after restart.
Anyway in my log I got an error:

> 2017-01-31 00:28:57.678 [INFO ] [c.internal.ModelRepositoryImpl] - Refreshing model 'mapdb.persist'
> 2017-01-31 00:28:58.220 [ERROR] [.o.m.c.i.folder.FolderObserver] - An unexpected exception has occured
> java.lang.AssertionError: data were not fully read, check your serializer 
> 	at org.mapdb.Store.deserialize(Store.java:299) ~[na:na]
> 	at org.mapdb.StoreDirect.get2(StoreDirect.java:475) ~[na:na]
> 	at org.mapdb.StoreWAL.get2(StoreWAL.java:336) ~[na:na]
> 	at org.mapdb.StoreWAL.get(StoreWAL.java:320) ~[na:na]
> 	at org.mapdb.Caches$HashTable.get(Caches.java:246) ~[na:na]
> 	at org.mapdb.EngineWrapper.get(EngineWrapper.java:58) ~[na:na]
> 	at org.mapdb.BTreeMap.get(BTreeMap.java:602) ~[na:na]
> 	at org.mapdb.BTreeMap.get(BTreeMap.java:589) ~[na:na] 

Is it some bug with mapdb ?

Have you tried deleting the persistence/mapdb folder? That worked for me in the past.

First I have tried to restore whole mapdb folder form last backup, where there were no issue. It didn’t help.
Then I deleted mapDb folder and it started working properly, up to now I have no idea what was the problem.