openHAB Persistence Tutorial: Restore Items on Startup

Have you noticed that when you restart your openHAB server the value of your items gets lost? The reason is that by default, openHAB doesn’t store any value, this means that every time you restart the server you lose the status of every single one of them.

That sucks, especially when you have gadgets that don’t give updates on a regular basis like my MiLight Smart Lights.

As a result of this wonderful behavior, if I restart the server, the lights are ON and OFF at the same time…Like the Schrödinger’s cat 🙂

(This is just a completely unjustified way to add more cat pictures on the internet…)

OpenHab Persistence Tutorial: Cat

 

Schrödinger’s capabilities to take care of your cat when you are away might be subject of another post…for now let’s just focus on explaining what is openHAB Persistence and how it might be beneficial for your Home Automation project.

 

This post is part of a series of three pieces on openHAB Persistence:

  1. openHAB Persistence Part 1: Restore The Value if your Items on Startup.
  2. openHAB Persistence Part 2: Build Graphs Using InfluxDB and Grafana.
  3. openHAB Persistence Part 3: Using Persistence in your rules.

 

What is openHAB Persistence?

Persistence provides openHAB with the ability to store the value of some or all of your items on a timely basis so you can leverage those values later for different purposes.

How can you Benefit from Implementing openHAB Persistence?

There are mainly 3 use cases where implementing openHAB persistence can be a game changer:

  • Reload on Restart: The simplest and arguably the most useful one is to restore the value of your items after a restart. I think this is a must for any implementation.
  • Creating Charts for Data Analysis: A home Automation system can help you saving money, time and energy but you need to be able to analyze the data first. Charts are a great way of accomplishing  that.
  • Access to previous values in your rules:  The ability to access past data can be a very powerful feature. For example, if you know that the temperature in a room is trending up, you can choose not to trigger the heat and let nature do its thing, hence saving energy.

Related: 8 Ways to Save Energy with Home Automation

This is first one of a 3 post series about openHAB persistence. I will be covering every single one of the use cases that I mentioned above, starting with the first one today.

How to Restore openHAB Items on Startup?

openHAB persistence can be a very powerful tool, it can give you an insane amount of insights about what is going on at home, on the flip side, it can also be very resource-hungry.

But…what if you only need to restore the value of your items? There are many ways of implementing openHAB Persistence but if this is the only thing you need to accomplish, the majority of them are a massive overkill…

There is a middle ground in this case and it is called MapDB. Ten minutes of your time for a lifetime of benefit!

Restore the Value of your Items on Startup Using MapDB

Related: First Steps with openHAB and Home Automation

What makes MapDB so unique is that it only keeps one value at a time per item. It is not meant to be used for chart generation or to access previous values. It is very lightweight and the perfect solution for this use case.

Let’s get started!

Install the MapDB openHAB Persistence Service

The first thing that you need to do is to install the MapDB persistence service. In order to do that, go to Addons on the PaperUI and navigate to the option Persistence. 

 

OpenHab Persistence Tutorial: Install Binding

 

Select MapDB Persistence and click on Install as you would do with any other binding.

After the service is installed, you need to select MapDB as your preferred openHAB Persistence service. You need to do this if you have only one service.

This step is very simple, just to go to Configuration -> System and scroll down until you see a section called persistence. Here you can specify MapDB as your preferred option.

 

OpenHab Persistence Tutorial: Default Persistence Service

We are done with the installation and now it is time to configure the items you want to persist the value for.

Configure MapDB

PaperUI doesn’t support this part of the configuration so you will have to do some tinkering with the config files, don’t worry, it is not complex.

If you are running Openhabian you should already have a shared folder where you can access the configuration from your desktop computer. If you don’t have a shared folder you can always log into your Openhab server using SSH.

Shared Folder from Desktop: \\OPENHABIANPI\openHAB-conf\persistence

Path directly on the server: /etc/openhab2/persistence

On the persistence folder, you should see a file called mapdb.persist. If it isn’t there, don’t panic, just create an empty file with that name.

You have the file, now what? If you are looking for the quick and easy answer here it is.

Just paste the following content on the mapdb.persist file, restart the Raspberry Pi and you are good to go.

Are you still here? The configuration above covers the use case that I presented initially, using it you will get the value of your items restored every time you restart the server

If you are curious about why that configuration works and want to learn more about it stick around.

openHAB Persistence Tutorial

This section is not very relevant for MapDB although it will be crucial for the next two parts of the series.

The configuration file for MapDB can be broken down into two sections, strategies and items.

Strategies

A strategy defines when the value of a given item should be stored. You can define several strategies and apply them to the different items you want to store.

In the example above, I have configured 2 strategies (don’t worry about the default one yet), the first one will store the value of an item every hour, the second one will do it every day at midnight.

If you are wondering how the expression “0 0 0 * * ?” magically translates into “Execute something every day at midnight” you will need to do some learning about Cron expressions. I know…It doesn’t seem very intuitive at first…If you want to know how to create your own Cron expressions check this website Cron Expression Generator. It is pretty neat 🙂

The custom strategies are handy for very specific use cases but openHAB provides predefined ones that cover most scenarios.

Predefined Strategies:

  • everyChange: It persists the value of the item only when it changes.
  • everyUpdate: It persists the value of an item when it receives an update, even if the value hasn’t changed.
  • restoreOnStartup: This strategy is the whole objective of the tutorial, when you add it to an item or group of items, the value gets restored on startup.

everyChange and everyUpdate sound exactly the same for you? You are not the first one…

Here is an example to help you understand. if the difference seems obvious to you just skip this part.

Scenario 1: The temperature on your thermostat changes from 70 to 72 degrees.

Expected Behavior: Both strategies everyChange and everyUpdate will write the value of your item.

Scenario 2: The temperature on your thermostat receives an update with the value 70, but the temperature was already 70 before, it hasn’t changed.

Expected Behavior: Only if the strategy is everyUpdate the value will be stored as a new record.

I don’t really find many cases where the strategy everyUpdate can be useful, do you? let me know in the comments.

The default strategy in the definition above is pretty self-explanatory. If an item doesn’t have a strategy assigned, it will apply everyChange.

Items

This section defines the items that you want to persist the value for.

 

Let’s go through a few examples.

 

The start symbol (*) applies to all the items in your openHAB configuration.

 

The configuration above stores the value for 4 items only.

  • milight_entrance_color and milight_livingroom_color will use the everyChange strategy and will restore the values on startup.
  • milight_kitchen_color will use everyUpdate and will restore the value on startup.
  • mqtt_livingroom_temperature will use the default strategy.

 

In order to store the value of all the members of a group, you will specify it like this: <GroupName>*.

You may have noticed that I have added the value gSensors separately. The reason is that gSensors*  persists the value of the group members but not the group itself.

Let’s wrap out with a full example.

For the sake of clarity, I will add the comments directly in the config file.

 

As you can imagine, strategies like everyDayAtMidnight or everyTuesdayAtFive aren’t very useful using MapDB. Don’t worry, though, in the next post of the series I will be explaining how to capture historical information and create charts with it.

You may want to also check the openHAB Documentation on Persistence for more details.

Do you have ideas on how to use Persistence? Let me know in the comments.

Feature Image Designed by macrovector / Freepik

This Post Has 14 Comments

  1. Simon Thorsted

    Perfekt just what i was looking for , thanks a lot 🙂

  2. Simon Thorsted

    looking forward for the next chapter , i hope soon ?

  3. Simon Thorsted

    sorry not about this ,but i can see you have Mi-light in your openhab system can i please see how you do that
    sorry for my english and spelling i im Danish .
    I have a lot of Mi-light but i can
    t get it to be controlled be openhab2 ,if you can please show my how !

  4. David C.

    Hi Simon,

    Glad you liked it! I am wrapping up a post on how to create a Smart thermostat and then I will publish the next one in the series. It will probably be mid May 🙂

  5. David C.

    Don’t worry about your spelling. I am Spanish so this is from non native speaker to non native speaker!

    I imagined that you followed the tutorial but have gotten stuck somewhere.

    I see an email from you.I will reply and see what we can do 🙂

  6. Kai

    Thanks for the write-up! Just a small hint: The correct spelling is “openHAB”, not “OpenHab” 🙂

  7. David C.

    Hi Kai!

    Thanks a lot for the comment and for such an amazing project! I love openHAB!

    Thanks for the hint, I have changed the spelling for this article and I will review the rest during the weekend.

  8. Thomas

    Thanks for your solution for solving this type of problems, it really works.

  9. David C.

    Thanks for the comment Thomas!

  10. Christian

    Very good tutorial! Thanks for this.

  11. Dajazzz

    Very clear to understand. Thank you so much

  12. David C.

    Hi Jasper,

    Looks like I need to review that post based on the changes from the latest version. We you able to install it?

  13. Giuseppe

    That’s a crystal clear explanation. Now I’m gonna move to the part 2…

    Thanks.

  14. David C.

    Glad you liked it!

Leave a Reply