Integration OH3 and HomeAssistant via MQTT binding

Hi. I run OH as main automation system and HA as secondary automation system since it has additional bindings/integrations not available in OH. I tried to integrate both via MQTT since this is supported by OH and the relevant binding was developed - see relevant page.

I have not found any end2end instruction, so this integration was not straight forward for me as I actually also beginner in MQTT protocol. So I just want to share my experience as I finally got it working with “some warnings”. I did not get HomeAssistant MQTT binding to work, but Generic MQTT Thing worked (almost) fine.

Hope this info will be useful for someone.

Configuration steps:

  1. HA. Install and then configure MQTT Mosqitto broker. Documentation is here.
    Configuraiton can be done manually by editing configuration.yaml or by using HA user interface (Configuration >Devices and Services > Mosquitto broker > (re-)configure
    Set also username and password for security.

  2. Install MQTT Explorer utility. A very useful tool for monitoring of MQTT topic data. It can also be used to verify that HA broker is actually running:
    image

  3. HA. Create necessary MQTT devices. As an example we will create a virtual number device by adding following number device in configuration.yaml

number:
  - platform: mqtt
    command_topic: "homeassistant/number/mynumber1/cmd"
    name: "Bedroom Temperature"
    state_topic: "homeassistant/number/mynumber1/state"
    unique_id: number111111111111111111
    min: 1
    step: 1
    max: 100
  1. OH. In OpenHab create an MQTT Broker thing and connect it to HA Mosquitto broker. You will need to fill the IP adress of your HA instance, port 1883 (standard MQTT port), username, password. State of the new thing must be “Online”.

  2. OH. Now create the new Generic MQTT Thing and put the MQTT Broker as bridge.
    Create new number channel and fill relevant configuration.


    Once created, create also the relevant number item for the newly created channel. At the end you will have:
    image¨

That’s it. It worked for me. The only thing still not working - status of Generic thing. The availabilty topic should be homeassistant/status and payload is online, offline - but for some reason state is constantly offline in OH.

Verification of data flow HA > OH

  1. HA. Publish the state change message to the number device topic “homeassistant/number/mynumber1/state” that we previously assigned to MQTT number device in HA.
    This can be done f.ex. via Developer Tools in HA:

  2. Check that the value really reached the MQTT broker by looking into MQTT Explorer:
    image

  3. OH item will now also be updated:

Verification of data flow OH> HA

  1. Write a short OH script calling MQTT broker:
val mqttActions = getActions("mqtt","mqtt:broker:1b72424470");
mqttActions.publishMQTT("homeassistant/number/mynumber1/state","44", true);

Do not forget to put actual ID of your OH Broker thing of course. Execute the script.

  1. Check that MQTT_Number1 item is 44 in OH:
    image
    it reached the HA broker:
    image

  2. Check HA number got the update:

4 Likes

Thanks for posting! I’ve moved this to a more appropriate category so it will be easier for users to find.

One more example with integration via the Selection virtual device (list of choices) used for different alarm options I used in my house.

Task is to integrate the following selection between two systems

  • OFF
  • INFO
  • ARM_DOORS
  • ARM_ALL
  • ARM_ALL_IN_5_MINUTES
  • SIREN

Necessary actions in OpenHab

  1. Create new Generic MQTT thing or just re-use one created in previous example.
  2. Create new channel with type “Text value”. Fill according to below picture:
  3. Create new String item “MQTT Arm State” for this channel
  4. In sitemap (just as an example) create the necessary mapping) f.ex.:

Necessary actions in HomeAssistant

  1. Create new “MQTT Select” item by appending this code into configuration.yaml:
select:
  - platform: mqtt
    command_topic: "homeassistant/select/arm_state/cmd"
    state_topic: "homeassistant/select/arm_state/state"
    name: "Arm State"
    options:
    - "ARM_OUT_DOORS"
    - "ARM_DOORS"
    - "ARM_ALL"
    - "ARM_ALL_IN_5_MINUTES"
    - "OFF"
    - "INFO"
    - "SIREN"
    retain: true
    optimistic: true
    unique_id: select_arm_1
  1. Create rule that will synchronize values of cmd and state once cmd topic is updated for MQTT Select item. This is needed since OH String item only listens the state topic while changing selection in HA only updates cmd topic.
    Append this code into automations.yaml:
- id: '1641586360163'
  alias: Sync MQTT Arm State Cmd to State
  description: ''
  trigger:
  - platform: mqtt
    topic: homeassistant/select/arm_state/cmd
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: homeassistant/select/arm_state/state
      payload_template: "{{ trigger.payload }}"
  mode: single

WORK DEMO 1: Update HA Item in UI will update OH Item

image > image >
image

WORK DEMO 2: Update OH Item in iOS app will update HA Item

> >
image >
image

1 Like

Hey @Bender1 , thnx, was already helpfull, i’m also new to Openhab, and was struggling with the state too , for a simple light in my case … sending payload to turn on/off is already working,
can you have a look for me? seems you were struggling too with state?

Have been attempting to synchronize openHAB and Home Assistant via MQTT and I am struggling with the STATE topics.

STATE is never updated. This lack of update results in the openHAB and Home Assistant user interfaces to be out of sync.

If I force the STATE to be updated via a rule/script will it result in a runaway loop turning the lights on and off?

What component are you using? I use a custom component to send the states of my lights to mqtt, it can also receive payloads to turn on/off

i use this one in HA: MQTT DiscoveryStream integration - Custom Integrations - Home Assistant Community

its the only one where you can send a payload to to turn on/off something, and it publishes the state too
in openhab i just use regular mqtt binding

Thanks for the information I will look into that.

I am hung up on the STATE topic as some have said that the actual device should issue it’s STATE and that I should not need to publish it to MQTT.

that page doesnt work :slight_smile:

But try that custom component, its the other way around, and its HA thats creating the MQTT item correctly
i use HA for main purposes and openhab for limited stuff

Using MQTT Explorer

Sending ON to oh3/switch/back_door_light/cmd does NOT turn on the light.

Sending ON to oh3/switch/back_door_light/state turns on the light.

AND it updates both the openHAB and Home Assistant UI

Using openHAB UI

ON is published to oh3/switch/back_door_light/cmd and the light turns on.

oh3/switch/back_door_light/state is not updated or changed nor is the Home Assistant UI updated or changed.

Using Home Assistant UI

ON is published to oh3/switch/back_door_light/cmd however the light DOES NOT turn on.

oh3/switch/back_door_light/state is not updated or changed nor is the openHAB UI updated or changed.

FURTHERMORE the Home Assistant UI back_door_light switch automatically reverts to the STATE found within oh3/switch/back_door_light/state

In openHAB

 - id: back_door_light
    channelTypeUID: mqtt:switch
    label: Back Door Light
    description: ""
    configuration:
      commandTopic: oh3/switch/back_door_light/cmd
      retained: true
      postCommand: true
      qos: 1
      stateTopic: oh3/switch/back_door_light/state

In Home Assistant

    - unique_id: back_door_light
      name: "Back Door Light"
      state_topic: "oh3/switch/back_door_light/state"
      command_topic: "oh3/switch/back_door_light/cmd"
      payload_on: "ON"
      payload_off: "OFF"
      state_on: "ON"
      state_off: "OFF"
      optimistic: false
      qos: 1
      retain: true

I am going in circles with the STATE of the STATE. I do not know where it comes from or what should be committing it to MQTT STATE TOPICS. Was told that STATE topics were the responsibility of the devices and what about SUBSCRIBING to topics is that done system wide or is that on a device/channel bases?

thats indeed verry strange , but sorry, i’m beginner user in openhab, so not have a lot of experience here, but i believe i had it working using that custom component, at the moment i dont use it anymore, i just tested it for later when i have some more time

but i still have copy/pasted my config that i use for later

in HA i just have lights/swiches that i expose to mqtt with that discoverystream
and this was my config in openhab that was working in both directions

# Openhab:

# UID: mqtt:topic:e94f1500bd:dad57a579d
# label: Eetkamer
# thingTypeUID: mqtt:topic
# configuration: {}
# bridgeUID: mqtt:broker:e94f1500bd
# channels:
  # - id: eetkamer
    # channelTypeUID: mqtt:switch
    # label: Eetkamer
    # description: ""
    # configuration:
      # commandTopic: homeassistant/light/eetkamer/set_light
      # formatBeforePublish: '{ "state" : "%s" }'
      # stateTopic: homeassistant/light/eetkamer/state
      # transformationPattern: JSONPATH:$.state

i needed to send JSON values instead of just plain text like ON or OFF
i need to send like :

{ “state”: “ON” }

Spooky. It’s like you were reading my mind as I was wondering why you did that.

Is set_light and state reserved words for your devices or is it just what you came up with?

Thanks for your time.

Hmm, not sure, I didn’t invent it :+) but that’s the logic how it’s handled in HA , so I used the same in openhab