Disclaimer

Please, proceed carefully following the tips published in this blog, specially when Main Power is involved. I'm not responsible for any damages caused by what is written in this blog.
Thank you

Monday, 6 November 2017

Home Automation System - Openhab persistence service with InfluxDB





In the last few days I studied the "persistence" service in OpenHAB v.2, just to store acquired data and be ready to create dashboards.

As usual, official docs are the best choice:


I chose InfluxDB as persistence service, a time series database, easy to integrate in Grafana, a web platform for dashboards creation.

The first thing to do is to install InfluxDB on the RPI3 (the same where OpenHAB is already running) following the official documentation https://docs.influxdata.com/influxdb/v1.3/introduction/installation/

Next, I have to enable the persistence service on OpenHAB v.2, using PaperUI, Add-ons section, Persistence tab


Then, OpenHAB must be configured with the InfluxDB connection info, editing services/influxdb.cfg file in the usual OpenHAB configuration folder. In my case, I used all the default options, so I just entered the password.

This is my configuration file:

# The database URL, e.g. http://127.0.0.1:8086 or https://127.0.0.1:8084 .
# Defaults to: http://127.0.0.1:8086
# url=http(s)://<host>:<port>

# The name of the database user, e.g. openhab.
# Defaults to: openhab
# user=<user>

# The password of the database user.
password=XXXXXXX

# The name of the database, e.g. openhab.
# Defaults to: openhab
# db=<database>

On the official guide you can find all the optionshttp://docs.openhab.org/addons/persistence/influxdb/readme.html

Now it's time to build the persistence strategy to let OpenHAB knows how store data on InfluxDB; in a few words I have to decided, item for item, when datas will be stored on db. I can choose between many options: on every change, on every update or by time schedule, using Quartz syntax. To do so, I created a new configuration file in the /etc/openhab2/persistence folder, called influxdb.persist. In this file I defined which kind of persistence strategy I will use (Strategies section...) and assigned them to items and groups (Items section).


Official docs:

My Strategies section is as follow:

Strategies {
everyMinute: "0 * * * * ?"
everyHour: "0 0 * * * ?"
everyDay: "0 0 0 * * ?"
default = everyUpdate, restoreOnStartup
}


I used groups to assign the persistence strategies to items; so I defined new groups in /etc/items/groups.items file

Group gPersistence (gAll)
Group gPersistence1m (gAll)
Group gPersistence1h (gAll)
Group gPersistence1d (gAll)
Group gPersistenceChange (gAll)

then I'll assign them the persistence strategy just defined in the Items section of the influxdb.persist file

Items {
gPersistence* :
gPersistence1d* : strategy = everyDay, restoreOnStartup
gPersistence1h* : strategy = everyHour, restoreOnStartup
gPersistence1m* : strategy = everyMinute, restoreOnStartup
gPersistenceChange* : strategy = everyChange, restoreOnStartup
.........
}

I will easily assign these groups to items to assign a particular persistence strategy

In the next post I will speak about Grafana, a powerful tool to build dashboard.