In this post we will see how we can control items i.e. lights from one openHAB instance to another using a Zigbee button and MQTT.
openHAB recently passed into version 3 which comes with a complete revamp of the GUI and a massive rewriting of the backend code. There are mainly two protocols that we will be using in this guide.
Zigbee is a well established smart home protocol that gains more and more traction especially due to lower prices compared to competitive protocols i.e. ZWave and MQTT is also a well established lightweight IoT protocol that facilitates message and data exchange between IoT devices and systems.
Motivation
The reason I use two openHAB instances is because I have been using openHAB v2 with a few dozen zwave devices and I did not want to upgrade just yet to openhab v3. At the same time I wanted to put some zigbee devices in the mix and I would like to try openHAB v3. Thankfully openHAB v3 offers the openHAB remote binding that allows two openHAB instances to be linked together.
Assumptions
- Zigbee2MQTT, MQTT Broker and both openHAB systems installed
- already openHAB v2 set up to control lights
- installed the following in openHAB v3
- Remote openHAB binding
- MQTT Binding
- JSONPath Transformation
- RegEx transformation
- For the purposes of this guide I have created the following in openHAB3
- Item called Bathroom Lights, a dummy switch that will push this state to the real switch BathLight controlling the bathroom lights in openhab2
- Thing that represents the MQTT Broker
- Thing that represents the remote openHAB2 server
- A generic MQTT Thing that represents the Aqara button
High Level Overview
I have an Aqara button working as a Zigbee device and it support single, double and long presses. Zigbee2MQTT translates those Zigbee messages to MQTT and sends them to an MQTT Broker. OpenHAB3 receives them through the MQTT binding and then at the same time updates a channel located in openHAB2 that controls the lights.
Configuration
You need to have the following 3 Things configured in openHAB3
The Generic MQTT Thing is the Aqara button (I probably should have changed the name to “Aqara Button”) and is associated to the MQTT broker
In the Channels tab you need to create a channel for the button as follows (for Channel Type select Text Value)
The MQTT State topic is the one that the zigbee2mqtt publishes to the broker, you can pick this up using either the mqtt broker logs or the MQTT Explorer application. The Aqara button sends a JSON message in the following format
Single press:
{"action":"single","battery":100,"linkquality":141,"voltage":3205}
Double press
{"action":"double","battery":100,"linkquality":141,"voltage":3205}
We will remove all strings and keep only ‘single’ / ‘double’ via the JSONPATH transformation.
Then via the REGEX transformation we will replace single with ON and double with OFF. You need to place the transformation in the Incoming Value Transformation field as indicated in the picture above (make sure you select show advanced checkbox)
JSONPATH:$.action∩REGEX:s/single/ON/g∩REGEX:s/double/OFF/g
You then link the above channel to the dummy switch item Bathroom Lights
Here is the Bathroom Lights item configuration
Now you go to Things –> Openhab2 server (via the remote openhab binding) –> Channels tab and you select the channel from openhab2 that controls the lights. In this case BathLight
You link that to the dummy switch item as well
It is very important that you change the profile for the openhab2 channel to FOLLOW
This is required because any update coming from MQTT will not be send as command to the openhab2 channel as explained here.
With the FOLLOW profile, any change on the dummy switch state will cause a change to the openhab2 channel as well.
In the end if you select the dummy item you should be able to see that it is linked to two channels, the incoming MQTT channel from the Aqara button and the outgoing channel towards the openhab2 server.
For all issues with the transformations and messages don’t forget to look at the openhab.log and event.log as they are providing useful insights as to what might have gone wrong
I did not write any rules for this integration which is really cool.
Note: you can get rid of the JSONPATH transformations by checking this thread