Skip to content

Getting started with openHAB2 from scratch, then giving up

ianmacs edited this page Jul 19, 2019 · 1 revision

Installation

Install openHABian on Raspberry Pi. Use their SD card image if possible.

The SD card image cannot be used for the Raspberry Pi 4. The Raspberry Pi 4 is new and the openHABian SD card image has not yet been updated to boot from and execute on the Raspberry Pi 4. For the Raspberry Pi 4, download the latest Raspbian Lite image and write it to an SD card. Mount the boot partition of the SD card after writing it, create an empty file with name "ssh" in the boot partition's root directory.

Boot the Pi 4 from the Raspbian Lite SD card, connect to it via ssh, user pi, password raspberry, change password, fill ~/.ssh/authorized_keys with public key(s). Follow openHABian instructions for installation on "other linux systems".

While stepping through the openHABian configuration, I chose these from the optional parts:

  • Log Viewer
  • Mosquitto. Set authentication password here now, or it will be a hassle later.
  • Influxdb & Grafana
  • NodeRED
  • 1wire. Does not succeed.
  • change hostname
  • change time zone
  • Change passwords for samba and amandabackup

Reboot. openHAB is not running after the reboot.

Restart the configuration menu. Executing openHAB stable seems to fix the 1wire setup. The fix_permissions seems to have been missing. Rebooting again. openHAB is still not running.

pi@openhab:~ $ service openhab2 status

