|
# ESPHome package for watchdog monitoring of remote device's IP connection. |
|
# Required: |
|
# relay, should be defined by main or other yaml. |
|
# target_ip_address: 192.168.20.121 # IP address to monitor. |
|
# Optional: |
|
# ping_loss_threshold: 3 # Number of minutes without pings to trigger relay. |
|
# Typically 30 for real and 3 for testing. |
|
# ping_hold_off: "5" # number of minutes after trigger, before pings are |
|
# monitored again. |
|
# api_loss_threshold: "0min" # The default is 15 min, 0 is disabled. |
|
# # Should be disabled when not used with HA. |
|
# |
|
# example use: |
|
# esphome_name: homeassistant-watchdog |
|
# friendly_name: HomeAssistant Watchdog |
|
# packages: |
|
# board: !include .sonoff-s31.yaml |
|
# network: !include .network.yaml |
|
# watchdog: !include .ping-watchdog.yaml |
|
# |
|
# recommended hardware: |
|
# https://www.amazon.com/Sonoff-S31-SONOFF-Plug-White/dp/B08TNF4835 |
|
|
|
substitutions: |
|
# IP address to monitor. |
|
target_ip_address: "127.0.0.1" |
|
# Number of minutes without pings to trigger relay. |
|
# Note - Typically 30 for real and 3 for testing. |
|
ping_loss_threshold: "3" |
|
# Number of minutes after trigger, before pings are monitored again. |
|
ping_hold_off: "5" |
|
api_loss_threshold: "0min" # The default is 15 min, 0 is disabled. |
|
off_delay: "5s" # Off time of relay, Select a time as to avoid "brown-out". |
|
# Note this is time for all hardware. Rpi and SSD, etc... |
|
|
|
# Caution - needs to be longer than startup of HA. |
|
# Other reboots default to 15, e.i WiFi. So lets give them a chance first. |
|
|
|
# Dependancies |
|
esphome: |
|
libraries: |
|
- ESP8266WiFi # Load a none standardard Platform IO library, used by below. |
|
- https://github.com/akaJes/AsyncPing # Load an externally sourced library for new ping component |
|
|
|
external_components: |
|
# The ping library should be placed into the following directory. |
|
# See README.md at https://github.com/trombik/esphome-component-ping |
|
- source: # include locally custom component. |
|
type: local |
|
path: ./components |
|
|
|
# Enable Home Assistant API. |
|
api: |
|
# When ping target is HA then api.reboot_timeout should be extended then |
|
# ping_loss_threshold. |
|
reboot_timeout: ${api_loss_threshold} # 15min is default |
|
|
|
# Variable to hold counter of successive failed pings, |
|
# it is updated after every ping update period. |
|
# When negative value is a holdoff. |
|
globals: |
|
- id: ping_loss_counter |
|
type: signed int |
|
restore_value: false |
|
initial_value: "0" |
|
|
|
switch: |
|
# This is very important, as the default is ALWAYS_OFF |
|
- id: !extend relay |
|
restore_mode: ALWAYS_ON |
|
|
|
binary_sensor: |
|
# Remove prior button actions as dict. |
|
- id: !extend onboard_button |
|
on_press: {} |
|
# Then create a new action guarenteed to turn back on. |
|
- id: !extend onboard_button |
|
on_press: |
|
- switch.turn_off: relay |
|
- delay: ${off_delay} |
|
- switch.turn_on: relay |
|
|
|
wifi: |
|
# Watchdog of ESP device itself for missing Wi-Fi. |
|
# Caution - needs to be longer than startup of network equipment |
|
reboot_timeout: 15min # 15min is default |
|
|
|
sensor: |
|
# Uptime component is helpful for monitoring watchdog timeouts events. |
|
# This can be compared to HA's uptime, for problem solving. |
|
- platform: uptime |
|
name: Uptime |
|
id: esp_uptime |
|
|
|
# Custom ESPHome Component PING |
|
# https://github.com/trombik/esphome-component-ping |
|
- platform: ping |
|
ip_address: ${target_ip_address} |
|
num_attempts: 2 |
|
timeout: 2sec |
|
update_interval: 60s # Typically 60s for real and 10s for testing. |
|
loss: |
|
name: Packet loss |
|
id: loss |
|
on_value: |
|
then: |
|
- logger.log: |
|
format: PING percent_packet_loss=%0.1f, ping_loss_counter=%d |
|
args: [id(loss).state, id(ping_loss_counter)] |
|
- if: |
|
condition: |
|
# when there are some PINGs and not in hold off |
|
lambda: !lambda |- |
|
return (id(loss).state < 100) && id(ping_loss_counter) > -1; |
|
then: |
|
# When Pings are good, reset the counter |
|
- logger.log: |
|
format: There are pings, clearing ping_loss_counter. |
|
- globals.set: |
|
id: ping_loss_counter |
|
value: "0" |
|
else: |
|
- if: |
|
condition: |
|
# increament counter and check ping counter vs threshold |
|
lambda: !lambda |- |
|
if (id(ping_loss_counter) < ${ping_loss_threshold}) { |
|
// only increament when below max limit, else it may eventually roll |
|
id(ping_loss_counter)++; |
|
} |
|
ESP_LOGD("main", "ping_loss_counter= %d", id(ping_loss_counter)); |
|
return (id(ping_loss_counter) > (${ping_loss_threshold} - 1)); |
|
then: |
|
# When there have been no pings, for to long. |
|
- logger.log: |
|
format: No pings for %d seconds. Too long, resetting relay |
|
args: |
|
- !lambda |- |
|
id(ping_loss_counter) |
|
- switch.turn_off: |
|
id: relay |
|
- switch.turn_on: |
|
id: relay |
|
- globals.set: |
|
id: ping_loss_counter |
|
value: -${ping_hold_off} |
|
latency: # not needed for this use case. |
|
name: Latency |
|
accuracy_decimals: 3 |
|
id: latency |
|
on_value: |
|
then: |
|
- logger.log: |
|
format: PING average_response_ms=%0.1f |
|
args: id(latency).state * 1000 |
Added "off_relay" delay to control proper off time as a managed variable.