It's time to integrate MQTT and OpenHAB v.2.
As already said in a previous post (http://domoticsduino.blogspot.com/2017/08/home-automation-system-sonoff-and-mqtt_19.html), actually I have a SONOFF Basic already installed which drives a garden lamp. As we already know, it's already setup with MQTT and so let's start the OpenHAB integration.
I followed step by step the Tasmota wiki, in the integration section (https://github.com/arendst/Sonoff-Tasmota/wiki/openHAB) and so, as first step, I installed some ADDONS in OpenHAB:
- MQTT Binding: http://docs.openhab.org/addons/bindings/mqtt1/readme.html
- MQTT Action: http://docs.openhab.org/addons/actions/mqtt/readme.html
- JsonPath Transformation: http://docs.openhab.org/addons/transformations/jsonpath/readme.html
Now I have to setup the MQTT connection to the MQTT broker which runs on the same Raspberry PI 3 as OpenHAB 2; so I have to edit the configuration file /etc/openhab2/config/mqtt.cfg directly through the linux shell (nano or vi) or using Eclipse SmartHome Designer, an advanced text editor optimized for OpenHAB v.2 configuration files. I used it. Its setup is very simple and then it needs to know the OpenHAB configuration files path. In my case I setup a network drive (called Z) for a samba share automatically defined by the installation of openHABianPI.
In particular, the configuration parameters I edited are the following ones:
openhabianpi-mqtt.url=tcp://localhost:1883
serve and port of the MQTT broker
openhabianpi-mqtt.clientId=openhabianpi
openHAB client Id for the MQTT connection
openhabianpi-mqtt.user=openhabian
Username for the MQTT connection
openhabianpi-mqtt.pwd=XXXXXXX
Password for the MQTT connection
openhabianpi-mqtt.retain=true
Defines if the broker should retain the messages sent to it
openhabianpi-mqtt is the logical name of the MQTT connection; in OpenHAB we could have many MQTT connections and in the items configuration we use this name as reference.
If you don't make mistakes, during the OpenHAB boot you may find log messages as following:
Ok, now I will configure items and the sitemap .
So, I created a file called sonoffbasic01.items in the folder /etc/openhab2/items in which I setup my new items.
I defined a "Switch" object ("light" category) which was linked to the full topic mqtt of the SONOFF Basic 01 tasmota/garden/light/%topic%/%prefix%/. Here is the syntax:
Switch Garden_Light_1Lamp "Garden 1-Lamp" <light>
(gLightSwitchGarden)
{mqtt="
<[openhabianpi-mqtt:tasmota/garden/light/sonoffbasic01/stat/POWER:state:default],
(gLightSwitchGarden)
{mqtt="
<[openhabianpi-mqtt:tasmota/garden/light/sonoffbasic01/stat/POWER:state:default],
}
For info about syntax of .items files please see official documentation http://docs.openhab.org/configuration/items.html
In this way I created an item called Garden_Light_1Lamp linked to MQTT messages of topic defined in the {...} block
Then, I added this new item in my sitemap . So, I edited sitemap definition file, appending the following rows:
Switch item=Garden_Light_1Lamp label="Garden Light 1 Lamp" icon="switch"
}
After saved this file, OpenHAB updated the sitemap and the "lights" section appears
Here is a short video about my test: