Home assistant checking if internet connection is online

Hello everyone - here’s my use case. Every so often the power company cuts off power, for some reason when power comes back on my router (Google WiFi) gets confused and remains offline. When I am home, no big deal, I unplug and plug it back in. If I am not home, the challenge is that I lose all remote access to my instance.

My idea is to plug the router into a ESP-home flashed plug, write an automation that checks periodically if I can ping an external website, and if not trigger a power cycle of the plug.

I have seen posts referring to the Ping sensor or even to use the Command Line Interface but can’t figure out how to apply that in my case.

Any suggestions ?

I’m using this binary sensor to detect internet unavailability (8.8.8.8 is google’s dns server):

- platform: ping
  name: Internet
  host: 8.8.8.8
  scan_interval: 15
2 Likes

Just thinking here… i might have a faulty way of thinking.
If power is cut off… everything is offline (powerless).

So when everything comes back online, i’d suggest you have a simple automation that triggers on home assistant boot up, maybe wait 3 to 5 min and power cycle some smart power plug to reboot the google wifi.

Don’t have to ping really… because if power is out… nothing is there to ping. When booting, its known for not to work so why not solve it that way?

Thanks @Vasco , I limited details in my use case to the necessary. The reality is that HA is running on a UPS so it does not lose power when power goes off, so is the Cable modem and so is the router. Sometimes when I lose power, cable internet goes down also, sometimes it does not…and sometimes when all is back up all works well and sometimes it does not… and sometimes for whatever reason Google Wifi shows as “offline” when it really is not…go figure, it all seems random to me. Hence the ask for a way to periodically check :slight_smile:

1 Like

Gotcha! Then maybe a simple ping to 8.8.8.8 is good indeed.

2 Likes

Is this a “sensor” entry in the configuration.yaml? What is the exact coding? Then how do you use this to notify you that the ping failed?

Yes

binary_sensor:
  - platform: ping
    name: Internet
    host: 8.8.8.8
    scan_interval: 15

You could send a notification to your phone, flash a light, TTS through speakers…endless possibilities

If the sensor goes to off you trigger the automation

Just bear in mind that some connections drop, and reconnect without issue. I’m with Vodafone broadband in the UK, and this is a pretty typical log:

3 Likes

Okay thank you so much. I’m not at all familiar on how to code in .yaml. Please help me code the alert to my mobile app if the binary_sensor is off. I have already ping code in configuration.yaml.

Thanks in advance.

trigger: when entity change to state ‘off’
action: call service notify

To do the notification, it is going to be an “if this than that” automation, which can be setup via your frontend UI… there’s an automation editor available for you.
No need to code anything in yaml.

I suppose you have tested the internet sensor. Did the test work?

Yes, the ‘binary sensor’ work. I created an automation via the UI when the “state” of the ‘binary sensor’ turns from on to off, I send a notification to my iPad device. But the only thing is, if I’m out of my LAN, I won’t be able to receive any notification from HA. I’m currently researching on SMTP to SMS (to my iPhone).

Thank you so much for the binary sensor configuration.

Thats really clean looking, can you share that config :slight_smile:

if you use official home assistant app it will handle notifications for you. So you can then just create an iOS notification and it will work weather you are on your LAN or not. You can check out how to set up IOS notifications here

Not to put a damper on things but if your internet is down you won’t be able to send anything out via notifications/SMTP/etc. So setting up that sort of automation is pretty useless I would say. Your only choice is to just power cycle the plug your router is on.

Edit: To clarify the original OP talked about being away from home. If you are home and the mobile app can get a notification from the LAN then you’ll get that notification. However, unless you have a local SMTP server and mail server that you use then SMTP wouldn’t do you any good either.

Yes, I’m aware of that. The binary sensor is a watchdog on my cable internet. I have an “internet-only” device from my mobile carrier that I will add to HA to send the SMTP-to-SMS alert to my iPhone! I am new to HA and I am hoping that I can implement my objective. Thanks for your thoughts on this.

My SMTP to Text is not working! I followed the HA guide on setting up SMTP but it does not work. Can anyone direct me to a thread that has SMTP to SMS working? Thanks in advance.

Note that I have successfully set up dual network connection. ETH0 wired to my home WiFi, WLAN0 to my Internet-Only mobile device’s HotSpot.

So when the Cable modem loses connection, I will be able to send a text message to my mobile phone using the HotSpot connection.

But wait, after I have success on setting up the SMTP-to-SMS, I need to be able to set up Routing in HA!!! My binary sensor to check for internet should be routed to the Home LAN router, then the notify using the SMTP-to-SMS should route connect to the HotSpot. Oh my…

You are most likely going to need to setup a static route for your SMTP service calls to always route through the WLAN interface, otherwise it’s good to get a lot more complicated to attempt to dynamically change your routing. Do you have a supervised install?

Thanks. But I’m not yet using WLAN0 for my SMS alert. I’m still on the ETH0 interface. I followed the instruction on setting up SMTP in HA to be able to send SMS via SMTP. It is not working. I will create another topic on SMTP setup. So whoever can help me, please visit my topic there.