TL;DR: I’ve built the following “package” (Packages - Home Assistant) that detects if my Internet goes down and then reboots my modem until service is restored.
Rather than relying on a single Internet endpoint to determine if my connection is down, I’ve created 3x ping-based binary-sensors. If any one of them is unreachable for 1m AND the other 2 are also unreachable, my “reboot_modem” script is called. My modem is plugged into a z-wave plug.
The script turns off the plug, waits 30s, turns the plug back on and waits 2m. If the selected ping-based binary-sensor is still not connected, it repeats these events.
Question: My automation has all 3 ping-based binary-sensors as triggers. In my mind, this means that if the Internet does go down, this automation would be called 3x and the script could also be running 3 versions? What is the best way to put a “lock” on the script to ensure only a single instance of it runs? I did find a custom component that creates a variable (GitHub - snarky-snark/home-assistant-variables: A custom Home Assistant component for declaring and setting generic variable entities dynamically.). I would set/unset this at the start and end of the script. Would appreciate any other thoughts on this.
binary_sensor:
- platform: ping
name: Google
host: 8.8.8.8
scan_interval: 5
- platform: ping
name: Cloudflare
host: 1.1.1.1
scan_interval: 5
- platform: ping
name: Quad9
host: 9.9.9.9
scan_interval: 5
automation:
- alias: internet down
description: "internet down call script to reboot modem"
mode: single
trigger:
- platform: state
entity_id:
- binary_sensor.google
- binary_sensor.cloudflare
- binary_sensor.quad9
from: "on"
to: "off"
for:
hours: 0
minutes: 1
seconds: 0
condition:
- condition: and
conditions:
- condition: state
entity_id: binary_sensor.google
state: "off"
- condition: state
entity_id: binary_sensor.cloudflare
state: "off"
- condition: state
entity_id: binary_sensor.quad9
state: "off"
action:
- service: script.reboot_modem
data: {}
script:
reboot_modem:
alias: reboot modem every 2m until connected
sequence:
- service: switch.turn_off
data: {}
target:
entity_id: switch.modem_plug
- delay:
hours: 0
minutes: 0
seconds: 30
milliseconds: 0
- service: switch.turn_on
data: {}
target:
entity_id: switch.modem_plug
- delay:
hours: 0
minutes: 2
seconds: 0
milliseconds: 0
- repeat:
until:
- condition: state
entity_id: binary_sensor.cloudflare
state: "on"
sequence: []
mode: single