Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[InfluxDB] Old binding port & some improvements - Initial contribution #6507

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,11 @@
<artifactId>org.openhab.persistence.mapdb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.persistence.influxdb</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.transform.bin2json</artifactId>
Expand Down
40 changes: 40 additions & 0 deletions bundles/org.openhab.persistence.influxdb/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry exported="true" kind="lib" path="lib/influxdb-java-2.15.jar" sourcepath="lib-src/influxdb-java-2.15-sources.jar"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/converter-moshi-2.5.0.jar"/>
<classpathentry kind="lib" path="lib/logging-interceptor-3.13.1.jar"/>
<classpathentry kind="lib" path="lib/moshi-1.5.0.jar"/>
<classpathentry kind="lib" path="lib/msgpack-core-0.8.16.jar"/>
<classpathentry kind="lib" path="lib/okhttp-3.13.1.jar"/>
<classpathentry kind="lib" path="lib/okio-1.17.2.jar"/>
<classpathentry kind="lib" path="lib/retrofit-2.5.0.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions bundles/org.openhab.persistence.influxdb/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.openhab.persistence.influxdb</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
45 changes: 45 additions & 0 deletions bundles/org.openhab.persistence.influxdb/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
This content is produced and maintained by the openHAB project.

* Project home: https://www.openhab.org

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/openhab/openhab2-addons

== Third-party Content

influxdb-java
* License: MIT License
* Project: https://github.com/influxdata/influxdb-java
* Source: https://github.com/influxdata/influxdb-java

okhttp / logging-interceptor
* License: Apache License 2.0
* Project: https://square.github.io/okhttp
* Source: https://github.com/square/okhttp

okio
* License: Apache License 2.0
* Project: https://github.com/square/okio
* Source: https://github.com/square/okio

retrofit / converter-moshi
* License: Apache License 2.0
* Project: https://square.github.io/retrofit
* Source: https://github.com/square/retrofit

moshi
* License: Apache License 2.0
* Project: http://square.github.io/moshi/1.x/moshi/
* Source: https://github.com/square/moshi

msgpack-core
* License: Apache License 2.0
* Project: https://msgpack.org/
* Source: https://github.com/msgpack/msgpack-java
69 changes: 69 additions & 0 deletions bundles/org.openhab.persistence.influxdb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# InfluxDB (0.9 and newer) Persistence

