How to make a switch determine binary state by only comparing to payload_on (state_on)?

Hello.
I have a device that publishes its state into MQTT topic. There are three possible states: 1, 2, 3, for simplicity. Even though the device may be in any of the listed states, I want home assistant to read binary state “ON” if the publushed state is 3, otherwise “OFF”.

As far I as observe, however, even when “state_off” is not set, when the publiched state does not match “state_on” (docs), it still tries to match “state_off”, and in case of mismatch, the switch state in the UI just reverts back to the previous state.

For example, by default the switch in UI is off, while the device is in either state 1 or 2, I toggle the switch, which triggers the device state to change to 3, and the switch is toggled “ON” in the UI. However, when I toggle switch OFF in the UI, which triggers the device state to change to either 1 or 2 (deterministic, but doesn’t matter which one), the switch in the UI is toggled OFF for a second, and then reverts back to ON, while the actual device state is now 1 or 2, and I expect the switch to be OFF.

Configuration tried:

mqtt:
  switch:
  - name: "Toggle"
    availability:
      - payload_available: 1
        payload_not_available: 0
        topic: "online"
    state_topic: "state"
    state_on: "3"
    # state_off: null ?
    command_topic: "command"
    payload_on: "set_3"
    payload_off: "set_2"

How to make a MQTT switch determine the state by only comparing to state_on, and ignoring state_off?

Try with optimistic: true

1 Like