
ioBroker.sql
This adapter saves state history into SQL DB.
Supports PostgreSQL, mysql, Microsoft SQL Server and sqlite.
You can leave port 0 if the default port is desired.
This adapter uses Sentry libraries to automatically report exceptions and code errors to the developers. For more details and for information how to disable the error reporting, see Sentry-Plugin Documentation! Sentry reporting is used starting with js-controller 3.0.
Settings
Connection Settings
- DB Type: Type of the SQL DB: MySQL, PostgreSQL, MS-SQL or SQLite3
- Host: IP address or host name with SQL Server
- Unix socket (MySQL only): Path of a local unix socket, e.g.
/var/run/mysqld/mysqld.sock. When set, the adapter connects through that socket and ignores Host and Port, which are hidden. Useful when the server runs on the same machine — it is faster than TCP — and it is the only way to reach a database on the Docker host from a container on a macvlan network.
- Port: Port of SQL Server (leave blank if not sure)
- Database name: Database name. Default iobroker
- User: Username for SQL. Must exist in the DB.
- Password: Password for SQL. It is stored encrypted in ioBroker.
- Password confirm: Just repeat password here.
- Encrypt: Some DBs support encryption.
- Round real to: Number of digits after the comma.
- Allow parallel requests: Allow simultaneous SQL requests to DB.
- Statistics tab: lists every ID in the database with its storage type, status (logged / logging off / state deleted), number of values, estimated size and the covered time range. The Clean up button first shows what would be deleted and removes it only after confirmation. States that still exist but merely have logging switched off are excluded unless you tick the checkbox, because their history is still reachable and may be wanted.
- Do not create database: Activate this option if a database already created (e.g. by administrator) and the ioBroker-user does not have enough rights to create a DB.
- Relax PostgreSQL commit durability: PostgreSQL only, off by default. Runs the adapter's own connections with
synchronous_commit=off, so a commit no longer waits until the write-ahead log is flushed to disk. This strongly reduces disk I/O and wear, e.g. on SD cards and consumer SSDs. If the PostgreSQL server crashes, the values stored in the last few hundred milliseconds can be lost; unlike fsync=off this setting cannot corrupt the database. Other clients of the same server are not affected.
Default Settings
- Debounce Time - Protection against unstable values to make sure that only stable values are logged when the value did not change in the defined amount of Milliseconds. ATTENTION: If values change more often than this setting effectively, no value will be logged (because any value is unstable)
- Blocktime - Defines for how long after storing the last value no further value is stored. When the given time in Milliseconds is over, then the next value that fulfills all other checks is logged.
- Record changes only - This function makes sure that only changed values are logged if they fulfill other checks (see below). Same values will not be logged.
- still record the same values (seconds) - When using "Record changes only" you can set a time interval in seconds here after which also unchanged values will be re-logged into the DB. You can detect the values re-logged by the adapter with the "from" field.
- Minimum difference from the last value - When using "Record changes only" you can define the required minimum difference between the new value and the last value. If this is not reached, the value is not recorded.
- ignore 0 or null values (==0) - You can define if 0 or null values should be ignored.
- ignore values below zero (<0) - You can define if values below zero should be ignored.
- Disable charting optimized logging of skipped values - By default, the adapter tries to record the values for optimized charting. This can mean that additional values (that e.g. not fulfilled all checks above) are logged automatically. If this is not wanted, you can disable this feature.
- Alias-ID - You can define an alias for the ID. This is useful if you have changed a device and want to have continuous data logging. Please consider switching to real alias States in the future!
- Storage retention - How many values in the past will be stored on disk. Data are deleted when the time is reached as soon as new data should be stored for a datapoint.
- Maximal number of stored in RAM values - Define how many numbers of values will be held in RAM before persisting them on disk. You can control how much I/O is done.
- Enable enhanced debug logs for the datapoint - If you want to see more detailed logs for this datapoint, you can enable this option. You still need to enable "debug" loglevel for these additional values to be visible! This helps in debugging issues or understanding why the adapter is logging a value (or not).
Most of these values can be pre-defined in the instance settings and are then pre-filled or used for the datapoint.
Database installation tips
MS-SQL:
Use localhost\instance for the host and check TCP/IP connections enabled.
https://msdn.microsoft.com/en-us/library/bb909712(v=vs.90).aspx
SQLite:
is a "file"-DB and cannot manage too many events. If you have a big amount of data, use the real DB, like PostgreSQL and co.
SQLite DB must not be installed extra. It is just a file on disk, but to install it you require build tools on your system. For linux, just write:
sudo apt-get install build-essential
For windows install node.js with the "Automatically install the necessary tools..."-option and then rebuild the native SQLite module, e.g:
iobroker stop sql
iobroker rebuild sqlite3
iobroker start sql
MySQL:
You can install mysql on linux systems as following:
apt-get install mysql-server mysql-client
mysql -u root -p
CREATE USER 'iobroker'@'%' IDENTIFIED BY 'iobroker';
GRANT ALL PRIVILEGES ON * . * TO 'iobroker'@'%';
FLUSH PRIVILEGES;
If required, edit /etc/mysql/my.cnf to set bind to IP-Address for remote connecting.
Warning: iobroker user is "admin". If required, give limited rights to iobroker user.
On the "windows" it can be easily installed via installer: https://dev.mysql.com/downloads/installer/.
Pay attention to the authentication method. The new encryption algorithm in MySQL 8.0 is not yet supported by node.js and you must select legacy authentication method.

