Automation to reboot when no internet

I have many power outages and the router is slower than the rpi3B+ where I have installed Home Assistant. This creates a problem: HA can’t comunicate to the exterior if this happens, so I have to reboot it manually (on-off the current) and then I can control it from the app.

I would like to have an automation where if I can’t access from the exterior HA restarts itself with

action:
  - service: homeassistant.restart

I don’t kno which condition can be useful, neither for the condition.

Thanks in advance to everyone that can help me.

I have seen that with Nginx Proxy Manager I can have the local IP accesible at the same time that a custom one.

This can be closed now.