Binary sensor over mqtt stays off

Hi,

I’m experimenting with a magnet sensor (reed switch) on a ESP32 that I use with MQTT.
I’m going to put the sensor hidden in my window and pull the wires to the attic so it’s not visible.

I have the ESP32 configured correctly so it published a message for open/closed to the MQTT topic so that part is working.
However when I add the binary sensor to HA it won’t change status and only show that it’s closed.

This is my .yaml for the sensor:

binary_sensor:
  - platform: mqtt
    name: "Window Test"
    state_topic: "house/windows/publish"
    payload_on: "true"
    payload_off: "false"
    device_class: opening

However even if house/windows/publish is set to true it never changes in HA.
I’m using MQTT Explorer to se the messages and I can se that the ESP32 is sending true/false messages to the topic.

Is there something I’m missing?

I have around 20 switches that are communicating over MQTT so HA has a connection to the broker.

Is it sending true and false as string or as boolean values? Your configuration for the binary_sensor is expecting string values.

I send them as string. I’ve tried with open/close and on/off as well but didn’t work either.

All of the evidence you’ve provided suggests this should work:

  1. You have 20 existing MQTT Switches and all are working therefore Home Assistant has a working connection to the MQTT broker.
  2. MQTT Explorer confirms the topic house/windows/publish is receiving true and false payloads.
  3. The binary_sensor is configured to recognize true as being the payload for on (and false for off).
  4. The binary_sensor entity appears in Home Assistant so the configuration is valid.

Each link in the chain has been confirmed to be operational:
[Device] <---> [MQTT broker] <---> [Home Assistant Binary_Sensor]

Nevertheless, the binary_sensor fails to change state and always remains off. It suggests the entity is not, in fact, receiving payloads via its subscribed topic or the payload differs from what it expects to receive. :man_shrugging:

Bah, it was the / in the beginning of the topic that did it…
Thank you for your assistans! :slight_smile:

1 Like

So this proved to be true:

None of the MQTT topics you posted here contain a leading slash /. Nevertheless, something had to wrong with one of the four pieces of evidence you provided … and it turned out to be the MQTT topic.

It would helpful to others if you marked my post (above) as the Solution in order to make it easier to find for other people who may encounter the same problem. Thank you.

It was a leading slash in the code of the mqtt topic on the ESP32. I should have been more specific,sorry about that.

You don’t need to be sorry, people make mistakes. However, you have marked the wrong post as the Solution.

The source of the problem was that the sensor was not receiving payloads via its subscribed topic. This was identified here.

It was identified even though the evidence you presented was misleading. You claimed to be using an MQTT topic without a leading slash when, in fact, it did have one.

The analysis I presented indicated each link in the chain is operational, yet the binary_sensor fails to change state. Therefore one of the links remains broken and some of the details you presented had to be incorrect. I surmised it was either the topic or the payload. It proved to be the topic.