With the help of InfluxDB you can tackle many exciting projects with openHAB. Basically, the InfluxDB is used to store simple time series. This means that you can store any state or change of an item (or all members of a group) of openHAB in a database and have access to historical values at any time. This extends the possibilities of your smart home installation, as you can also use the historical values. A simple example is if the fan control based on the temperature of your sensors in the last hours. Another possibility is an appealing visualization of the temperature trend over the day using Grafana.
Installation of InfluxDB
With the help of InfluxDB you have a database optimized for speed with time series and thus for the use of sensor data.
Loading the openHAB image of Docker Hub
As a base for the installation of InfluxDB you first have to download the image from the official site of Docker Hub. In the container station you can download the image with one click or you can use the following command:
docker pull influxdb
Folder creation
To allow storage of data outside of Docker, you first have to create the following folder on your system
influxdb
Creation of the InfluxDB container
Afterwards you can create the container via the container station or the correct docker commands. Since you will need the IP address and the port, you should cache these values for a short time.
Setup of InfluxDB
To be able to store single data from your openHAB instance in the InfluxDB, you have to set up your InfluxDB in the first moment – the code for this is:
influx
CREATE DATABASE openhab_db
CREATE USER admin WITH PASSWORD 'admin' WITH ALL PRIVILEGES
CREATE USER openhab WITH PASSWORD 'openhab'
CREATE USER grafana WITH PASSWORD 'grafana'
GRANT ALL ON openhab_db TO openhab
GRANT READ ON openhab_db TO grafana
With this code you create the necessary database and create users with different rights to view this database. As you will visualize the data with Grafana in the following tutorial, you will create a Grafana user in lines 5 and 7. If you do not want to do this, you can simply delete these lines.
Link to openHAB
In the next step you configure openHAB to store the data of items or groups in the InfluxDB.
In the first step you install the extension for the InfluxDB in openHAB. You will find the necessary line in the file “conf/services/addons.cfg”:
persistence = influxdb
Afterwards you have to enter the credentials to allow openHAB to access the InfluxDB. This is done in the file “conf/services/addons.cfg”:
url=http://IP_ADRESSE:PORT
user=openhab
password=openhab
db=openhab_db
retentionPolicy=autogen
The last step is to tell openHAB which items you want to save at what interval. Here you can add items at any time to store them in your InfluxDB:
Strategies
{
everyFiveMinutes : "0 0/5 * * * ?"
default = everyChange
}
Items
{
ITEM_NAME : strategy = everyChange
GROUP_NAME* : strategy = everyFiveMinutes
}
With the above code, the item with the name ITEM_NAME is stored in the InfluxDB with every change. Furthermore all items of the group named GROUP_NAME will be saved in the InfluxDB every five minutes.
Check the data records in InfluxDB
A check of the data records is possible via the shell within the container. In the container station you only need to click from the terminal followed by the command “/bin/sh”.
Alternatively you can start the shell with the following command:
docker run -it –rm CONTAINER_NAME /bin/sh
Afterwards you can use the following commands to view your saved items in order:
influx
USE openhab_db
SELECT * FROM ITEM_NAME
Further outlook
In the introduction I have already revealed some possibilities for your openHAB installation. I often use the InfluxDB for visualization using Grafana and the Habpanels in openHAB. Therefore I will explain the installation in the next article.
I hope you continue to enjoy reading!
Matthias