- Platform information:
- Hardware: Raspi Pi 3
- OS: Openhabian (Raspbian)
- openHAB version: 2.4.0-1
- MariaDB mysql-Server
Hi community,
this is my first post, so sorry if I miss relevant information.
I would call me still being a beginner with OH, but have quite something already running.
My issue: I want to update a database table via rule and time cron. The called “test switch” to execute the rule will be changed to a gasmeter-sensor, connected to a GPIO as input.
So far I got that stuff running but receiving an (SQL-) error message in the log when I execute the rule (“write to database”). When I issue the SQL-command (mysql -h localhost -u openhabian -pxxxxxxxxxx -e “INSERT INTO PTest (value) VALUES (0.4);” -D openhab_db) via SSH-console, it works. So I don’t really know what to change. Any help appreciated. Many thanks in advance.
Please see in the following my setup:
Item configuration:
Switch GasSwitchtemp "Gasswitchtemp" // to emulate gpio-pin "Gasmeter"
Switch GasSwitchtempDB "GasswitchtempDB" // to emulate Time Cron
Contact Gasmeter "Gasmeter" (Monitoring) { gpio="pin:18" }
Number GasmeterDaily "GasmeterDaily" (Monitoring, PersistenceOnChange)
Rule configuration:
var mysql_host = "localhost"
var mysql_user = "openhabian"
var mysql_pw = "xxxxxxxxxxx"
var mysql_db = "openhab_db"
rule "monitor gasmeterdaily"
when
Item GasSwitchtemp changed to ON // Item Gasmeter changed to CLOSED in future
then
var updatevalue = (GasmeterDaily.state as Number) + 0.1
GasmeterDaily.sendCommand(updatevalue)
end
rule "write to database"
when
Item GasSwitchtempDB changed to ON //Time cron "0 59 23 1/1 * ? *" in future
then
var nowvalue = (GasmeterDaily.state as Number)
var nowvaluestring = nowvalue.toString()
var String sql_query = "mysql -h " + mysql_host + " -u " + mysql_user + " -p" + mysql_pw + " -e \"INSERT INTO PTest (value) VALUES (" + nowvaluestring + ");\" -D " + mysql_db
var String mysql = executeCommandLine(sql_query, 5000)
if(mysql!="") {
logInfo("SQL","SQL query to be executed: " + sql_query)
logInfo("SQL","Result SQL query: " + mysql)
GasmeterDaily.sendCommand(0)
}
end
Log:
2019-02-10 20:01:36.545 [INFO ] [g.eclipse.smarthome.model.script.SQL] - SQL query to be executed: mysql -h localhost -u openhabian -pxxxxxxxxxx -e "INSERT INTO PTest (value) VALUES (0.4);" -D openhab_db
2019-02-10 20:01:36.550 [INFO ] [g.eclipse.smarthome.model.script.SQL] - Result SQL query: ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"INSERT INTO PTest (value) VALUES (0.4);"' at line 1
As mentioned, issuing the “INSERT INTO”-query via SSH-console works fine.
Any help appreciated!
Cheers,
Matthias.