Detect internet is down and reboot modem

Hi,

Looking for a method to detect if the internet link is down and reboot the modem.
Have a sensor configured which runs a script that pings and returns a state of “UP” or “DOWN”, that works fine.

Need a method to count say 10 consecutive “DOWN” states, then toggle a GPIO pin on the PI, which then power cycles the modem and resets the counter. If in the “up” state the counter resets to 0 and therefore does not reboot the modem. It would also be nice to display the counter within HA.

Any help greatly appreciated. Happy to even pay someone to tutor me through this…

Rob.

1 Like

You can set the trigger of your automation to look out for a certain state with a certain duration.
For example to trigger the reboot only after being down 5 minutes

http://pastebin.com/dJeGVBqb

Big thanks!

Sounds simple…just need to get my head around where I put the trigger. Bit of a learning curve from my end.

Does the trigger gets actioned once?
IE The internet service (in this instance it’s a satellite modem) can take 10 minutes before the state changes from “down” to “up”

Thanks again,

Rob.

You should put it in an Automation.

Yes the trigger will only get actioned once, when passing from UP to DOWN.
So even if your connection take 10 minutes to come back UP, it’s not a problem the trigger will only be fired by the next change of start from UP to DOWN.

To give you an example I use it to detect when I come back home, it turn a light home when my state pass from ‘away’ to ‘home’. If I then turn off the light, it will stay off until I leave and come back again (the trigger won’t be trigger several time).

Thanks Touliloup!

Do you mind if I can keep bouncing some questions on this?
Yes it’s working fine. I’ve setup another trigger when switch is “off” to turn the switch “on”.
Works like a charm but…
Issue now is the fact that the trigger works on the state change from "up to “down”.
trigger will only ever work again if the unit goes back in the “up” state.
Whilst in the “down” state, need a trigger that will forever keep trying to power cycle the modem.
Can you suggest a way that I could keep power cycling the modem every hour?

Some background on my project, the modem is a satellite modem installed in a remote unmanned farm located typically 100kms away from anyone and monitoring the water levels of a cattle water trough. If I can’t detect the state of the waterlevels, livestock could be at risk and need to drive out and check the water bore pump is working.
Hence need to keep trying to recover the modem.

Here’s a screen shot to give you an idea of the setup.

Thanks again.

Rob.

You should make another automation that:
-is triggered every hour
-with the condition state = ‘DOWN’

See here for the time trigger:

Thanks again,

That makes sense.

Rob.

Hi Again,

Intention of code below, should trigger every 60 minutes and switch off a device.
Log file reports the following error:
"homeassistant.bootstrap: Invalid config for [automation]: [platform] is an invalid option for [automation]. Check: automation->condition->0->platform. (See /home/homeassistant/.homeassistant/configuration.yaml:42)’
And for the life of me can’t see what’s wrong…any suggestions appreciated.

- alias: 'offline2'  # wait a period of time of failed pings before switching off the modem, this is diffrent to the state change
  hide_entity: True
  trigger:
     - platform: time     
       minutes: 60
  condition:
    - platform: state
      entity_id: sensor.SATELLITE
      state: 'DOWN'
  action:
    service: homeassistant.turn_off
    entity_id: switch.skymuster

According to the State condition documentation, your condition should be declare like that:

condition:
  condition: state
  entity_id: sensor.SATELLITE
  state: 'DOWN'

Also, I’m not sure your time trigger will work. This will trigger the automation every time the minutes of the current time == 60. Which never happen since we pass from 13h59m59s to 14h00m00s for example. The time is never 13h60m00s.
To achieve a trigger every 60 minutes, you can set it as follow:

trigger:
  - platform: time     
    minutes: 1

And it will be trigger every hour, 1 minute past whole (at 11h01, 12h01, 13h01, etc…).

See the Time trigger documentation for more example.


And as a last point, please check the entity_id of your sensor, SATELLITE doesn’t appear as being a valid value to me. Entity ID are always lower case -> satellite

You can make sure of that by looking into the states view in home-assistant. You’ll have a list of all entities and can check whether it is lower or upper case.

Thanks touliloup!

That worked, but believe not achieving the desired effect.
This trigger will trigger at 1 minute past the hour regardless of how long the service has been in the down state.
Need a trigger that after 60 minutes of being in the down state, to then act on this.

Rob.

To achieve that (only restart after 60 minutes of down), you should add a for to the state condition:

