So this morning I woke up to no internet service, which is rare as I have fiber. While this doesn’t bother me all that much as the majority of my gear and media is local to my network, it got me thinking that I want to receive a notification when my internet access has been restored. I’m not sure fully how to go about this and could use the communities help.
I want to receive a notification when home-assistant detects that it can ping a cloud server (says google) which I could do via a binary sensor. But what I’m not sure how to do is how do I track the amount of time between the first bad ping and the first good ping, as I want the notification to tell me how long my internet service has been down for?
I’ll probably set up the ping to be every 5 mins just to help filter out any intermittent drop outs.
Your can use a input_boolean to track the time. When ping fails then turn it off. When its back to normal than calculate the down as follows and turn the input_boolean to on.
{% set down_time = (((as_timestamp(now()) - as_timestamp(states.input_boolean.internet_connection.last_changed))) / 60) | int %}
{%- if down_time == 1 -%}
"Internet was down for 1 minute"
{%- else -%}
"Internet was down for {{down_time}} minutes"
{%- endif -%}
But that sensor will only let you know that Google’s DNS is up.
What if Google’s DNS goes down, yet your Internet connection remains up? Rare, probably, but could happen.
Ping the ISP default gateway on the WAN side instead. Or even better, use two or three ping sensors to ping several IPs such as the default gateway on the ISP side, the next hop, and your ISP DNS.