Trying to update toggle state of input boolean w/ MQTT data?

Created several INPUT BOOLEAN TOGGLES that trigger automatons that publish ON / OFF to a openHAB Mosquitto host which then turns associated lights on and off.

Would like the input boolean toggles on the Home Assistant UI to automatically update their state and be in sync with the physical switches when the switches are controlled via openHAB or manualy turned on or off.

Is there a method to subscribe an input boolean toggle to a topic?

Also how can it be structured to prevent a loop?

If I understand your explanation correctly, you have a switch in openHAB that you want to display and control in Home Assistant. Both systems should be able to control the physical switch and both systems should display the switch’s current state.

If that’s what you want then you should use the MQTT Switch integration (not input_booleans with automations).

Let me know if you need my help to configure an MQTT Switch. I would need to know the two topics used for receiving and controlling the switch as well as the payloads that represent its on and off states.

Hi Taras

Yes openHAB is controlling multiple physical switches.

I have created Generic MQTT THING w/ Channels which are linked to the appropriate THING switch.

That is the objective.

Help is always appreciated.

( homeassistant/switch/backdoorlight/state ) is the topic that I am using and it seems to be working.

( ON ) or ( OFF ) is the payload/message

I “think” I have it working now. I was having issues with state_topic and command_topic.

Was able to get it working by having state_topic and command_topic have the same topic.

I had looked at MQTT SWITCH briefly before but with my limited mqtt knowledge and integrating with UI I moved on quickly thinking it should not be this difficult to do this.

Thanks for directing me back to MQTT SWITCH.

1 Like

Will MQTT LIGHT allow me to control dimmer switches hosted on openHAB in a similar fashion?

Yes. MQTT Light provides three different ways (schemas) for configuration. The default schema is adequate for most situations. Here are all of the MQTT topics related to controlling brightness using the default schema.

MQTT Light does not seem to be updating the UI of Home Assistant.

I can control the DIMMER associated with openHAB via the Home Assistant MQTT Light UI but when I control the DIMMER from openHAB the HA UI is not updated. Also the ON/OFF toggle done not turn on or off when moving the percentage bar of the HA UI

    - unique_id: dimmer
      name: “Dimmer”
      state_topic: "homeassistant/switch/dimmer/cmd"
      command_topic: "homeassistant/switch/dimmer/cmd"
      brightness_state_topic: 'homeassistant/switch/dimmer/cmd'
      brightness_command_topic: 'homeassistant/switch/dimmer/cmd'
      command_topic: 'homeassistant/switch/dimmer/cmd'
      state_topic: 'homeassistant/switch/dimmer/cmd'
      brightness_scale: 100
      payload_on: "100"
      payload_off: "0"
      on_command_type: brightness
      qos: 1
      retain: true
      optimistic: false

Usually, one topic is used for receiving the device’s status and a separate topic is used for publishing commands to the device.

In your case, you’re using a single topic for both receiving status from the device and for controlling it. Effectively, any command it publishes is immediately received as status … as opposed to receiving the actual status from the device.

In addition, Home Assistant reserves the use of a topic starting with homeassistant for the use of MQTT Discovery. It’s probably not a good idea to use that root topic for anything other than MQTT Discovery.

Within the openHAB UI there are only two fields available MQTT State Topic and MQTT Command Topic.

If I recall correctly when I used the following it did not seem to change the UI between the two platforms or operate the switchs hosted on openHAB from the Home Assistant UI.

state_topic: "homeassistant/switch/dimmer/state"
command_topic: "homeassistant/switch/dimmer/cmd"

I will update my topics for correct functionality.

Will change homeassistant to oh3

How do I receive the status from the device?

How does the state_topic get set?

At this point I am going in circles and have run into a dead end.

This is what I am working with now:

On Home Assistant

    - 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

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

openHAB can update and control the dimmer and Home Assistant UI BUT I scripted the publishing of the STATE so the MQTT state topics are being supplied by script code and not the device.

I would like to get the STATE topics via the proper method and need additional assistance to make that happen.

If I have correctly understood openHAB’s light configuration, it controls the light exclusively using a value between 0 and 100, inclusively. In other words, it doesn’t need to use commands like ON or OFF to control the light, just brightness levels.

I believe the equivalent in Home Assistant is this:

  - name: My Dimmer
    state_topic: oh3/dimmer/my_dimmer/state_bright
    state_value_template: "{{ 'ON' if value | int(0) > 0 else 'OFF' }}"
    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
    on_command_type: brightness

on_command_type is set to brightness to indicate to Home Assistant that the physical light only needs brightness values to control it.

state_topic is set to the same topic as brightness_state_topic. However, state_value_template is also employed to convert a received integer value to ON or OFF (which are the default values Home Assistant uses for interpreting the physical device’s current state).


This line in the configuration probably doesn’t do anything to control your physical lights. However, we can’t remove it because, according to the documentation, command_topic is a required option.

    command_topic: oh3/dimmer/my_dimmer/cmd

I suspect Home Assistant may not publish anything to that topic when on_command_type is set to brightness. I suggest using an MQTT client to monitor the topic just to confirm if Home Assistant does/doesn’t publish anything to it.

You are correct which is why I created a script to insert ON / OFF in a cmd and state topic. I will try your state_value_template.

Which brings up another question is there a method for the range for the slider to be between 0 - 100 and not 1 - 100. (I constructed a script that treats 1 as 0 and sets the switch to OFF.)

The thing I am really struggling with is the STATE. Where does the STATE come from and how is it inserted into the STATE topic. I need the "True Idiots Guide to MQTT"

You are correct again. I actually had to swap cmd and state topics around in HA to get the UIs sort of sinking up. (Home Assistant MQTT Light below.)

Never get anything auto-magically appearing in the STATE items. Concerned that I do not have my configuration setup correctly to receive information from the DEVICES to populate MQTT. Can you step me though it or point me to that "True Idiots Guide to MQTT"

I’m losing track of what does/doesn’t work. What were the results of testing the configuration I proposed in my previous post? Specifically, the behavior using the state_value_template.

It’s the combination of the following options:


that determines the entity’s state value. In the example I posted, it uses state_value_template to convert the received brightness value into the default values for payload_on (ON) and payload_off (OFF).

The brightness_scale option represents the brightness range from 0 to the value specified (you can’t specify a different lower boundary value).

Will perform some test over the weekend and get back with you.

Have conducted a number of “experiments” and I seem to be going in circles.

From Home Assistant I can control the lights by publishing STATE topics and the lights will turn on and off as well as adjust brightness levels. These STATE commands which are published also update the UI within openHAB.

From openHAB UI I can control the lights on/off/brightness levels and COMMAND topics are published. However the STATE topics are never updated which causes the Home Assistant UI never to update and have the correct current state of the light.

If I attempt to update the STATE topics weird things start to happen and the dimmable lights start ramping up and down after adjusting the brightness level.


Question: Was your original question resolved by using the MQTT Switch integration? Or was that never employed and you moved on directly to attempting to implement MQTT Light?