This service allows you to persist and query states using the [InfluxDB](http://influxdb.org) time series database. The persisted values can be queried from within openHAB. There also are nice tools on the web for visualizing InfluxDB time series, such as [Grafana](http://grafana.org/).

> There are two Influxdb persistence bundles which support different InfluxDB versions. This service, named `influxdb`, supports InfluxDB 0.9 and newer, and the `influxdb08` service supports InfluxDB up to version 0.8.x.

## Database Structure

The states of an item are persisted in *measurements* with names equal to the name of the item, or the alias, if one is provided. In both variants, a *tag* named "item" is added, containing the item name. All values are stored in a *field* called "value" using integers or doubles, `OnOffType` and `OpenClosedType` values are stored using 0 or 1.

Some example entries for an item with the name "speedtest" without any further configuration would look like this:

> SELECT * FROM "openhab"."autogen"."speedtest"
name: speedtest
time item value
---- ---- -----
1558302027124000000 speedtest 123289369.0
1558332852716000000 speedtest 80423789.0


## Prerequisites

First of all you have to setup and run an InfluxDB server. This is very easy and you will find good documentation on it on the [InfluxDB web site](https://docs.influxdata.com/influxdb/).

Then database and the user must be created. This can be done using the InfluxDB admin web interface. If you want to use the defaults, then create a database called `openhab` and a user with write access on the database called `openhab`. Choose a password and remember it.

## Configuration

This service can be configured through UI, or in the file `services/influxdb.cfg`.

| Property | Default | Description |
|----------|---------|-------------|
| url | http://127.0.0.1:8086 | Database URL |
| user | openhab | Name of the database user, e.g. `openhab` |
| password | habopen | Password of the database user that you chose in [Prerequisites](#prerequisites) above |
| db | openhab | Name of the database |
| retentionPolicy | autogen | Name of the retentionPolicy. Please note starting with InfluxDB >= 1.0, the default retention policy name is no longer `default` but `autogen`. |
| replaceUnderscore | false | Whether underscores "_" in item names should be replaced by a dot "." ("test_item" -> "test.item"). Only for *measurement* name, not for *tags*. Also applies to alias names. |
| addCategoryTag | false | Should the category of the item be included as *tag* "category"? If no category is set, "n/a" is used. |
| addTypeTag | false | Should the item type be included as *tag* "type"? |
| addLabelTag | false | Should the item label be included as *tag* "label"? If no label is set, "n/a" is used. |

All item- and event-related configuration is defined in the file `persistence/influxdb.persist`.

## Tags

*Tags* are a great tool to manage your data when using not only OpenHAB, but for example also Grafana or Kapacitor for visualisation or alerting. This binding lets you add *tags* in two different ways:

### Configuration

There are three options in the config to add *tags* for the item category, the item type and the item label.

### Metadata

Item Metadata is data added to the item itself. The namespace for this binding is "influxdb". Every single key-value pair will be used as *tag* name - *tag* value. So for example the metadata

Number test {influxdb="" [foo="bar", baz="qux"]}

will add *tags* "foo" and "baz" with the *values* "bar" and "qux" every time the value of the item "test" gets stored.

### Warning

Be aware that using many *tags* will lead to an exponential increase of the number of time series, resulting in a huge amount of memory consumption. Use only *tags* you really need!

## Examples

[01) Basic usage: temperature and humidity](examples/01-temperature.md "First example")

[02) Advanced usage: power consumption](examples/02-powerconsumption.md "Second example")
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## Temperature and humidity

The first example shows how to persist item values in a very basic way. We do group the *measurements*, but we do not add additinal *tags*. Think of two temperature sensors and two humidity sensors, each one in your living room and one in your kitchen.

Content of item file `items/temperature.items`:

Group g_temperature
Group g_humidity

Number:Temperature c_livingroom_temperature "Temperature living room [%.1f °C]" (g_temperature)

Number:Temperature c_kitchen_temperature "Temperature kitchen [%.1f °C]" (g_temperature)

Number c_livingroom_humidity "Humidity living room [%d%%]" (g_humidity)

Number c_kitchen_humidity "Humidity kitchen [%d%%]" (g_humidity)

The groups "g_temperature" and "g_humidity" simplify the persistence declaration as shown below. Each group will be one *measurement*.

Content of persistence file `persistence/influxdb.persist`:

Strategies {}

Items {
g_temperature* -> "g_temperature" : strategy = everyUpdate
g_humidity* -> "g_humidity" : strategy = everyUpdate
}

In the end we have two *measurements*, each has two items:

SELECT * FROM "openhab"."autogen"."g_temperature" ORDER BY time DESC LIMIT 4
name: g_temperature
time item value
---- ---- -----
1565175206339000000 c_livingroom_temperature 21.2
1565175203349000000 c_kitchen_temperature 20.7
1565175199542000000 c_livingroom_temperature 21.1
1565175194696000000 c_kitchen_temperature 20.6


SELECT * FROM "openhab"."autogen"."g_humidity" ORDER BY time DESC LIMIT 4
name: g_humidity
time item value
---- ---- -----
1565175206339000000 c_livingroom_humidity 55
1565175203349000000 c_kitchen_humidity 61
1565175199542000000 c_livingroom_humidity 55
1565175194696000000 c_kitchen_humidity 62
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Power meter

The second example shows how to add additional *tags*. Imagine you have a two power meters for the fridge and the dishwasher in your kitchen, and one for the TV on you living room. They all measure the same thing ("instant power"), so you can combine them into one *measurement*.

Content of item file `items/powermeter.items`:

Group g_instantpower

Number:Power c_fridge_instantpower "Power fridge [%.1f W]" (g_instantpower) {influxdb="" [room="Kitchen", target="Fridge"]}

Number:Power c_dishwasher_instantpower "Power dishwasher [%.1f W]" (g_instantpower) {influxdb="" [room="Kitchen", target="Dishwasher"]}

Number:Power c_tv_instantpower "Power TV [%.1f W]" (g_instantpower) {influxdb="" [room="Living room", target="TV"]}

We are adding two *tags*: "room" and "target". Another *tag* "item" with the item name gets added automatically.

The group "g_instantpower" simplifies the persistence declaration as shown below.

Content of persistence file `persistence/influxdb.persist`:

Strategies {}

Items {
g_instantpower* -> "g_instantpower" : strategy = everyUpdate
}

This will create a single *measurement* "g_instantpower":

SELECT * FROM "openhab"."autogen"."g_instantpower" ORDER BY time DESC LIMIT 6
name: g_instantpower
time target item room value
---- ------ ---- ---- -----
1565175206339000000 Fridge c_fridge_instantpower Kitchen 80
1565175203349000000 Dishwasher c_dishwasher_instantpower Kitchen 348
1565175199542000000 TV c_tv_instantpower Living room 63
1565175194696000000 Fridge c_fridge_instantpower Kitchen 81
1565175194185000000 Dishwasher c_dishwasher_instantpower Kitchen 390
1565175193689000000 TV c_tv_instantpower Living room 70

You are now able to query the database for the specific item, the target and the room. When adding more power meters, you do not have to modify your Grafana dashboards or alert rules, the only thing you have to do is add them to the group "g_instantpower".
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
21 changes: 21 additions & 0 deletions bundles/org.openhab.persistence.influxdb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>2.5.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.persistence.influxdb</artifactId>

<name>openHAB Add-ons :: Bundles :: Persistence Service :: InfluxDB</name>

<properties>
<bnd.importpackage>!android.*,!com.android.*,!dalvik.*,!kotlin,!kotlin.*,!javax.*,!org.conscrypt,!sun.*</bnd.importpackage>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.persistence.influxdb-${project.version}"
xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${project.version}/xml/features</repository>

<feature name="openhab-persistence-influxdb" description="InfluxDB Binding" version="${project.version}">
<feature>openhab-runtime-base</feature>
<!-- <feature prerequisite="true">wrap</feature> -->
<!-- <bundle dependency="true">wrap:mvn:org.influxdb/influxdb-java/2.15$Bundle-Name=InfluxDB%20Java%20Library&amp;Bundle-SymbolicName=org.influxdb.influxdbjava&amp;Bundle-Version=2.15</bundle> -->
<!-- <bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.persistence.influxdb/${project.version}</bundle> -->
</feature>
</features>
Loading