Emojis and upper/lower case in an existing database
Tables that the adapter creates now store every character (also emojis) and keep IDs apart that differ only in upper/lower case (alias.0.Licht and alias.0.licht). Tables that already exist are not changed automatically, because converting a large table takes a while and blocks it.
If you want this for an existing database: stop the SQL instance, make a backup of the database, and run these three commands, e.g. in phpMyAdmin (replace iobroker with the name of your database):
ALTER TABLE `iobroker`.datapoints MODIFY name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE `iobroker`.sources MODIFY name TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
ALTER TABLE `iobroker`.ts_string MODIFY val TEXT CHARACTER SET utf8mb4;
Then start the instance again. Without this, everything keeps working as before; only values with emojis cannot be stored.
Structure of the DBs
The default Database name is iobroker, but it can be changed in the configuration.
Sources
This table is a list of adapter's instances that wrote the entries. (state.from)
| DB |
Name in query |
| MS-SQL |
iobroker.dbo.sources |
| MySQL |
iobroker.sources |
| PostgreSQL |
sources |
| SQLite |
sources |
Structure:
| Field |
Type |
Description |
| id |
INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1) |
unique ID |
| name |
varchar(255) / TEXT |
instance of adapter, that wrote the entry |
Note: MS-SQL uses varchar(255), and others use TEXT
Data points
This table is a list of data points. (IDs)
| DB |
Name in query |
| MS-SQL |
iobroker.dbo.datapoints |
| MySQL |
iobroker.datapoints |
| PostgreSQL |
datapoints |
| SQLite |
datapoints |
Structure:
| Field |
Type |
Description |
| id |
INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1) |
unique ID |
| name |
varchar(255) / TEXT |
ID of variable, e.g. hm-rpc.0.JEQ283747.1.STATE |
| type |
INTEGER |
0 - number, 1 - string, 2 - boolean |
Note: MS-SQL uses varchar(255), and others use TEXT
Numbers
Values for states with type "number". ts means "time series".
| DB |
Name in query |
| MS-SQL |
iobroker.dbo.ts_number |
| MySQL |
iobroker.ts_number |
| PostgreSQL |
ts_number |
| SQLite |
ts_number |
Structure:
| Field |
Type |
Description |
| id |
INTEGER |
ID of state from "Data points" table |
| ts |
BIGINT / INTEGER |
Time in ms till epoch. Can be converted to time with "new Date(ts)" |
| val |
REAL |
Value |
| ack |
BIT/BOOLEAN |
Is acknowledged: 0 - not ack, 1 - ack |
| _from |
INTEGER |
ID of source from "Sources" table |
| q |
INTEGER |
Quality as number. You can find description here |
Note: MS-SQL uses BIT, and others use BOOLEAN. SQLite uses for ts INTEGER and all others BIGINT.
The user can define additional to type number the functionality of counters. For this purpose, the following table is created:
| DB |
Name in the query |
| MS-SQL |
iobroker.dbo.ts_counter |
| MySQL |
iobroker.ts_counter |
| PostgreSQL |
ts_counter |
| SQLite |
ts_counter |
Structure:
| Field |
Type |
Description |
| id |
INTEGER |
ID of state from "Data points" table |
| ts |
BIGINT / INTEGER |
Time in ms till epoch. Can be converted to time with "new Date(ts)" |
| val |
REAL |
Value |
This table stores the values when the counter was exchanged and the value does not increase, but failed to zero or lower value.
Strings
Values for states with type string.
| DB |
Name in query |
| MS-SQL |
iobroker.dbo.ts_string |
| MySQL |
iobroker.ts_string |
| PostgreSQL |
ts_string |
| SQLite |
ts_string |
Structure:
| Field |
Type |
Description |
| id |
INTEGER |
ID of state from "Data points" table |
| ts |
BIGINT |
Time in ms till epoch. Can be converted to time with "new Date(ts)" |
| val |
TEXT |
Value |
| ack |
BIT/BOOLEAN |
Is acknowledged: 0 - not ack, 1 - ack |
| _from |
INTEGER |
ID of source from "Sources" table |
| q |
INTEGER |
Quality as number. You can find description here |
Note: MS-SQL uses BIT, and others use BOOLEAN. SQLite uses for ts INTEGER and all others BIGINT.
Booleans
Values for states with type boolean.
| DB |
Name in query |
| MS-SQL |
iobroker.dbo.ts_bool |
| MySQL |
iobroker.ts_bool |
| PostgreSQL |
ts_bool |
| SQLite |
ts_bool |
Structure:
| Field |
Type |
Description |
| id |
INTEGER |
ID of state from "Data points" table |
| ts |
BIGINT |
Time in ms till epoch. Can be converted to time with "new Date(ts)" |
| val |
BIT/BOOLEAN |
Value |
| ack |
BIT/BOOLEAN |
Is acknowledged: 0 - not ack, 1 - ack |
| _from |
INTEGER |
ID of source from "Sources" table |
| q |
INTEGER |
Quality as number. You can find description here |