Getting mapdb persistence to work with openhab2

Hi,

My transition to openhab2 has hit a roadblock getting persistence services to work.

I have mapdb installed (and running) and set as the default.

the mapdb.persist file reads:

Items {
// persist all items once a day and on every change and restore them from the db at startup
startpersist* : strategy = everyChange, restoreOnStartup
}

I then have a bunch of items in the (startpersist) group.

In openhab 1.8 this worked fine. However in 2.0 my items are not being persisted. I see no errors in the log. Not sure how to turn on debug logging for mapdb - if anyone knows how to do that then that would be really helpful

thanks,

Dan

Try on the karaf console:

log:set DEBUG org.openhab.persistence.mapdb

Thanks - I now have debug logging working. Unfortunately that gives no hint of what is wrong - the logs look fine as I start, stop and restart openhab2, although I would think I’d see log entries as items change and there don’t seem to be any:

2016-12-11 12:01:36.330 [INFO ] [core.karaf.internal.FeatureInstaller] - Installed 'openhab-persistence-mapdb'
2016-12-11 12:05:51.691 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'mapdb.persist'
2016-12-11 12:05:55.394 [DEBUG] [org.openhab.persistence.mapdb       ] - BundleEvent STARTING - org.openhab.persistence.mapdb
2016-12-11 12:05:55.396 [DEBUG] [org.openhab.persistence.mapdb       ] - BundleEvent STARTED - org.openhab.persistence.mapdb
2016-12-11 12:05:55.411 [DEBUG] [pdb.internal.MapDBPersistenceService] - mapdb persistence service is being activated
2016-12-11 12:05:55.702 [DEBUG] [pdb.internal.MapDBPersistenceService] - Scheduled Commit-Job with interval 5sec.
2016-12-11 12:05:55.703 [DEBUG] [pdb.internal.MapDBPersistenceService] - mapdb persistence service is now activated
2016-12-11 12:05:55.709 [DEBUG] [org.openhab.persistence.mapdb       ] - ServiceEvent REGISTERED - {org.openhab.core.persistence.PersistenceService}={service.pid=org.openhab.mapdb, commitinterval=5, component.name=org.openhab.persistence.mapdb, component.id=189, commitsamestate=false, service.id=304, service.bundleid=185, service.scope=bundle} - org.openhab.persistence.mapdb
2016-12-11 12:08:27.795 [DEBUG] [org.openhab.persistence.mapdb       ] - BundleEvent STOPPING - org.openhab.persistence.mapdb
2016-12-11 12:08:27.804 [DEBUG] [org.openhab.persistence.mapdb       ] - ServiceEvent UNREGISTERING - {org.openhab.core.persistence.PersistenceService}={service.pid=org.openhab.mapdb, commitinterval=5, component.name=org.openhab.persistence.mapdb, component.id=189, commitsamestate=false, service.id=304, service.bundleid=185, service.scope=bundle} - org.openhab.persistence.mapdb
2016-12-11 12:08:27.804 [DEBUG] [pdb.internal.MapDBPersistenceService] - mapdb persistence service deactivated
2016-12-11 12:08:27.812 [DEBUG] [pdb.internal.MapDBPersistenceService] - Found 1 MapDB-Jobs to delete from DefaulScheduler (keys=[MapDB_SchedulerGroup.Commit_Transaction])
2016-12-11 12:08:27.814 [DEBUG] [org.openhab.persistence.mapdb       ] - BundleEvent STOPPED - org.openhab.persistence.mapdb
2016-12-11 12:08:52.375 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'mapdb.persist'
2016-12-11 12:08:56.358 [DEBUG] [org.openhab.persistence.mapdb       ] - BundleEvent STARTING - org.openhab.persistence.mapdb
2016-12-11 12:08:56.360 [DEBUG] [org.openhab.persistence.mapdb       ] - BundleEvent STARTED - org.openhab.persistence.mapdb
2016-12-11 12:08:56.362 [DEBUG] [org.openhab.persistence.mapdb       ] - ServiceEvent REGISTERED - {org.openhab.core.persistence.PersistenceService}={service.pid=org.openhab.mapdb, commitinterval=5, component.name=org.openhab.persistence.mapdb, component.id=189, commitsamestate=false, service.id=304, service.bundleid=185, service.scope=bundle} - org.openhab.persistence.mapdb
2016-12-11 12:08:56.376 [DEBUG] [pdb.internal.MapDBPersistenceService] - mapdb persistence service is being activated
2016-12-11 12:08:56.603 [DEBUG] [pdb.internal.MapDBPersistenceService] - Scheduled Commit-Job with interval 5sec.
2016-12-11 12:08:56.603 [DEBUG] [pdb.internal.MapDBPersistenceService] - mapdb persistence service is now activated

Any help gratefully received - having been very comfortable with openhab 1.8 I’m rapidly feeling out of my depth here…

I remember reading somewhere that a “Strategy” has to be defined, regardless of using it or not, so try to change your file to:

Strategies {
        default = everyUpdate
}
Items {
// persist all items once a day and on every change and restore them from the db at startup
startpersist* : strategy = everyChange, restoreOnStartup
}
1 Like

brilliant - thank you. I’ll edit the wiki to make that point

1 Like

I’m trying to use mapdb persistence, but my virtual buttons keep turning off.

I have this in my runtime.cfg:

org.eclipse.smarthome.persistence:default=mapdb

This is my mydb.persist:

Strategies {
        default = everyUpdate
}

Items {
// persist all items once a day and on every change and restore them from the db at startup
StartPersist* : strategy = everyChange, restoreOnStartup
}

My items file has below:

Group StartPersist
Switch Daylight 		"Daylight" 		<sun> 			(Virtuals, StartPersist)
Switch Christmas 		"Christmas" 	<christmas> 	(Virtuals, StartPersist)
Switch Summer 			"Summer" 		<temperature> 	(Virtuals, StartPersist)

If I turn on my virtual switches in the app, then reboot my RPi, the switches are set to off again.

Looks all good to me.
Do you have any errors when saving your persist config file in openhab.log?
You should only see something like
Refreshing model 'mydb.persist'

Yes.

2018-05-24 15:21:01.511 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'mydb.persist'

Then I have no idea what is wrong.
Before restarting openHAB, did you switch your virtual items several times?
The first trigger will be from null to ON or OFF, maybe that one is not persisted. A second trigger then should store the actual state on everyChange.

Hmm, that still didn’t work. I can see the virtuals in the log being set to OFF and ON, but after I reboot, it still doesn’t keep the switches set to ON.

There is an issue where the first update of an item is not saved. Only when an item that already has state is updated the database is committed.

I’ve created a fix for it in this PR:

1 Like

I have serious issues getting this to work as well.
I installed it via the addons.cfg where i added mapdb to the persistence line.
Also i modified the runtime.cfg to contain: org.eclipse.smarthome.persistence:default=mapdb

After a restart the mapdb.cfg-File is generated but the mapdb.perist is not.
Only after i deleted the mapdb.config file from the user-folder and reinstalled again it worked.
(strange behaviour though …).
now when i store a value via rest for example i get a log message that sais: stored.
but if i restart now it does not recover the old state but says NULL …

is this the same issue described here?

I’d like to try the fix but i don’t know where to copy the files to.

Thank you

First: it is mapdb.persist (typo!) and no, it is not generated automatically, you need to add that file with your own content manually:

Don’t mess with the *.config files, those are automatically generated and not to be edited manually.

Hi guys,

I am trying to implement the persistence without succes.

Step followed:

  • mapdb installed in PAPER UI
  • file runtime.cfg added the org.eclipse.smarthome.persistence:default=mapdb
  • rule within mapdb.persist file. Is everyUpdate or everyChange?
Strategies {
        default = everyUpdate
}

Items {
// persist all items once a day and on every change and restore them from the db at startup
StartPersist* : strategy = everyChange, restoreOnStartup
}

I have tried as well that without success. All switches are in NULL.

And the more curiouse thing is that I have a sonoff DUAL where the status is ON and OFF, and I see properly the status in the sitemap, but other sonoff bassic which is in ON, the actual status is NULL in sitemap, should be ON. The rest os switches are NULL

I have restarted with sudo systemctl restart openhab2.service

Any suggestion? Better if I work with rules?
Thank you.

I have solved my issue after restart with the NULL status, with some rules. because mapdb did not work for me. So I used the HTTP binding, easier to check up the POWER of each switch :smiley:

Hi

I’m trying my hand at a persistence service now too.

Do I need to “install” anything OUTSIDE of openHAB2 to get mapDB working?

For example, I’ve found this GitHub - jankotek/mapdb: MapDB provides concurrent Maps, Sets and Queues backed by disk storage or off-heap-memory. It is a fast and easy to use embedded Java database engine.

From everything that I can find regarding mapDB with openHAB2, I “think” I only have to add the mapDB persistence service from within PaperUI, then set the default DB in configuration/services and add a mapdb.persist file.

BUT… that all seems FAR too simple.

The mapdb.persist file is loaded…

Strategies {
        default = everyUpdate
}

Items {
// persist all items once a day and on every change and restore them from the db at startup
StartPersist* : strategy = everyChange, restoreOnStartup
}

and I can see mapdb-1.0.9.jar in /usr/share/openhab2/runtime/system/org/mapdb/mapdb/1.0.9

Where would I see the log entries for each the the item is persisted?

(For example, I can’t see anything in /var/log/openhab2/openhab.log or /var/log/openhab2/events.log )

No.

One way to access is through RESTApi:

{
  "name": "QR1",
  "datapoints": "1",
  "data": [
    {
      "time": 1549781038612,
      "state": "OFF"
    }
  ]
}

Note that with MapDB only the last state is persisted, there are no more historic states:

Thanks for your uber fast response.

I would have happily waited a few days :smile:

That’s fine by me, I only want to restore things like radio station choices and step durations (that are stored in items)

Thanks :smile:

1 Like

Hi

Thanks for the pointers :slight_smile:
That really helps me understand.

I’ve looked at the rest API and I can see I do have a persistence service now :slight_smile:

But either I’m missing something, or I’ve done something wrong, as I can’t see the last state.

Unlike your example that clearly shows up
“time”: 1549781038612,
“state”: “OFF”

FYI

I did toggle the DiningRoom light a couple of times before I queried the rest API

Do I have to assign the items I would like persisting to a group of some kind?

My understanding of the mapdb.persist file is that it saves everything… (Not what I need, but I thought that would be a good starting point)

GROUPS…!!!.

I hadn’t assigned the items I wanted persisting to a GROUP >>>>

Items {
// persist all items once a day and on every change and restore them from the db at startup
StartPersist* : strategy = everyChange, restoreOnStartup
}

where StartPersist is the start of the name of the group that I had to assign.

(In theory, I guess I could have groups named “StartPersist” / “StartPersistLounge” / “StartPersistOutdoor” and so on.

Anyway, thanks for your help…

I now have things showing up in the RestAPI query :sunny:

2 Likes

I’ve got a problem storing data in my mapdb.persist file. It looks like mapdb.persist is started after reboot of my system:

20:55:11.322 [DEBUG] [org.openhab.persistence.mapdb        ] - BundleEvent STARTING - org.openhab.persistence.mapdb
20:55:11.322 [DEBUG] [db.internal.MapDBPersistenceActivator] - MapDB persistence bundle has been started.
20:55:11.322 [DEBUG] [org.openhab.persistence.mapdb        ] - BundleEvent STARTED - org.openhab.persistence.mapdb
20:55:11.337 [DEBUG] [apdb.internal.MapDBPersistenceService] - mapdb persistence service is being activated
20:55:11.541 [DEBUG] [apdb.internal.MapDBPersistenceService] - Scheduled Commit-Job with interval 5sec.
20:55:11.541 [DEBUG] [apdb.internal.MapDBPersistenceService] - mapdb persistence service is now activated
20:55:11.541 [DEBUG] [org.openhab.persistence.mapdb        ] - ServiceEvent REGISTERED - {org.openhab.core.persistence.PersistenceService}={service.id=420, service.bundleid=247, service.scope=bundle, component.name=org.openhab.persistence.mapdb, component.id=270} - org.openhab.persistence.mapdb

But looking in the database delivers no result:

{
  "name": "hobby_spots",
  "datapoints": "0",
  "data": []
}

What goes wrong?