Detect reboot to restart home assistant

I’m sorry if the title is very confusing and this will not be an issue faced by 99.9999% of the people.
But i would like to solve this if possible.

I’m running home assistant instance at my home. Unfortunately due to very bad management by my country’s politicians, and lack of fuel to run thermal power plants - our country is having pretty bad power cuts at this moment.
My UPS won’t survive a 4-6 hour power cut, so I’m turning everything OFF once the power is gone and restart everything once it’s back.

My issue is that every time home assistant starts, it will say some of the devices are offline (such as my Shelly EM) - the only good/reliable way i found so far to solve this is to restart the home assistant after the host boot (running supervised instance on PI 4). Rebooting the devices are one option, but rebooting the Shelly EM requires me to turn power on and off to the whole house. And no one likes that after 6 hour power cut.

Anyway this can be automated?

I don’t have a solution, but I am looking for something similar. In my case sometimes after a restart Wifi connections seem broken - it is working though. All integrations using the cloud are offline and HomeKit is unavailable. Once I restart HA it works.

My idea is to trigger on that somehow. But of course only once so it doesn’t keep restarting if internet is unavailable.

Little bit late to the party but stumpled across this during searching the forums.

Did you find a solution?
You could enable a helper input_boolean when the UPS shutdown triggers and use the Uptime Integration to detect a recent start and trigger a reboot if the input_boolean helper is turned on and the uptime timer hits a specific time.

Following template returns the minutes since last restart:
{{ ((as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) / 60) | int }}