By   November 9, 2019

The Xplornet modem out at DR has decided to become unreliable and needs a sporadic restart. I’ve been pleased with how well the TP-Link Kasa HS105 Wifi switches perform so I just needed to figure out how to get HomeAssistant to reliably monitor the internet connection and reset it without inducing an infinite power-cycle loop. This is what I ended up with. Seems to work ok.

 

In configuration.yaml:

device_tracker:
platform: ping
hosts:
google: 8.8.8.8
beer: 67.205.176.146

In automations.yaml:

- id: Ensure Internet is on
alias: Ensure Internet is on
initial_state: on
trigger:
platform: state
entity_id: switch.internet
to: 'off'
for:
minutes: 1
action:
service: homeassistant.turn_on
entity_id: switch.internet

id: Ensure Internet is on at HA startup
alias: Ensure Internet is on at HA startup
initial_state: on
trigger:
platform: homeassistant
event: start
action:
service: homeassistant.turn_on
entity_id: switch.internet

id: Restart internet connection
alias: Restart internet connection
initial_state: on
trigger:
- platform: state
entity_id: device_tracker.google
to: 'not_home'
- platform: state
entity_id: device_tracker.beer
to: 'not_home'
condition:
condition: and
conditions:
- condition: state
entity_id: device_tracker.google
state: 'not_home'
- condition: state
entity_id: device_tracker.beer
state: 'not_home'
action:
service: homeassistant.turn_off
entity_id: switch.internet