Esphome - gate sensor with deep sleep

Hi,
I am working on a gate sensor using simple magnetic sensor with one wire connected to GPIO and the other one to ground - it works great with connected to power. But the whole point of that thing is to be running of a battery monitoring garden gate.

The config is simple:

binary_sensor:
#Reed sensor
  - platform: gpio
    pin:
      number: 18
      mode: INPUT_PULLUP
    name: "Garden Gate"
    device_class: door

deep_sleep:
  id: deep_sleep_gate
  run_duration: 180s
  sleep_duration: 10min
  wakeup_pin: 
    number: 18

mqtt:
  broker: ip_address
  username: mqtt_user
  password: mqtt_password

please ignore the run and sleep duration times as they are set for testing.
The main issue is that without mqtt it takes about 20-25 seconds for the device to wake up and report the status.
That is why I have added MQTT - that is much more responsive and within 5 seconds after wakeup it reports the door open - however when it goes to sleep the “Garden Gate” sensor status is being reported by mqtt as unavailable. Is there a way to change that so it reports the last known status?
Thanks

I know nothing about MQTT, but at least one solution would be to have an automation which gets triggered on the valid states (open / closed I assume) and then copies the state over to an input_boolean. (you could also use an input_select or a template sensor)

Hi
Does adding the retain line help?

mqtt:
  broker: ip_address
  username: mqtt_user
  password: mqtt_password
  retain: true

Setting the MQTT Birth and Will message to blank stops the device from going Unavailable while it is asleep

mqtt:
  broker: *redacted*
  username: *redacted*
  password: *redacted*
  birth_message:
  will_message:
1 Like

Of course. I mangle the will and birth in mqtt for a temp sensor that goes into deep sleep so that the graph just shows the last temp until it updates, and doesn’t leave blank spaces. Forgot about that :face_with_hand_over_mouth: