Persistence isnt working

Hallo,

i dont get persistence working. I think i’ve done everything right, get no errors in log, but still have no persistence.

My jdbc.cfg

############################ JDBC Persistence Service ##################################
# I N S T A L L   J D B C   P E R S I S T E N C E   S E R V I C E 
#
# https://github.com/openhab/openhab/wiki/JDBC-Persistence
#
# Tested databases/url-prefix: jdbc:derby, jdbc:h2, jdbc:hsqldb, jdbc:mariadb, jdbc:mysql, jdbc:postgresql, jdbc:sqlite
# 
# derby, h2, hsqldb, sqlite can be embedded, 
# If no database is available it will be created, for example the url 'jdbc:h2:./testH2' creates a new DB in OpenHab Folder. 
#
# Create new database, for example on a MySQL-Server use: 
# CREATE DATABASE 'yourDB' CHARACTER SET utf8 COLLATE utf8_general_ci;

# D A T A B A S E  C O N F I G
# Some URL-Examples, 'service' identifies and activates internally the correct jdbc driver.
# required database url like 'jdbc:<service>:<host>[:<port>;<attributes>]'
# url=jdbc:derby:./testDerby;create=true
# url=jdbc:h2:./testH2
# url=jdbc:hsqldb:./testHsqlDb
# 
url=jdbc:mariadb://localhost:3306/openhab
# url=jdbc:mysql://localhost:3306/openhab
# url=jdbc:postgresql://192.168.0.1:5432/testPostgresql
# url=jdbc:sqlite:./testSqlite.db
# url=

# required database user
# 
user=openhab

# required database password
# 
password=mypw

# E R R O R   H A N D L I N G
# optional when Service is deactivated (optional, default: 0 -> ignore) 
#errReconnectThreshold=

# I T E M   O P E R A T I O N S
# optional tweaking SQL datatypes
# see: https://mybatis.github.io/mybatis-3/apidocs/reference/org/apache/ibatis/type/JdbcType.html   
# see: http://www.h2database.com/html/datatypes.html
# see: http://www.postgresql.org/docs/9.3/static/datatype.html
# defaults:
#sqltype.CALL          =   VARCHAR(200)
#sqltype.COLOR         =   VARCHAR(70)
#sqltype.CONTACT       =   VARCHAR(6)
#sqltype.DATETIME      =   DATETIME
#sqltype.DIMMER        =   TINYINT
#sqltype.LOCATION      =   VARCHAR(30)
#sqltype.NUMBER        =   DOUBLE
#sqltype.ROLLERSHUTTER =   TINYINT
#sqltype.STRING        =   VARCHAR(65500)
#sqltype.SWITCH        =   VARCHAR(6)

# For Itemtype "Number" default decimal digit count (optional, default: 3) 
#numberDecimalcount=

# T A B L E   O P E R A T I O N S
# Tablename Prefix String (optional, default: "item") 
# for Migration from MYSQL-Bundle set to 'Item'.
#tableNamePrefix=Item

# Tablename Prefix generation, using Item real names or "item" (optional, default: false -> "item") 
# If true, 'tableNamePrefix' is ignored.
#tableUseRealItemNames=

# Tablename Suffix length (optional, default: 4 -> 0001-9999) 
# for Migration from MYSQL-Bundle set to 0.
#tableIdDigitCount=

# Rename existing Tables using tableUseRealItemNames and tableIdDigitCount (optional, default: false) 
# USE WITH CARE! Deactivate after Renaming is done!
#rebuildTableNames=true

# D A T A B A S E  C O N N E C T I O N S
# Some embeded Databases can handle only one Connection (optional, default: configured per database in packet org.openhab.persistence.jdbc.db.* )
# see: https://github.com/brettwooldridge/HikariCP/issues/256
# jdbc.maximumPoolSize = 1
# jdbc.minimumIdle = 1

# T I M E K E E P I N G
# (optional, default: false) 
#enableLogTime=true

My persistence.persist

// persistence strategies have a name and a definition and are referred to in the "Items" section
Strategies {
    everyHour 	: "0 0 * * * ?"
    everyDay  	: "0 0 0 * * ?"
    everyMinute : "0 * * * * ?"

    // if no strategy is specified for an item entry below, the default list will be used
    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
    Temperature*, Weather* : strategy = everyMinute
}

The tables are createt, if i create a chart in habmin, but no values were inserted. The tables stay empty!

Any idea what i am doing wrong? Is it an issue of openhab2?

Temperature and Weather are both Groups?

What is your default persitence?

Do you have any errors or other logs from the jdbc binding in the openhab logs?

Hi, maybe they are groups, but i dont use them - i have several homematic temperature sensors and mqtt items like this:

Number temperature11 "espeasy_1/1/Temperature [%.1f °C]" <temperature> (mqtt, espeasy, espeasy_1) {mqtt="<[mosquitto:/espeasy_1/1/Temperature:state:default]"}
Number temperature12 "espeasy_1/2/Temperature [%.1f °C]" <temperature> (mqtt, espeasy, espeasy_1) {mqtt="<[mosquitto:/espeasy_1/2/Temperature:state:default]"}
Number temperature13 "espeasy_1/3/Temperature [%.1f °C]" <temperature> (mqtt, espeasy, espeasy_1) {mqtt="<[mosquitto:/espeasy_1/3/Temperature:state:default]"}
Number temperature14 "espeasy_1/4/Temperature [%.1f °C]" <temperature> (mqtt, espeasy, espeasy_1) {mqtt="<[mosquitto:/espeasy_1/4/Temperature:state:default]"}

there are no errors … and as i ve written: The tables were created but no values written.

and i have changed the * : strategy to everyMinute - still noch change…

