HA MQTT binary state does not correspond

This is my Tasmota module debug log:

19:58:12.195 MQT: stat/Poort/RESULT = {"Switch2":{"Action":"OFF"}}
19:58:14.296 MQT: stat/Poort/RESULT = {"POWER":"OFF"}
19:58:14.300 MQT: stat/Poort/POWER = OFF
19:58:25.518 MQT: stat/Poort/RESULT = {"Switch3":{"Action":"ON"}}
19:58:32.407 MQT: stat/Poort/RESULT = {"POWER":"ON"}
19:58:32.411 MQT: stat/Poort/POWER = ON

configuration.yaml:

binary_sensor:
  - platform: mqtt
    name: Poort gesloten
    state_topic: "stat/Poort/RESULT"
    off_delay: 2 # any number of seconds you want
    payload_on: '{"Switch2":{"Action":"ON"}}'
    payload_off: '{"Switch2":{"Action":"OFF"}}'
  - platform: mqtt
    name: Poort helemaal open
    state_topic: "stat/Poort/RESULT"
    off_delay: 2 # any number of seconds you want
    payload_on: '{"Switch3":{"Action":"ON"}}'
    payload_off: '{"Switch3":{"Action":"OFF"}}'

I suspect in Lovelace to see ‘Poort gesloten’ as OFF and ‘Poort helemaal open’ as ON but both are off:
Schermafbeelding 2021-03-14 om 20.08.49
If I check the entity state in Developer Tools, both sensors are ‘off’ while Switch3 is ON
Already restarted HA but result remains the same.
What’s wrong?

The issue is that the on and off payloads are in json and so you have to mention the value.json for extracting the on and of values. Please try this configuration and see

binary_sensor:
  - platform: mqtt
    name: Poort gesloten
    state_topic: "stat/Poort/RESULT"
    off_delay: 2 # any number of seconds you want
    payload_on: "ON"
    payload_off: "OFF"
    value_template: "{{ value_json['Switch2']['Action'] }}"
  - platform: mqtt
    name: Poort helemaal open
    state_topic: "stat/Poort/RESULT"
    off_delay: 2 # any number of seconds you want
    payload_on: "ON"
    payload_off: "OFF"
    value_template: "{{ value_json['Switch3']['Action'] }}"

Hi @sheminasalam, thanks for your answer!
I tried your code and the sensor status changed but came back after 2 seconds to ‘OFF’
As I want a ‘follow’ function, I removed the off_delay: 2 and it works now!
THANKS

1 Like