MQTT - How can I control devices on openHAB via Home Assistant?

Thanks for the information.

FWIW I was able to get things working, at least for ON/OFF switches, using Home Assistant MQTT SWITCH.

I will look into your suggestions as I may be a solution for other types of devices that MQTT SWITCH does not handle.

It appears the solution will be to use MQTT Switch and MQTT Light on Home Assistant.

MQTT Light is used for dimmers and allows additional parameters to be set other than just ON and OFF.

My question @rlkoshak how do I publish that additional information in openHAB to MQTT for the dimmers?

For example Home Assistant uses the following information for the MQTT Light (dimmers):

# Example configuration.yaml entry
mqtt:
  light:
    - name: "Office light"
      state_topic: "office/light/status"
      command_topic: "office/light/switch"
      brightness_state_topic: 'office/light/brightness'
      brightness_command_topic: 'office/light/brightness/set'
      qos: 0
      payload_on: "ON"
      payload_off: "OFF"
      optimistic: false

While openHAB from within it’s UI offers MQTT State Topic & MQTT Command Topic and the openHAB CODE shows the following:

 - id: bedroom_dimmer
    channelTypeUID: mqtt:dimmer
    label: Bedroom Dimmer
    description: ""
    configuration:
      retained: true
      postCommand: true
      min: 0
      qos: 1
      max: 100
      commandTopic: oh3/dimmer/bedroom_dimmer/cmd
      stateTopic: oh3/dimmer/bedroom_dimmer/cmd

Can I add to/edit that information in the CODE section of the openHAB UI so that it includes the required dimmer information for MQTT submission for Home Assistant?

If so will it be wiped out when I ADD CHANNELS to the THING in the openHAB UI?

You will need to create a separate Channel for the Dimmer topics.

Note: using the same topic for command and state almost always leads to infinite loops, especially when coupled with postCommand enabled. When you send a command to the Item, it publishes a message to the commandTopic. It receives that message on the stateTopic, commands the Item which causes it to publish again ad infinitum. -

OKAY. Thanks for that.

The following is going to be such a NOOB question.

How / when does the information get published to the STATE topic(s)

When I did the following I do not believe anything was published to the STATE topic.

commandTopic: oh3/dimmer/bedroom_dimmer/cmd
stateTopic: oh3/dimmer/bedroom_dimmer/state

And you are correct about the infinite loop. :upside_down_face:

OH doesn’t ever publish to the stateTopic. It only subscribes to the stateTopic. The stateTopic is how something else (HA) sends information to openHAB.

When OH receives a message on the stateTopic, it updates (or commands if postCommand is true) the Item with that new state.

When an OH Item receives a command, it will publish that to the commandTopic. The commandTopic is how OH sends information to something else (HA).

That makes sense BUT when I look at my configuration (see below) I get confused.

ON Home Assistant


  light:
    - unique_id: my_dimmer
      name: "My Dimmer"
      state_topic: "oh3/dimmer/my_dimmer/state"
      command_topic: "oh3/dimmer/my_dimmer/cmd"
      brightness_state_topic: "oh3/dimmer/my_dimmer/state_bright"
      brightness_command_topic: "oh3/dimmer/my_dimmer/cmd_bright"
      brightness_scale: 100
      payload_on: "ON"
      payload_off: "OFF"
      on_command_type: last
      qos: 1
      retain: true
      optimistic: false

ON openHAB

channels:
  - id: my_dimmer
    channelTypeUID: mqtt:dimmer
    label: My Dimmer
    description: ""
    configuration:
      retained: true
      postCommand: true
      min: 0
      qos: 1
      max: 100
      commandTopic: oh3/dimmer/my_dimmer/cmd
      stateTopic: oh3/dimmer/my_dimmer/state
  - id: my_dimmer_level
    channelTypeUID: mqtt:colorHSB
    label: My Dimmer Level
    description: ""
    configuration:
      commandTopic: oh3/dimmer/my_dimmer/cmd_bright
      retained: true
      postCommand: true
      qos: 1
      stateTopic: oh3/dimmer/my_dimmer/state_bright

Who “owns” the topics? Presumably it’s HA. What’s a command and what’s a state depends on who “owns” the topics. If you want to command HA to do something, you use the cmd topic. When HA has done something, it will post the new state to the state topic.

Therefore OH needs to publish (i.e. commandTopic) to the cmd topic and needs to subscribe (i.e. stateTopic) to the state topic.

Mosquitto is running on openHAB.

The physical devices are/were originally controlled by/connect to openHAB.

I have the MQTT Integration on HA configure to the MQTT Broker / Mosquitto on openHAB.

So am I wrong to believe that openHAB owns the topics?

What matter is what dictates that oh3/dimmer/my_dimmer/cmd_bright is the command topic? If it’s OH, then yes OH owns the topic. An in that case, OH needs to subscribe to the cmd topic and publish to the state topic. The cmd topic is where messages coming from outside OH come and the state topic is where OH publishes changes to the Item.

However, keep in mind that the binding will only publish commands so you might need a rule to translate changes to commands.

Where Mosquitto is running is irrelevant.

I have multiple 4 relays boards running Tasmota configured to connect to my local MQTT broker. My HomeAssistant instance uses Tasmota Integration to manage (ON/OFF) these relays.
On my OH3.3/3.4 instance have created 4 Channels shown in the attached Yaml file. I’m able to manage these relays from both OH and HA.

UID: mqtt:topic:nacBroker:nacRC01
label: nacRC01 - Generic MQTT Thing
thingTypeUID: mqtt:topic
configuration:
  payloadNotAvailable: offline
  payloadAvailable: online
bridgeUID: mqtt:broker:nacBroker
location: System
channels:
  - id: Relay1
    channelTypeUID: mqtt:switch
    label: Relay1
    description: ""
    configuration:
      commandTopic: cmnd/nacRC01/Relay1/POWER1
      stateTopic: stat/nacRC01/POWER1
      off: OFF
      on: ON
  - id: Relay2
    channelTypeUID: mqtt:switch
    label: Relay2
    description: ""
    configuration:
      commandTopic: cmnd/nacRC01/Relay2/POWER2
      stateTopic: stat/nacRC01/POWER2
      off: OFF
      on: ON
  - id: Relay3
    channelTypeUID: mqtt:switch
    label: Relay3
    description: ""
    configuration:
      commandTopic: cmnd/nacRC01/Relay3/POWER3
      stateTopic: stat/nacRC01/POWER3
      off: OFF
      on: ON
  - id: Relay4
    channelTypeUID: mqtt:switch
    label: Relay4
    description: ""
    configuration:
      commandTopic: cmnd/nacRC01/Relay4/POWER4
      stateTopic: stat/nacRC01/POWER4
      off: OFF
      on: ON

Regarding the Dimmer Channel - id: my_dimmer commandTopic is there a way publish ON if the dimmer value is greater than zero and OFF if the dimmer value is zero rather than a number?

HA is looking for ON / OFF as well as the brightness level.

Anything is possible through rules.

At the moment OH and HA will only update mosquitto and fail to update each others UI.

If I swap out state and state_bright with cmd and cmd_bright OH will update the UI of HA but HA will not update the UI of OH.

Furthermore using MQTT Explorer I do not see state or state_bright.

In MQTT Explorer I have the following:

oh3
   dimmer
      my_dimmer
       cmd
       cmd_bright

What am I overlooking?

It seems that this is an issue with state and subscribing.

In HA I have the following:

  light:
    - unique_id: my_dimmer
      name: "My Dimmer"
      state_topic: "oh3/dimmer/my_dimmer/state"
      command_topic: "oh3/dimmer/my_dimmer/cmd"
      brightness_state_topic: "oh3/dimmer/my_dimmer/state_bright"
      brightness_command_topic: "oh3/dimmer/my_dimmer/cmd_bright"
      brightness_scale: 100
      payload_on: "ON"
      payload_off: "OFF"
      on_command_type: last
      qos: 1
      retain: true
      optimistic: false

In OH I have the following:

channels:
  - id: my_dimmer
    channelTypeUID: mqtt:dimmer
    label: My Dimmer
    description: ""
    configuration:
      retained: true
      postCommand: false
      min: 0
      qos: 1
      max: 100
      commandTopic: oh3/dimmer/my_dimmer/cmd_bright
      stateTopic: oh3/dimmer/my_dimmer/state_bright

*** UPDATE ***

I created the following using MQTT Explorer:

oh3/dimmer/my_dimmer/state_bright

oh3/dimmer/my_dimmer/state

Set the values to 35 and ON.

Viewed the device in OH and HA they had the correct values but changing the dimmer levels in their UI(s) did not change the values of state or state_bright within mosquitto or each others UI.

Thanks. Created rule that published ON / OFF.

I am hung-up on the STATE_TOPIC.

The closer I think I get to figuring this out the further away I seem to be to solving this.

When I started this I set up an MQTT Thing should I have set up an HomeAssistant MQTT Component instead?

I am confused on how OH subscribe to topics is it the MQTT Thing that subscribes to the topic and not the channels?

If I use MQTT Thing does that mean I need to create a MQTT Thing for each switch?

If I use HomeAssistant MQTT Component will that handle all of my switches?

Step back.

Get out a piece of paper.

Chart out each and every data flow. That becomes your recipie for how to configure everything. Once you have all the messages and data flows mapped out it’s just a matter of configuring HA and OH to publish/subscribe each data flow. You can address them one by one until you have them all.

1 Like