Below is a simple example on how to “connect” Home Assistant with openHAB via MQTT so both share the same Switch states. As MQTT broker I’ve tested the setup with RabbitMQ and Mosquitto.
openHAB2 configuration
In the openHAB2 Paper UI head to “Add-ons” - “Bindings” and enable the “MQTT Binding”.
/etc/openhab2/services/mqtt.cfg
mqtt:broker.url=tcp://localhost:1883
mqtt:broker.clientId=openhab
mqtt:broker.retain=true
mqtt:broker.async=false
/etc/openhab2/items/switch.items
Switch Switch1 {mqtt=">[broker:myhome/bedroom/switch1:command:ON:1],>[broker:myhome/bedroom/switch1:command:OFF:0],<[broker:myhome/bedroom/switch1:state:ON:1],<[broker:myhome/bedroom/switch1:state:OFF:0"}
Switch Switch2 {mqtt=">[broker:myhome/bedroom/switch2:command:ON:1],>[broker:myhome/bedroom/switch2:command:OFF:0],<[broker:myhome/bedroom/switch2:state:ON:1],<[broker:myhome/bedroom/switch2:state:OFF:0]"}
/etc/openhab2/sitemaps/default.sitemap
sitemap default label="My first sitemap"
{
Switch item=Switch1 label="Switch1"
Switch item=Switch2 label="Switch2"
}
Home Assistant configuration
/var/lib/homeassistant/configuration.yaml
mqtt:
broker: 127.0.0.1
port: 1883
client_id: home-assistant-1
keepalive: 60
switch: !include devices/switches.yaml
/var/lib/homeassistant/devices/switches.yaml
- platform: mqtt
name: "Switch1"
state_topic: "myhome/bedroom/switch1"
command_topic: "myhome/bedroom/switch1"
payload_on: "1"
payload_off: "0"
retain: true
- platform: mqtt
name: "Switch2"
state_topic: "myhome/bedroom/switch2"
command_topic: "myhome/bedroom/switch2"
payload_on: "1"
payload_off: "0"
retain: true
You can then use
$ mosquitto_sub -h localhost -d -v -t "#"
to watch what is being recieved by the MQTT broker.
And to test you can send e.g.
$ mosquitto_pub -h localhost -m "1" -t myhome/bedroom/switch1 -u guest -P guest -d
to switch switch1 on (send “0” so switch it off) and you should be able to watch the switches in the webinterfaces of both Home Assistant and openHAB acting accordingly.