condition:
  condition: state
  entity_id: sensor.SATELLITE
  state: 'DOWN'
  for:
    hours: 1

Note that you might have to wait up to 1h59 until it restart.
If internet goes down at 11h02, next trigger is at 12h01 but will be ignored because internet will have been down for only 59 minutes. And only at 13h01 your modem will restart!

I don’t know if that’s a problem in your case.

Thanks again!

Would be nice if you could set the time trigger to work on the time selected. Not a major issue,your suggestion above will work fine.
BTW. The fact that this routine is working, using capitals seems fine, will change to lower case to conform.
It currently reports via state view as follows:
sensor.satellite DOWN friendly_name: SATELLITE

Have a great day!

Rob.

Might be case insensitive then.

Thanks have a nice day too, I’ll have a great night now :wink:

Rob, would you be able to paste your sensor.satelite scripts that returns the up or down state to HA?

Thanks

OK I think I figured out how to detect when I’m off the Internet. I’ll document it here in case it can help others.

binary_sensor:
  - platform: command_line
    command: 'ping -c 1 8.8.8.8 | grep "1 received" | wc -l'
    name: 'is_charter_online'
    sensor_class: connectivity
    payload_on: 1
    payload_off: 0
3 Likes

Thanks Rob,

Nice, I’ll test this out.
Used this method which also works:

Good point with the above method, for testing purposes you can change the Ip to an address on the fly in the .sh script to an address which fails.

I’ve also since moved on a bit to using appdaemon, as the trigger/action monitoring method. The advantage is the countdown timer starts when the ping fails not at the start of the hour.
Other issue I’ve found with the above method using HA, on startup of HA if the state is already down, doesn’t trigger as no state change from UP to DOWN occurred.
It’s got a slight bug which I’m trying to resolve, but once resolved will share.

Rob.

1 Like

I know I am late (5 years) to this party, but it was a top Google hit for detecting internet down. I wanted to know when the Unifi system rebooted the Cable Modem through the Unifi Modem Plug. I had no luck with the binary_sensor.command_line above. The grep command seemed off and also complained about the connectivity line. What worked for me. Also, did not need scan_interval specified because modem reboot takes about 90 seconds.

binary_sensor:
  - platform: command_line
    command: 'ping -c 1 8.8.8.8 | grep "1 packets received" | wc -l'
    name: 'is_charter_online'
    payload_on: 1
    payload_off: 0
1 Like

Seems to me that all the examples over complicate things.

Use a ping sensor.

Here’s my “router booter”:

configuration.yaml:

binary_sensor:
########INTERNET CONNECTION PING SENSORS
  - platform: ping
    host: xxx.xxx.xxx.xxx
    name: "ISP DNS Ping"
    scan_interval: 60
  - platform: ping
    host: xxx.xxx.xxx.xxx
    name: "ISP Default Gateway Ping"
    scan_interval: 60

You can ping your ISP DNS, or your ISP gateway. Gateway has the advantage is that it should always be up if your Internet connection is up.

It will be the first hop past your router if you tracert to an external IP address (ie. 8.8.8.8). So if you tracert 8.8.8.8, you’ll get:

Tracing route to dns.google [8.8.8.8]
over a maximum of 30 hops:

1 3 ms 1 ms 1 ms your local router (ie. 192.168.x.x)
2 5 ms 5 ms 6 ms your ISP router (THIS IS THE IP YOU WANT TO PING)
3 17 ms 16 ms 16 ms next hops…

Then your automation is something like:

###########REBOOT ROUTER IF NO PING
- id: '9568133497643846847'
  alias: Reboot Router If No Ping To Gateway
  trigger:
  - entity_id: binary_sensor.isp_default_gateway_ping
    platform: state
    to: 'off'
    for:
      minutes: 10
  action:
  - data:
      topic: 'cmnd/routerbooter/POWER'
      payload: "OFF"
    service: mqtt.publish
  - delay: '00:00:30'
  - data:
      topic: 'cmnd/routerbooter/POWER'
      payload: "ON"
    service: mqtt.publish
  - delay: '00:05:00'
  - data:
      message: 'Internet was down for 10 minutes. Rebooted router 5 minutes ago.'
      title: 'HOUSE: Router Reboot'
    service: notify.aaron_email

I switch the router power by directly publishing MQTT messages (for reasons) but you can perform any action.

2 Likes