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:

1 Like

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