I will mention that it works sometimes i.e. that state is marked as unavailable. I know that I can write an automation to achieve the desired result, but I thought this would happen automatically. Here’s my code:
Home Assistant does not maintain a constant connection to its sensors, and so the HA dashboard shows the last value received - as you have observed. I see you are using ESP32 deep_sleep, which actually powers the ESP32 off … so disconnection is normal expected behaviour.
I found it helpful when trying to work out if a device was faulty to turn on the “Last updated” secondary value in most of the Entities lists in my dashboards, like
You can see the the result in the preview pane on the right hand side. I have now turned on the Last Updated secondary info almost everywhere to give me confidence that the values are indeed being received by HA.
I notice that your ESP is awake for only 10 seconds every 5 minutes. Ten seconds is not very long … must be difficult trying to catch that window for doing updates OTA
I wonder if sometimes 10 seconds is not long enough for the ESP to power on, ESPHome to start up and connect to wi-fi … and so the “unavailable” you see may be that the ESP32 is still waking up. I have noticed my HA dashboard sensors show “Unavailable” briefly while my ESP32 is waking up. Or maybe it is that HA hasn’t received several updates in a row and has timed out.
Going off on a tangent, you might like to add a battery voltage sensor to your ESP32. I think it’s better to have a value, and be able to react before your battery goes flat. And how do you intend to handle a flat battery ?
Other than that, I think the logic to determine if the ESP has been disconnected too long will need to be in HA.
I’ve found that if I open the door, the sensor is constantly triggered and awake so OTA works fine.
I would like to know the battery voltage but it isn’t critical for this application. I will have easy physical access and these only need them to work for a finite time which is much less than the capacity of my batteries.