Setup Persistence with UI in OH4

In my OpenHabian 4.0.3 system, I have a number of numeric variables that revert to NULL when my raspberry pi is restarted after a power failure. I am trying to use the UI to setup MapDB as a persistence service to maintain the values on restarting.

  1. I have installed MapDB from the Addons page and opened the configuration dialogue.

  1. This is the code generated for the three variables - 2 are numerical values and the third is a switch.
  - items:
      - IrrDays1
      - IrrigationEnable
      - IrrTime1
      - everyChange
      - restoreOnStartup
    filters: []
  - name: everyMinute
    cronExpression: 0 * * ? * *
  - name: everyHour
    cronExpression: 0 0 * * * ?
  - name: everyDay
    cronExpression: 0 0 0 * * ?
  - everyChange
  - restoreOnStartup
thresholdFilters: []
timeFilters: []
equalsFilters: []
includeFilters: []

  1. I saved the configuration and set the default persistence service to MapDB.

  2. I changed the values of the three items to ensure that the new values are recorded in the database.

  3. After a few minutes, I turned off the power to the raspbery pi and then restarted it again.

  4. The switch has reverted to OFF and the two numeric values are both NULL.

What am I doing wrong?


The fact that the switch is OFF is interesting. If expect it to be NULL unless it’s linked to a Channel. In that case, the binding is always going to override the restored value with the actual state of the device anyway to restoring that Item probably isn’t doing anything for you.

Using the API Explorer you can query MapDB and see if it’s actually saved values for those Items. If not there must be something up with your config. I don’t have a lot of experience configuring Persistence through the UI so all I can say on that front is it looks right.

I am afraid I not a Unix person. Can I use the API Explorer from Windows? Could you please give me a hint on how to access the MapDB database.


Yes, as it is part of the UI.
There is not much documentation but see openHAB REST API | openHAB how to get it started.

From MainUI → Developer Tools → API Explorer.

Scroll down to the persistence end point.

OK, I have worked out how to use MainUI → Developer Tools → API Explorer.

  1. Get / Persistence correctly shows both rr4j and mapdb.

  2. Get / Persistence / mapdb correctly shows the config file that I showed above.

  3. Get / Persistance / items shows the following

    "name": "IrrigationEnable",
    "state": "OFF",
    "timestamp": "2023-10-07T09:29:30.000+0200"
    "name": "IrrTime1",
    "state": {
      "value": 15
    "timestamp": "2023-10-07T09:29:26.000+0200"
    "name": "IrrDays1",
    "state": {
      "value": 2
    "timestamp": "2023-10-07T09:29:15.000+0200"

The values for the three variables are correct and change when I alter then in the UI. So far so good, the value of the 3 three variables are being saved in the database.

  1. Unplug the raspberry pie and restart. The Get / Persistance / items just shows [ ] with no values, so the database is being erased when the power is cut, hence the values are set NULL and OFF. Changing the values in the UI, correctly saves the new values in the database.

Seems as if the database is being held in memory, rather than being written to the SD card. If this is the case, how do I fix it?


Did you properly shutdown the Pi ?
If not, you will definitely loose your persistence, as it is written to zram AFAIK.

No I did not properly shut down the Pi as I am trying to guard against a power failure, not an orderly shut down. I do have a battery backup supply for the Pi which will see me over a hour or so, but If I am away from the house or on vacation, the supply will fail.

If the MapDB is written to zram by default as you suggest, is there any way to force it to write to the SD card at the same time?


It looks as if the Zram and database issue has been addressed here

I already have a spare 500GB SSD so I have ordered a neat Argon One M.2 Case for the Pi which includes M.2 mounting board. Once I have this working, I may need to come back for help if I cannot figure out the UNIX commands to migrate the database files.


proper shutdown is required as during the shutdown ZRAM will be stopped and content will be writen to the underlying filesystem.

If its failing it will not help against a power failure. A low battery needs to trigger a normal shutdown.
If the battery fails and the power causes an immediate shutdown of the Pi your SD card is at risk of getting a corrupted filesystem.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.