Howe to trigger an automation when a device comes online

I've looked at the 'similar topics', the CookBook and other searches but none seem to fit my example.

I have an ESPHome device that only comes online every 30 minutes - it is in a deep sleep the rest of the time to save battery. I have defined a 'switch' in the HA interface that is read by the device when it comes on line to stop it from going to sleep so that I can do things such as a firmware update.

However as there can be a considerable time between clicking the switch and the device coming online (long enough for me to be distracted!!!) and several times the battery has run down before I've got around to doing the update.

I already have an automation that is triggered when a new ESPHome firmware update is installed and that will update all of the online ESPHome devices - except for this one.

What I'm looking for is a trigger that will fire when the device comes online (with a later condition that there is an update ready to be installed) so as to perform the action of toggling the 'switch' to stop the device going back to sleep, performing the update and then toggling the switch again.

However I don't seem to find the correct thing to act as the trigger for the automation. (I stupidly tried the Google AI that came up when I did a search for answered, but it just produced the usual slop that was totally wrong.)

Perhaps whatever ESPHome uses to say if a device is 'Offline' of 'Online' in the 'ESPHome Builder' page....

Thanks
Susan

Just pick any entity in the device and add a state trigger that fires when it changes from "unavailable" to any state.

As Clyde said, that's one option, but it will depend on the device. I have many battery-operated devices that won't show unavailable while sleeping. In that case, you'd need to use another way. If the device uses WiFi, you could use an integration for your router to see when it connects.

You could also use an actionable notification with an action to perform the update, if you use the companion app.

EDIT: Missed that it's an ESPHome device.

You could also have the d vice set a state of a sensor, like a time stamp maybe.

There is the [status binary sensor[(Status Binary Sensor - ESPHome - Smart Home Made Simple) that should be exactly what is needed. It should only be on when the device is awake and connected - at other times it will be off or unavailable.

Thanks @clydebarrow - your second suggestion works perfectly.