I’m trying to automate the power cycling of my cable modem and firewall router in the event that the Internet connection is lost for more than ten minutes. In the past, I’ve experienced outages after loss of power and when ISP has been re-configuring the modem. I’m using a Ping binary sensor to monitor the Internet connection by pinging Google DNS server (8.8.8.8) as described in Detect internet is down and reboot modem - #18 by AaronCake
# is the Internet up?
binary_sensor:
- platform: ping
host: 8.8.8.8
name: "dns.google"
count: 2
scan_interval: 300
Strategy: Whenever the connectivity sensor transitions from on to off or home-assistant is restarted, check to see if connectivity is restored within 10 minutes, else power cycle the cable modem and then the firewall router. Then send an appropriate notification.
The Automation:
alias: Lost Connection
description: Monitor Internet Connection
trigger:
- platform: state
entity_id:
- binary_sensor.dns_google
from: "on"
to: "off"
for:
hours: 0
minutes: 0
seconds: 10
- platform: homeassistant
event: start
condition:
- condition: state
entity_id: binary_sensor.dns_google
state: "off"
for:
hours: 0
minutes: 10
seconds: 0
action:
- service: system_log.write
data:
level: warning
message: Internet connection lost
logger: Ping
- type: turn_off
device_id: 46dffef3544f957b1ee80028faa412a3
entity_id: switch.cable_modem
domain: switch
- type: turn_off
device_id: 9fb210fc476fc7687e58187cae82ff14
entity_id: switch.edgerouterx
domain: switch
- delay:
hours: 0
minutes: 1
seconds: 0
milliseconds: 0
- type: turn_on
device_id: 46dffef3544f957b1ee80028faa412a3
entity_id: switch.cable_modem
domain: switch
- delay:
hours: 0
minutes: 3
seconds: 0
milliseconds: 0
- type: turn_on
device_id: 9fb210fc476fc7687e58187cae82ff14
entity_id: switch.edgerouterx
domain: switch
- delay:
hours: 0
minutes: 5
seconds: 0
milliseconds: 0
- service: notify.all_users
data:
message: >-
Internet connection was lost for ten minutes so cable modem and router
were restarted.
title: Cable Modem & Router Restarted
mode: single
The automation actions have been tested in Developer Tools and they properly reboot both cable modem and router, write to the logs, and send the notifications. The automation triggers have been tested by automation.trigger
in Developer Tools and by disconnecting the cable modem input. Both trigger the automation, but it fails in the condition section with error message “Stopped because a condition failed at …”. Does anyone have a clue what I’m doing wrong?
Trace logs:
{
"trace": {
"last_step": "condition/0/entity_id/0",
"run_id": "d09e10746223148f7c8ff306e640a46e",
"state": "stopped",
"script_execution": "failed_conditions",
"timestamp": {
"start": "2022-12-16T17:10:59.999902+00:00",
"finish": "2022-12-16T17:11:00.000797+00:00"
},
"domain": "automation",
"item_id": "1670963664418",
"trigger": "state of binary_sensor.dns_google",
"trace": {
"trigger/0": [
{
"path": "trigger/0",
"timestamp": "2022-12-16T17:11:00.000157+00:00",
"changed_variables": {
"this": {
"entity_id": "automation.lostconnection",
"state": "on",
"attributes": {
"last_triggered": "2022-12-16T02:29:45.073664+00:00",
"mode": "single",
"current": 0,
"id": "1670963664418",
"icon": "mdi:web",
"friendly_name": "Lost Connection"
},
"last_changed": "2022-12-16T16:34:51.560332+00:00",
"last_updated": "2022-12-16T16:34:51.560332+00:00",
"context": {
"id": "01GMDWKPH8R47NDQFP13KMX48F",
"parent_id": null,
"user_id": null
}
},
"trigger": {
"id": "0",
"idx": "0",
"alias": null,
"platform": "state",
"entity_id": "binary_sensor.dns_google",
"from_state": {
"entity_id": "binary_sensor.dns_google",
"state": "on",
"attributes": {
"round_trip_time_avg": 17.563,
"round_trip_time_max": 17.936,
"round_trip_time_mdev": "",
"round_trip_time_min": 17.19,
"device_class": "connectivity",
"friendly_name": "dns.google"
},
"last_changed": "2022-12-16T02:35:47.829121+00:00",
"last_updated": "2022-12-16T17:05:48.024784+00:00",
"context": {
"id": "01GMDYCBFRP806GDE6QEM6K8W5",
"parent_id": null,
"user_id": null
}
},
"to_state": {
"entity_id": "binary_sensor.dns_google",
"state": "off",
"attributes": {
"device_class": "connectivity",
"friendly_name": "dns.google"
},
"last_changed": "2022-12-16T17:10:49.994007+00:00",
"last_updated": "2022-12-16T17:10:49.994007+00:00",
"context": {
"id": "01GMDYNJCACFCCFNM1C28TY4N6",
"parent_id": null,
"user_id": null
}
},
"for": {
"__type": "<class 'datetime.timedelta'>",
"total_seconds": 10
},
"attribute": null,
"description": "state of binary_sensor.dns_google"
}
}
}
],
"condition/0": [
{
"path": "condition/0",
"timestamp": "2022-12-16T17:11:00.000343+00:00",
"result": {
"result": false
}
}
],
"condition/0/entity_id/0": [
{
"path": "condition/0/entity_id/0",
"timestamp": "2022-12-16T17:11:00.000495+00:00",
"result": {
"result": false,
"state": "off",
"duration": "2022-12-16T17:01:00.000599+00:00"
}
}
]
},
"config": {
"id": "1670963664418",
"alias": "Lost Connection",
"description": "Monitor Internet Connection",
"trigger": [
{
"platform": "state",
"entity_id": [
"binary_sensor.dns_google"
],
"from": "on",
"to": "off",
"for": {
"hours": 0,
"minutes": 0,
"seconds": 10
}
},
{
"platform": "homeassistant",
"event": "start"
}
],
"condition": [
{
"condition": "state",
"entity_id": "binary_sensor.dns_google",
"state": "off",
"for": {
"hours": 0,
"minutes": 10,
"seconds": 0
}
}
],
"action": [
{
"service": "system_log.write",
"data": {
"level": "warning",
"message": "Internet connection lost",
"logger": "Ping"
}
},
{
"type": "turn_off",
"device_id": "46dffef3544f957b1ee80028faa412a3",
"entity_id": "switch.cable_modem",
"domain": "switch"
},
{
"type": "turn_off",
"device_id": "9fb210fc476fc7687e58187cae82ff14",
"entity_id": "switch.edgerouterx",
"domain": "switch"
},
{
"delay": {
"hours": 0,
"minutes": 1,
"seconds": 0,
"milliseconds": 0
}
},
{
"type": "turn_on",
"device_id": "46dffef3544f957b1ee80028faa412a3",
"entity_id": "switch.cable_modem",
"domain": "switch"
},
{
"delay": {
"hours": 0,
"minutes": 3,
"seconds": 0,
"milliseconds": 0
}
},
{
"type": "turn_on",
"device_id": "9fb210fc476fc7687e58187cae82ff14",
"entity_id": "switch.edgerouterx",
"domain": "switch"
},
{
"delay": {
"hours": 0,
"minutes": 5,
"seconds": 0,
"milliseconds": 0
}
},
{
"service": "notify.all_users",
"data": {
"message": "Internet connection was lost for ten minutes so cable modem and router were restarted.",
"title": "Cable Modem & Router Restarted"
}
}
],
"mode": "single"
},
"blueprint_inputs": null,
"context": {
"id": "01GMDYNW4Z9GBQQ3F7VDHG5J9Y",
"parent_id": "01GMDYNJCACFCCFNM1C28TY4N6",
"user_id": null
}
},
"logbookEntries": []
}
Forgot to add that I’m running HAOS on a Raspberry Pi 4 behind a Ubiquiti EdgerouterX.
Home Assistant 2022.12.6
Supervisor 2022.11.2
Operating System 9.4
Frontend 20221213.0 - latest