openHAB2 & MySQL persistence setup

The following tutorial is an example how I got MySQL persistence working in my environment.

  1. Install “MySQL Persistence” via PaperUI

  2. Install and configure MySQL:

sudo apt-get install mysql-server

Here, it will ask for a root password, saying that “While it is not necessary, it is suggested.” Of which it is always a good idea to include a root password. Make sure, of course, to remember this password as well.

Start the MySQL commandline as root

sudo mysql -u root -p

Create a database for OpenHAB

CREATE DATABASE OpenHAB;

Create a user for OpenHAB

CREATE USER 'openhab'@'localhost' IDENTIFIED BY 'YOURPASSWORD';

Grant the user permissions for the database

GRANT ALL PRIVILEGES ON OpenHAB.* TO 'openhab'@'localhost';

Quit the Mysql command prompt

quit
  1. Restarted openHAB Service and found or created (don’t know anymore) config file under
/etc/openhab2/services$

My mysql.cfg looks like the following (shotened to the important sections):

# the database url like 'jdbc:mysql://<host>:<port>/<database>' (without quotes)
url=jdbc:mysql://127.0.0.1:3306/OpenHAB

# the database user
user=openhab

# the database password
password=YOURPASSWORD
  1. Create a persistence file under
/etc/openhab2/persistence$

Mine is called “mysql.persist” and it looks something like this:

// persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {
	// if no strategy is specified for an item entry below, the default list will be used
	everyMinute	: "0 * * * * ?"
	every5Minutes : "0 */5 * * * ?"
   	everyHour   : "0 0 * * * ?"
   	everyDay    : "0 0 0 * * ?"
   	default = everyChange
}

/* 
 * Each line in this section defines for which item(s) which strategy(ies) should be applied.
 * You can list single items, use "*" for all items or "groupitem*" for all members of a group
 * item (excl. the group item itself).
 */

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

    // additionally, persist all temperature and weather values every hour
       gTemperatur* : strategy = every5Minutes, restoreOnStartup

I know the persistence file is not optimized and is logging too many values - this is work in progress.

So this works for me and I can login to the MySQL Instance and see the values logged to the tables.

My feedback, corrections or additions welcome. Hope this helps.

Cheers
Christian

14 Likes

Thank you for this tut … but how do I get the persisted data displayed?
Best Gerrit

It’s not always about the roundtrip. I believe Christians motivation was to have his item status history in a database that he can access with a wide range of tools or his own implementations easily.

If you are looking to display something you are free to do this in the next step. There are many tools to generate statistics out of MySQL databases for example…

My primary aim was to persist my values. But I’ve tested around charting these values on the sitemap as well.

Here an example from my sitemap:

Frame label="Charts"{
	Text label="Temp. EG" icon="groundfloor" {
	Switch item=Chart_Intervall label="Zeitraum" icon="clock" mappings=[0="Stunde", 1="Tag", 2="Woche"]
    	Chart item=gTempChartEG service="mysql" period=h refresh=300 visibility=[Chart_Intervall==0, Chart_Intervall=="Uninitialized"]
    	Chart item=gTempChartEG service="mysql" period=D refresh=300 visibility=[Chart_Intervall==1]
    	Chart item=gTempChartEG service="mysql" period=W refresh=300 visibility=[Chart_Intervall==2]
    }
 }

And here the corresponding items:

 Number	Temperatur_EG_Arbeitszimmer		"Temp. EG Arbeitszimmer [%.1f °C]"	<temperature>	(EG_Arbeitszimmer,gTemperatur,gTempChartEG)		{ knx="4/1/2" }
 Number	Temperatur_EG_Wohnzimmer		"Temp. EG Wohnzimmer [%.1f °C]"		<temperature>	(EG_Wohnzimmer,gTemperatur,gTempChartEG)		{ knx="4/1/1" }
 Number	Temperatur_EG_Kueche			"Temp. EG Küche [%.1f °C]"		<temperature>	(EG_Kueche,gTemperatur,gTempChartEG)			{ knx="4/1/0" }
 Number	Chart_Intervall				"Chart Intervall"

What I need to sort out is the problem that my KNX device sends an update on each change. Every minute or 5 minutes would be sufficient. But the charts look nice (3 temperature curves in one chart).

Hey Christian,

thank you so much for adding this.

I’m still quite new to openHab, I didn’t come to openHAB2 by migrating from openHAB and it’s quite confusing when you try it this way. So many docs refere to the old system and even so they are similar, they still differ in some details.
I’ve set up my bindings, things and items via PaperUI. This way is not as transparent as if one would have everything in Textfiles.

Do you know by any chance how to group items created by the system?

Best
Gerrit

Okay … after I found out that it’s impossible to group automatically created items … well at least it’ not possible to do so via the ui anyway, I created items manually. I had to create an extra item for the group and then I could define the group in the manualy created items.
But, when I call my Sitemap in order to display a chart, the followin is displayed in karaf:
13:59:19.425 [ERROR] [sql.internal.MysqlPersistenceService] - mySQL: Unable to find table for query ‘AT_BAD’.

I had a look at the mysql-OpenHAB db. I can see tree tables (Items, item0001 and items). The first two are empty and in the last one I can find a single entry.

Any ideas what to do/check next? Something happened/work … but no as it should be :wink:

I have the same when i install JDBC Persistence MySQL and MySQL Persistence, i uninstalled JDBC Persistence and remove cfg file and it works. I do not know for what is JDBC Persistence when openhab2 works with MySQL Persistence.

… uninstalling JDBC Persistence MySQL didn’t chage anything in my case. If I call the sitemap/charts, Karaf displays some errors … thats it.

1 Like

… but it worked the other way around! I deleted mysql-service, installed jdbc and replaced mysql inthe sitemap with jdbc. Suddenly karaf became aware that it could’t find the tables and created them (items0001-0016). In the items table I’ve got ids and names!

… too bad that it’s not logging any data

Kind of figured it out.
First of all, I ended up using “just” mysql-Service, not jdbc-mysql, not both.
Naming the persitance-file *.persist insted of *.persistance got me the mysql-service creating the tables. I’ve got no idea why there were tables being created before at all … since my file had the wrong extension, but at least it’s now working as expected.

Best
Gerrit

Hello…
Followed the guide, but i get the following errors:

"mySQL: Connection not valid"
mySQL: tableName was null
mySQL: Could not create entry for 'Temp_Outside_South' in table 'Items' with statement 'INSERT INTO Items (ItemName) VALUES (?)': No operations allowed after connection closed.

But i don’t get any connection errors anymore, first it told me that password and user wasn’t okay - but after correcting this?
I only installed the MySQL - not the JDVC MySQL, as i can see that @gerrit says it isn’t nessecary?

When checking in the SQL database i can see that the table are created, one for Itemname and one for ItemID - but no data in them

UPDATE:
Uninstalled erverything, and only installed the JDBC-MySQL and the Addon for the JDBC (see guide here)

Also you need to make a jdbc.persist file - and not the mysql.persist
This worked for me

I have the problem, that I can only get charts for max. one day. Even if I set period=3d or =w, I only see the values of the last 24 hours displayed. In MariaDB I can see all old values from, e.g., last week.

Is there a way to get the older data also displayed in Chart?

Hi there,

never heard of this :frowning:
You are sure, that you defined period=3d ?
Try using period=w (week) …

Hi @Wurzelseppi

I figured out the problem after many days. I don’t know why, but (at least with MariaDB) I have to use “h, 4h, 12h” with a lowercase h but “D, 3D, W, M, 4M, Y” with capital letters. Then it works flawless. But if I use a small letter other than the h it won’t work.

Anyhow - now it does what I expected it to do. I replaced all with sed in my sitemap and voilà … all is good.

Is it OH2 or OH1?

It is OH2, installed via apt unstable repository (what I actually miss in Paper UI is a small text that indicates the installed version / build number):

[10:56:56] root@openhab:/etc/openhab2# apt-cache policy openhab2
openhab2:
  Installed: 2.1.0~20170212123642-1
  Candidate: 2.1.0~20170212123642-1
  Version table:
 *** 2.1.0~20170212123642-1 0
        500 http://dl.bintray.com/openhab/apt-repo2/ unstable/main armhf Packages
        100 /var/lib/dpkg/status
     2.1.0~20170210142723-1 0
        500 http://dl.bintray.com/openhab/apt-repo2/ unstable/main armhf Packages
     2.1.0~20170205135640-1 0
        500 http://dl.bintray.com/openhab/apt-repo2/ unstable/main armhf Packages
     2.1.0~20170204040936-1 0
        500 http://dl.bintray.com/openhab/apt-repo2/ unstable/main armhf Packages
     2.1.0~20170204005838-1 0
        500 http://dl.bintray.com/openhab/apt-repo2/ unstable/main armhf Packages
     2.1.0~20170203040910-1 0
        500 http://dl.bintray.com/openhab/apt-repo2/ unstable/main armhf Packages

I’ve updates the tutorial because I’ve figured out that both mySQL and mySQL.JDBC persistence didn’t work together anymore. In the recent releases (latest Snapshot Builds of OH2.1) I’ve got errors and it wasn’t working anymore. So I’ve uninstalled the JDBC persistence and commented out the configuration entries unter /Services/jdbc.cfg. Since I’ve had a mess in my mySQL DB, I’ve dropped all tables and restarted OB and everything got recreated.

I am a little lost her.
I would like to use mySQL as the central DB for persistence instead of multiple persitneces for different item types, but have some questions:

  1. I don’t need jdbc persistence mySQL, but only the “regular” mySQL persistence?
  2. Consecutively I need to modify mysql.cfg (and not jdbc), right?
  3. or the other way round (jdbc mySQL instead of mySQL in question 1 and 2?

I am using OH2 stable

Right, you need to have a MySQL Server somewhere, the "MySQL Persistence“ installed via PaperUI, a “mysql.cfg”(/etc/openhab2/services$) and “mysql.persist” (/etc/openhab2/persistence$) file.