MQTT Switch State

In my use case an MQTT-Switch changes its state like this:

on → undefined → off and
off → undefined → on

Is there a possibility to ignore the “undefined” state?

You could set up a trigger-based template binary sensor that triggers on your switch going on or off. With the new template layout:

template:
  - trigger:
      - platform: state
        entity_id: switch.mqtt_switch
        to:
          - 'on'
          - 'off'
    binary_sensor:
      - name: "Filtered MQTT switch"
        state: "{{ states('switch.mqtt_switch') }}"

Thanks a lot! For now I could fix it on the publisher side. But good to know how it would work in HA!

You can also use the MQTT Switch’s value_template to properly handle the three received states. Or its availability_template (which would allow it to ignore a received value of undefined).