Binary sensor status after deep sleep flakey in HA

My ESP32 is configured to wake up from deep sleep based on a magnetic reed switch opening and closing and then goes back to sleep after 2s.

This works perfectly and it shows the state of the switch ( which is configured as binary sensor) correctly initialized in the logs, it connects to wifi and sets up the HA api, however its state as seen by my Home Assistant instance is very flakey - sometimes it gets updated and sometimes it doesn’t.

How can I fix this? Can I perhaps publish the state periodically somehow or some other alternative?

Use on_boot and component.update.

2 seconds is probably not enough time to connect to wifi. Try increasing your awake time.

1 Like

This doesn’t work for a GPIO binary sensor. It says “GPIOBinarySensor does not inherit from PollingComponent”.

I have tried 5s, which didn’t change anything. From the logs it is definitely connecting to wifi in under a second each time.

Tried 10s and it seems better. I guess the issue is not connecting to wifi, but connecting to the HA instance which can sometimes take a little longer.

In an ideal world I’d wait until it connects to HA and sends the data and then immediately go back to sleep (instead of waiting some relatively long time and hoping for the best). Is that possible?

I guess the ideal world wasn’t that hard to do. This works together with a long stay awake time:

  on_boot:
    priority: -100
    then:
      - wait_until:
          api.connected
      - delay: 1s
      - deep_sleep.enter: deep_sleep_1

Thanks for the hints!

If you make sure to define a nearby SSID you can use wifi fast connect. That should reduce your on time.

https://esphome.io/components/wifi.html
Screenshot 2022-02-11 at 01-09-05 WiFi Component

1 Like