Binary_sensor in MQTT attached ESPHome device won't compile

I am trying to code a binary sensor in ESPHome device that uses MQTT rather than the HA API (a D1_mini) but it won’t compile:

binary_sensor:
  - platform: mqtt
    name: "prevent-deep-sleep"
    topic: "homeassistant/input_boolean/prevent_deep_sleep/state" 
    payload_on: "on" 
    payload_off: "off" 
    internal: True
    id: "prevent_deep_sleep"

The error is:

INFO ESPHome 2025.6.1
INFO Reading configuration /config/esphome/thermo-hygro.yaml...
INFO Unable to import component mqtt.binary_sensor: No module named 'esphome.components.mqtt.binary_sensor'
Failed config

binary_sensor.mqtt: [source /config/esphome/thermo-hygro.yaml:123]
  
  Platform not found: 'binary_sensor.mqtt'.
  platform: mqtt
  name: prevent-deep-sleep
  topic: homeassistant/input_boolean/prevent_deep_sleep/state
  payload_on: on
  payload_off: off
  internal: True
  id: prevent_deep_sleep```

I am very confused!
. I have tried deinstalling/reinstalling esphome add-on (version 2025.6.1)
. retyping the code in case of hidden characters
. done a full hardware reboot

Has anyone any ideas please?

For my curiosity, from where you found documentation for mqtt binary sensor?
Try with state_topic instead of topic.

See the example that uses MQTT in this topic:

Thanks, but forget what the whole program is supposed to do. I am trying to get a switch in HA to be available in ESPhome as a binary sensor. I am converting a working ESPhome device that uses the API to MQTT.

Well, various places including here in the forum (though I am damned if I can find it again).

Is the switch define as a MQTT switch? If so just subscribe to its command topics.

If is using some other platform, write an automation that is triggered on state change, and publishes that state change using the mqtt_publish.

You subscribe to MQTT topics on ESPHome using mqtt: on_message;, e.g

mqtt:
  # ...
  on_message:
    topic: my/custom/topic
    qos: 0
    then:
      - lambda: |-
         if (x == “on” ) ….

# x contains payload

Haven’t seen any. Neither any documentation.
So are you sure this is valid?

Thats exactly my point. I am not sure its valid at all. I found it somewhere, copied and pasted it and it looks to me to be bull! Hence, confused!

Sensor component has platform: mqtt_subscribe.
Binary sensor according to docs doesn’t have any mqtt platform.

might answer your question.

Looks like you are using syntax that was deprecated some time ago. It is usually a good idea to review the documentation for any of the commands you are using to make sure you are following the current way of doing things.

HA actually has mqtt binary sensor.

For esphome it’s more confusing. There is library
https://api-docs.esphome.io/mqtt__binary__sensor_8h_source
Someone with better skills than I have could find a way to use it…