Home Assistant - MQTT - openHAB: Switch example

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.

6 Likes

Just curious; why do you run both openHAB and Home assistant in parallel?

At the moment I don’t run them in parallel, for now this was just a test to see if it’s feasable. I’d like to use Home Assistant exclusivly, my main home automation is Loxone because I wanted to have everything in the house wired without too much hassle and DIY regarding the electric part.

Sadly currently there is no Home Assistant component for easily interacting with Loxone (Loxone Smart Home Support). So my fallback plan is to maybe have openHAB with the Loxone binding working as a bridge to Home Assistant that’s why I tried something simple like “connecting” a switch first.

Maybe it’s not worth the additional hassle and I’ll settle with openHAB exclusivly to extent the proprietary Loxone system. However currently my personal preference is Home Assistant because of the development pace and it just feels nicer to me. At the moment I have a Home Assistant test setup working with some multimedia components, an Echo Dot for voice control (which is something Loxone is completely lacking for example), Mopidy, Snapcast and two Raspberry clients with the Snapclient which will act as roomspeakers in the ceiling so I can for example send TTS messages and stuff like that.

So at the moment I’m still pretty much in the learning, trinkering and thinking phase, which will probably get more clear when I finally have moved to the new home and can try things for real.

I’m very interested in this. Are you running both on the same hardware? I’m wondering if a pi3 is enough power to run them in parallel.

My rationale for running both would be to use some components that OpenHAB does not have (such as Xiaomi Vacuum), and being able to leverage OpenHab for my.openhab connectivtiy to Google Home/Assistant. I know Home Assistant has HA Cloud (Bridge), but at $5/month, it’s a lot steeper than I’d like.

Running a mix of smart things and home assistant now and ST went down yet again tonight… likely my final straw.

1 Like

I’m doing the same thing with a Pi Zero. Thanks for this post. It really helped.

I used your idea to integrate HA with Niko Home Control (supported by OpenHAB but not by HA). Works great, thanks a million for paving the way!

With the recently released openHAB 2.4 this might be even easier now as the new MQTT Things extension supports the HomeAssistant MQTT Components specification convention out-of-the-box. More details about this new feature can be found in the related blog post:

I have some insteon switches on openhab (insteon compenant on HA doesn’t work well) which I like to control from home assistant, would you kindly be able to point me to the right direction

@fxcoolby when was the last time you tried the insteon component in HA?

This sounds great! I gave OpenHAB a few tries but ended with hass.
The only things I miss regarding integration is the RWE Smarthome or Innogy or Livisi. So for OpenHAB there is an binding. So including those thermostats I have with this way would be awesome… I think I’ll give it a try! :slight_smile: Thanks a lot for this thread!

Hi Very interesting post.
I was able to replicate with just some mods.
These are my first steps with home assistant.
Ho can “connect” this switch to a switch already existing (Legrand/Bticino living now)
Or directly activate the Legrand/Bticino living now by mqtt.
My goal is turn on/off and check living now switches from mqtt thru home assistant.
Thanks in advance. Enrico.

Hello. I too would like to interact, via Home Assistant, with switches and dimmers associated with my openHAB server. Did you ever look into this further and get it working? If so please share details.

Hey sorry for late response, the only way you can do this is through MQTT, insteon is much more stable on HA and hence i ditched Openhab entirely a yaer or so. There might be easier ways to do it on Openhab, but to best of my outdated knowledge MQTT portocl is the best way to bridge the platforms.

Guys, anyone still using Openhab , want to setup it too as secondary, what is now the best approach?

Going to use this component, as it will expose all entities with commands, but what is the binding in Openhab? i am new to openhab