● openhab2.service - openHAB 2 - empowering the smart home
   Loaded: loaded (/usr/lib/systemd/system/openhab2.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Sun 2019-07-14 22:30:46 CEST; 2s ago
     Docs: https://www.openhab.org/docs/
           https://community.openhab.org
  Process: 780 ExecStart=/usr/share/openhab2/runtime/bin/karaf $OPENHAB_STARTMODE (code=exited, status=1/FAILURE)
 Main PID: 780 (code=exited, status=1/FAILURE)

I want to ask for help on the openHAB community forum. The forum template wants me to fill in the Java version. The process so far has not installed any Java version. The openhab2 package that was installed only suggests Java, and does not recommend it nor depend on it. After installing the suggested default-jre-headless and another reboot, openHAB finally runs.

I connect to it at http://openhab:8080/ and select the standard setup.

File /etc/openhabian.conf contains a time server setting that points to the ntp pool. I do not see the need to have two devices in my home network query outside time servers independently, and edit this setting to point openHAB to my router to use as the time server. Changing this file did not trigger activity in the log, therefore I restart openHAB 2 manually to activate the change.

Creating stuff in openHAB

I do not want to rely on PaperUI to do things for me. Bad experience in the past.

The first thing (item?) I want to measure is the CPU temperature of the Raspberry Pi where openHAB runs.

First (failed) attempt to import CPU temperature into openHAB

This attempt failed. The second attempt works better.

I can read the CPU temperature on the PI with

cat /sys/class/thermal/thermal_zone0/temp

which returns the temperature in milli-degrees Celsius. To convert the temperature to degrees Celsius I can insert a decimal point before the last 3 digits:

sed -e 's/\(.*\)\(...\)$/\1.\2/' /sys/class/thermal/thermal_zone0/temp

To invoke a command and use the output as data I need the exec binding in openHAB. To install it, I edit the configuration file /etc/openhab2/services/addons.cfg, uncomment the binding line and make it look

binding = exec

Saving the file triggers installation of the bining as I can observe in the log window. The documentation of the exec binding https://www.openhab.org/addons/bindings/exec/ tells me I need a thing definition like

Thing exec:command:uniquename [command="/command/to/execute here", interval=15, timeout=5, autorun=false]

I create a new file /etc/openhab2/things/raspberry_pi.things, insert

Thing exec:command:RaspberryOpenhabTemperature [command="sed -e 's/\\(.*\\)\\(...\\)$/\\1.\\2/' /sys/class

/thermal/thermal_zone0/temp", interval=15, timeout=5, autorun=false]

(I think I need to escape the backslashed) and save the file. The openHAB log prints:

2019-07-16 11:33:59.172 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'raspberry_pi.things'
2019-07-16 11:33:59.318 [hingStatusInfoChangedEvent] - 'exec:command:RaspberryOpenhabTemperature' changed from UNINITIALIZED to INITIALIZING
2019-07-16 11:33:59.331 [hingStatusInfoChangedEvent] - 'exec:command:RaspberryOpenhabTemperature' changed from INITIALIZING to ONLINE
2019-07-16 11:33:59.359 [WARN ] [hab.binding.exec.handler.ExecHandler] - Couldn't transform response because transformationService of type 'REGEX' is unavailable
2019-07-16 11:34:00.363 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'raspberry_pi.things'
2019-07-16 11:34:14.418 [WARN ] [hab.binding.exec.handler.ExecHandler] - Couldn't transform response because transformationService of type 'REGEX' is unavailable

I'm not sure what the warning means. Googling the warning tells me that I need to install the REGEX transformation. I edit the #transformation = line in /etc/openhab2/services/addons.cfg to read

transformation = regex

and the openHAB log file tells me that the extension 'transformation-regex' has been installed, and the warnings stop. The exec documentation tells me I need an item similar to

String APCRaw "[%s]" (All) {channel="exec:command:apc:output"}
Number YourNumber "Your Number [%.1f °C]"

therefore I'm creating an item in new file /etc/openhab2/items/raspberry_pi.items like this:

Number RaspberryOpenhabTemperatureValue "[%.1f   C]" (All) {channel="exec:command:RaspberryOpenhabTemperature:output"}

The log tells me

2019-07-16 12:06:09.855 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'raspberry_pi.items'
2019-07-16 12:06:09.932 [.ItemChannelLinkAddedEvent] - Link 'RaspberryOpenhabTemperatureValue-exec:command:RaspberryOpenhabTemperature:output' has been added.
2019-07-16 12:06:10.952 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'raspberry_pi.items'

Which is at least not an error or a warning. Where is my CPU temperature now? I look in PaperUI control and find it:

Value -NaN C

Disappointing. Maybe the backslash escapes were not needed after all? Removing sed and replace it with cat to see if a basic integer number can enter openHAB:

Thing exec:command:RaspberryOpenhabTemperature [command="cat /sys/class/thermal/thermal_zone0/temp", interval=15, timeout=5, autorun=false]

But after that, the value is still minus not-a-number. Time to ask the openHAB forum for help: https://community.openhab.org/t/create-first-item-on-a-new-installation-with-config-files/78110

The "exec" binding can only produce Strings and not Numbers. To fix this, either the temperature has to be imported as Strings, and significantly more stuff has to be done inside openHAB to convert the strings to numbers, or the "systeminfo" binding can be used to read the CPU temperature. I am going to implement the second option, using "systeminfo". To remove again what I have created so far in this first attempt, I empty the ".things" and ".items" files and remove the "regex" transformation and the "exec" binding from the config files.

Second (successful) attempt to import CPU temperature into openHAB

I am going to use the "systeminfo" binding to read the CPU temperature of the Raspberry Pi 4 running openHAB into openHAB. File /etc/openhab2/services/addons.cfg is edited to contain the line:

binding = systeminfo

Using the examples in the "systeminfo" documentation, I create file /etc/openhab2/things/raspberry_pi.things with content

systeminfo:computer:RaspberryOpenhab [interval_high=7, interval_medium=80]

and file etc/openhab2/items/raspberry_pi.items with content

Number RaspberryOpenhabTemperature "CPU Temperature" <temperature> { channel="systeminfo:computer:RaspberryOpenhab:sensors#cpuTemp" }

Now, the PaperUI control page shows the item with a reasonable temperature value.

Creating a chart of the CPU temperature

Trying to create a chart of the CPU temperature in HabPanel does not succeed. Probably because persistence is not activated. I try to follow this tutorial https://community.openhab.org/t/influxdb-grafana-persistence-and-graphing/13761: Edit file /etc/openhab2/services/addons.cfg to contain the line

persistence = influxdb

Create inside influxdb a database and a user for openhab:

influx -username admin -password <password_from_installation>
CREATE USER openhab WITH PASSWORD 'supersecrtepassword'
CREATE DATABASE openhab
GRANT ALL ON openhab to openhab

Edit file /etc/openhab2/services/influxdb.cfg to insert the password.

Create file /etc/openhab2/persistence/influxdb.persist with content

Strategies {
    everyMinute : "0 * * * * ?"
    everyHour   : "0 0 * * * ?"
    everyDay    : "0 0 0 * * ?"
}

Items {
    RaspberryOpenhabTemperature : strategy = everyChange
}

openHAB starts to store CPU temperature values in influxdb as can be verified by

influx -username admin -password SomePassword
use openhab
show series
select * from RaspberryOpenhabTemperature

To create a graph, I use HABPanel. In HABPanel, I create a chart item, edit it to be a chart n3-line-chart, set persistence service to influxdb. In the axis tab, I set y limits to 10 and 90 and ticks to 10. In the series tab, I select Item: RaspberryOpenhabTemperature and a color for it, and choose live update. Save and Run. This is the graph displayed: HABPanel graph of CPU temperature

Measure power consumption of freezer

I have a switchable power socket that runs the Tasmota firmware https://github.com/arendst/Sonoff-Tasmota. This power socket can also monitor the power consumed by the connected device. I want to use this power socket to monitor the power consumption of my freezer. I configure the power socket in the Tasmota firmware web interface to use the Mosquitto server installed with openHABian. The device name that it uses is u-freezer.

With mosquitto_sub I verify that the socket sends MQTT messages to mosquitto:

mosquitto_sub -t '+/u-freezer/+' -h localhost -u openhabian -P OkIneedANewPassword -v

which displays things like these

tele/u-freezer/STATE {"Time":"2019-07-18T14:43:31","Uptime":"0T00:51:51","Heap":15,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":20,"POWER":"ON","Wifi":{"AP":1,"SSId":"MySSID","BSSId":"D0:12:34:56:78:9A","Channel":4,"RSSI":42,"LinkCount":1,"Downtime":"0T00:00:05"}}
tele/u-freezer/SENSOR {"Time":"2019-07-18T14:43:32","ENERGY":{"TotalStartTime":"2019-07-16T20:58:38","Total":0.916,"Yesterday":0.541,"Today":0.335,"Period":0,"Power":0,"ApparentPower":0,"ReactivePower":0,"Factor":0.00,"Voltage":232,"Current":0.000}}

... if you wait long enough. The default update frequency of these messages is every 5 minutes. It can be lowered to every 10 seconds (that's the minimum possible value) with

teleperiod 10

in the web console of the tasmota firmware. I follow this guide for the openHAB MQTT integration: https://hoeser-medien.de/2018/12/sonoff-tasmota-mit-openhab-2-4-mqtt/ and add the mqtt binding in file /etc/openhab2/services/addons.cfg, create a file /etc/openhab2/things/mosquitto_bridge.things with content

Bridge mqtt:broker:mosquitto "Mosquitto" [
  host="localhost",
  secure=false,
  username="openhabian",
  password="GoodPassword",
  clientid="openHAB"
]

and then create file /etc/openhab2/things/mosquitto.things with content

Thing mqtt:topic:u-freezer "Freezer Power Socket" (mqtt:broker:mosquitto) {
  Channels:
    Type string : POW-Voltage    "POW Voltage"     [ stateTopic="tele/u-freezer/SENSOR" ]
} // end of thing

and finally create file /etc/openhab2/items/mosquitto.items` with content

String Sonoff_Sensors "[%s]" { channel="mqtt:topic:u-freezer:POW-Voltage"}

After trying a few variants of the above file contents, and a restart of openHAB, the bridge, and thing go online without errors. A link is created from the thing to the item, says the log. But the item does not appear on the control page of PaperUI. Frustrating.

After a lot of reading and trying things out, my configuration files finally look like this:

grep -v ^# /etc/openhab2/services/addons.cfg

binding = systeminfo, mqtt
persistence = influxdb
transformation = jsonpath

cat /etc/openhab2/things/mosquitto_bridge.things 
Bridge mqtt:broker:mosquitto "Mosquitto" [
  host="localhost",
  secure=false,
  username="openhabian",
  password="FantasticPassword",
  clientid="openHAB"
]

cat /etc/openhab2/things/mosquitto.things 
Thing mqtt:topic:mosquitto:u-freezer "Freezer Power Socket" (mqtt:broker:mosquitto) {
  Channels:
    Type string : switchstate "Freezer Switch" [ stateTopic="tele/u-freezer/STATE",  transformationPattern="JSONPATH:$.POWER" ]
    Type number : power "Freezer Power" [ stateTopic="tele/u-freezer/SENSOR", transformationPattern="JSONPATH:$.ENERGY.Power" ]
} // end of thing

cat /etc/openhab2/items/mosquitto.items
Switch Freezer_Switch "[%s]" { channel="mqtt:topic:u-freezer:switchstate"}
Number Freezer_Power "[%d]" { channel="mqtt:topic:mosquitto:u-freezer:power"}

Note the discrepancy in the channel naming between these items. I think the Number item references the channel correctly. At least, it did not work until I inserted :mosquitto: there. The Switch item curiosly also works. Leaving it like that was just an oversight.

cat /etc/openhab2/persistence/influxdb.persist
Strategies {
    everyMinute : "0 * * * * ?"
    everyHour   : "0 0 * * * ?"
    everyDay    : "0 0 0 * * ?"
}

Items {
    * : strategy = everyUpdate
}

I can now draw a graph of the power used by the freezer over time in HABPanel: Power consumption of freezer over time

The freezer periodically activates its motor to keep the low temperature inside at the target. The second-to-last activation is significantly longer than the other activations. I want to use openHAB to warn me when the freezer door is left open. This happens about twice a year by accident in our household. The freezer has a built-in alarm that has so far failed to detect the situation. It has never warned us.

During the second-to-last freezer motor activation, the freezer door was left open by a small gap. More details in this graph: Power consumption of freezer over time with door left open a tiny gap

Normally, the freezer motor is activated for about 9 mintutes, and then the freezer is idle for about 17 minutes. In this experiment, I opened the freezer door by a small gap about 6 minutes into the idle interval. The idle interval was then shorter than normal, around 12 minutes instead of 17 minutes, and the following active interval was significantly longer. After 20 minutes of activity, more than double the usual time, I closed the door gap again manually.

Next thing I need to do is to detect this situation with openHAB and implement a way to warn us so that someone gets notified and closes the door again.

Epilogue

It turns out that the connection to the switch item was in fact not working. What I saw working was an item that I had created with PaperUI during my attempts to get anything in openHAB to react to MQTT. Now I seem to be stuck with this item, it cannot be deleted anymore with PaperUI.

2 years ago I have set up an openHAB system with PaperUI to collect temperature data from my heating system. And in the end felt that I have learned nothing about some important parts of openHAB, like sitemaps and rules, and there was no way out of this corner that I had myself painted in, and finally the system stopped to work properly and I had no idea how to fix it. This is why this time, I wanted to configure everything in openHAB using config files.

By now I have decided that openHAB is not worth the effort. Configuring openHAB requires constant fighting against the system, the documentation that I find is lacking or outdated. I can find better ways to spend my time.