The Temperature* syntax means persist all members of the Temperature group. It is not a wild card. Also, it is case sensitive so it wouldn’t work even if * were treated as a wild card.

That isn’t your problem, but it was something I noticed.

Any answers to the other questions I asked?

as you can see in my persist file:

default = everychange

and

  • : strategy = everyChange, everyMinute, restoreOnStartup

and as the comment say * meas persist all tiems …

Any answers to the other questions I asked?

What is your default persitence?

Do you have any errors or other logs from the jdbc binding in the openhab logs?

in what config file i find my default persistence??? I dont know what you want to know?

No errors, concerning jdbc only these log entrys:

2016-10-25 23:51:10.383 [INFO ] [persistence.jdbc.internal.JdbcMapper] - JDBC::openConnection: Driver is available::Yank setupDataSource
2

In PaperUI you can find and set the default persistence in Configuration > System > Persistence.

That log indicates you do not have the Maria JDBC driver installed. I have no idea how to do this in OH 2. The instructions for it are documented here for OH 1.

thanks - jdbc was selected!

if i have no jdbc driver / maria db driver, how can openhab create tables inside my mariadb???

Exactly. You must download and install the Maria DB driver. I just looked and there is a JDBC Persistence MariaDB addon listed under Persistence in PaperUI. Did you install this?

yes i did…

the problem is: openhab2 creates tables in my maria db - so the db connection is working. I can find the following tables inside my openhab db:

item0001
item0002
item0003
item0004
items

and yes - i installed the maria db persistence package via paperui and i downloaded now the /mariadb-java-client-1.5.4.jar to the /usr/share/openhab2/addons folder

After copying the file the following log entry appeared:

2016-10-26 00:08:58.972 [INFO ] [persistence.jdbc.internal.JdbcMapper] - JDBC::openConnection: Driver is available::Yank setupDataSource

still no values inside the tables

I have no further idea. I don’t use JDBC so have no further experience to draw upon.

Maybe just try the MySQL driver which probably is already there. As MariaDB is just a fork of MySQL, it’s all the same…

It was all the same. But it is a fork so the two are drifting apart over time. It is likely to work now but there is no guarantee it will work the same forever.

I can give you an example of how I implemented mySQL a couple of days ago and it worked.

  1. Install “JDBC Persistence MySQL” and “MySQL Persistence” via PaperUI
  2. Installed and configures 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) two config files under
/etc/openhab2/services$

My jdbc.cfg (shortened to the important sections):

url=jdbc:mysql://127.0.0.1:3306/OpenHAB

# required database user

user=openhab

# required database password

password=YOURPASSWORD

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. I’ve created a persistence file under
/etc/openhab2/persistence$

and its 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. Sorry for the formating, it’s my first post :wink: Hope that helps.

Cheers
Christian

2 Likes

Would you be willing to write this up as a tutorial under the Tutorials and Examples tag? I see a lot of people having troubles getting MySQL and JDBC in general working and it would be handy to have an easily findable posting we can point people at.

Sure, will do that. Just need to change a few layout things.

I just tried to get persistance working, but I’m not sure, what goes wrong.

ok here’s my steps:

(1) addons.cfg

# A comma-separated list of persistence services to install (e.g. "rrd4j,jpa")
persistence = mysql 	

(2) mysql.cfg

############################ mySQL Persistence Service ##################################

# the database url like 'jdbc:mysql://<host>:<port>/<database>'
mysql:url=jdbc:mysql://192.168.xx.xx/openHAB

# the database user
mysql:user=xxx

# the database password
mysql:password=xxx

# the reconnection counter
#mysql:reconnectCnt=

# the connection timeout (in seconds)
#mysql:waitTimeout=

# optional tweaking of mysql datatypes
# example as described in https://github.com/openhab/openhab/issues/710
# mysql:sqltype.string=VARCHAR(20000)

# Use MySQL Server time to store item values (=false) or use openHAB Server time (=true).
# For new installations, its recommend to set "mysql:localtime=true".
# (available since 1.9, optional, defaults to false)
#mysql:localtime=true

(3) sensoren.persist

Strategies {
	everyMinute	: "0 * * * * ?"
	every5Minutes	: "0 */5 * * * ?"
   	everyHour	: "0 0 * * * ?"
   	everyDay	: "0 0 0 * * ?"
   	everyWeek	: "0 0 0 0 * ?"
   	everyMonth	: "0 0 0 0 0 ?"

    default = everyChange
}

Items {
    * : strategy = everyMinute, everyHour, everyDay, restoreOnStartup
}

After saving, I’ve got a new table in MySQL “Items” like this

CREATE TABLE `Items` (
  `ItemId` int(11) NOT NULL AUTO_INCREMENT,
  `ItemName` varchar(200) NOT NULL,
  PRIMARY KEY (`ItemId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf16;

But this table stays empty, and there’s no other tables being created despite time strategies everyMinute and default strategy everyChange

The mySQL Persistance bündle is live (obviously):

208 | Active   |  80 | 1.9.0.201611210210    | openHAB mySQL Persistence bundle

I don’t get something out of the logs (even after log:set trace org.openhab.binding.mysql).

only thing I get:

2016-11-30 22:36:22.848 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sensoren.persist'
2016-11-30 22:36:22.947 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sensoren.persist'

the odd thing is the double entry of refreshing - but I get those on every occasion, should be another issue…?

I’ve seen several people have issues getting MySQL working. Unfortunately I’ve been unsuccessful helping them figure out how to make it work.

InfluxDB is awesome though. :wink:

1 Like

Thing is, the connection is there, but no values are inserted. Was this somehow the case in your unsuccessful cases? Or didn’t it connect in the first place. Perhaps it’s just a misconfiguration of strategies or items…? :thinking: