How to immediately detect when an Esphome device is online/offline?

I have a timer controlled pump built into my tankless water heater, and I’d like to know how often it runs and how long it runs every time. To do that, I plugged a ESP8266 smart plug flashed with ESPHome and added it to Home assistant. My idea was to use the “Connected” status of this ESPHome device and calculate the statistic of that entity. However, there’s always an unpredictable delay between when the device is powered and when HA detects it.

May I ask for any brainstorming how to detect the online/offline event with minimal delay from HA? Thanks!

Another idea is to have the ESPHome device sent an MQTT message periodically, e.g., 1Hz, basically a watchdog and use the MQTT Will to detect its offline event. But I’m looking for any native/elegant idea! Thanks!

Try using fast connect in the wifi settings. That may shorten the delay.

https://esphome.io/components/wifi/

Yea I’m already using that. I guess the unpredictable delay may come from how often HA loops to detect the status of the device? Both online and offline case.

What’s the relation to “connected time”? Esphome device does the job programmed, connected or not. If your approach is to power esp together with pump, you could use Uptime component (Time Since Boot).

Good point! I forgot the uptime sensor. It should be more accurate