MQTT Switch not switching

Hi,

I have created an MQTT switch:

switch:
  - platform: mqtt
    name: "Flur Bewegungsmelder an/aus"
    state_topic: "ioBroker/0_userdata/0/Flur_Bewegungsmelder_aktiv"
    command_topic: "ioBroker/0_userdata/0/Flur_Bewegungsmelder_aktiv/set"
    payload_on: "true"
    payload_off: "false"
    state_on: "true"
    state_off: "false"
    qos: 0
    retain: true

It properly shows up under entities. However I have some issues with it.

  1. The state is wrong. In MQTT explorer it shows it is true, however in HA it is false (off).
  2. When I switch it on in HA, it shows that something is sent to the MQTT server. However then after maybe 2 seconds, it goes back to false/off in HA again. In case I set it manually to false in MQTT Explorer, and then press the switch in HA, it changes it to true in MQTT Explorer and stays.

Any ideas how to get this running? :slight_smile:

Thanks,
Johannes

First of all : setting retain: true on the switch definition causes the command to be retained. Are you sure you want this ?

But for your problem : using mqtt explorer, check that your device answers within 10 seconds with true/false on the topic

ioBroker/0_userdata/0/Flur_Bewegungsmelder_aktiv

when you issue a command on the topic

ioBroker/0_userdata/0/Flur_Bewegungsmelder_aktiv/set

Ok, I set retain to false now. Does not make sense to set the retain flag. :slight_smile:

Tried what you described below and when it change the /set topic, there is no update in the

ioBroker/0_userdata/0/Flur_Bewegungsmelder_aktiv

topic. Probably this is the reason?!

Johannes

That is the reason. HA needs to be informed on the state topic when you issue a command on the command topic. If that does not happen, the switch will go back to the previous position.

Ok, that helped a lot. I had activated the option to create the set topic separately in my main system (ioBroker). But it does not seem to work quite well.

I changed it to this now:

switch:
  - platform: mqtt
    name: "Flur Bewegungsmelder an/aus"
    state_topic: "ioBroker/0_userdata/0/Flur_Bewegungsmelder_aktiv"
    command_topic: "ioBroker/0_userdata/0/Flur_Bewegungsmelder_aktiv"
    payload_on: "true"
    payload_off: "false"
    state_on: "true"
    state_off: "false"
    qos: 0
    retain: false

And with that it lets me control the value in MQTT.