Skip to content

Instantly share code, notes, and snippets.

@mpflaga
Last active December 30, 2023 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpflaga/122e71bd98a08bac979af1131cce5680 to your computer and use it in GitHub Desktop.
Save mpflaga/122e71bd98a08bac979af1131cce5680 to your computer and use it in GitHub Desktop.
ESPHome recipe using SonOff S31 as an external Home Assistant Watchdog.

This watchdog relies on the native API connection reboot_timeout, to do the work. On Boot the ESP the .api-watchdog.yaml toggles off the switch’s relay supplying power to the Home Assistant for the specified time, ensuring the relay will properly power cycle the device. Additionally, the package is setting up metering sensors for testing and monitoring purposes.

Notes:

  1. This project has multiple files arranged as packages for modular setup of hardware, network and features.

    • homeassistant-watchdog.yaml - Top level file, that combines the below.
      • .api-watchdog.yaml - Abstracting particulars for the watchdog.
      • .network.yaml - Abstracting network setup.
      • .sonoff-s31.yaml - Abstracting hardware setup.
  2. The . dot prefix hides the files from ESPHome's HomeAssistant integration. The same can be done with the main file. As to prevent it from getting updated by the "UPDATE ALL" command. Which would result in a premature power cycle for the Home Assistant. Given it likely would not be the last in the upgrade order.

  3. The off_delay should be specified to a sufficiently long enough time, to ensure all dependent devices power cycle and not just the Rpi. Such as any USB-SSD devices and any other USB devices. The default is set to 5s.

# ESPHome package for watchdog monitoring of remote device's API connection.
# Required: relay, should be defined by main or other yaml.
# Optional: api_loss_threshold, can be defined by main, else defaults to 16m.
#
# example use:
# esphome_name: homeassistant-watchdog
# friendly_name: HomeAssistant Watchdog
# packages:
# board: !include .sonoff-s31.yaml
# network: !include .network.yaml
# watchdog: !include .api-watchdog.yaml
#
# recommended hardware:
# https://www.amazon.com/Sonoff-S31-SONOFF-Plug-White/dp/B08TNF4835
substitutions:
# Number of minutes without HA connection to trigger relay.
# Caution - needs to be longer than startup of HA.
# Other reboots default to 15, e.i WiFi. So lets give them a chance first.
api_loss_threshold: "16min" # The default is 15 min
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...
esphome:
on_boot:
- then:
- switch.turn_off:
id: relay
- delay: ${off_delay}
- switch.turn_on:
id: relay
# Enable Home Assistant API.
api:
# Watchdog of ESP device itself for missing HomeAssistant's API connection.
# Caution - needs to be longer than startup of HA
reboot_timeout: ${api_loss_threshold} # 15min is default
on_client_connected:
- logger.log: API got a connection!
on_client_disconnected:
# Note - This is triggered on any disconnect, regardless of muliple API
# connections. Note the api.connected condition is not valid here.
# See below binary_sensor "status" shows when there is any API connections.
- logger.log: API lost a connection!
switch:
# The default is ALWAYS_OFF
# on_boot or button will toggle the specified time period.
- id: !extend relay
restore_mode: ALWAYS_OFF
binary_sensor:
# Remove any/all prior button actions as dict.
- id: !extend button
on_press: {}
# Then create a new action guarenteed to turn back on.
- id: !extend button
on_press:
- then:
- 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
# ESPHome Package for basic network setup of remote device
# example use:
# substitutions:
# esphome_name: living-room
# packages:
# board: !include .shellyplus1.yaml
# network: !include .network.yaml
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
domain: .lan # My network uses this domain, yours may vary.
ap:
ssid: ${esphome_name}
password: !secret wifi_password
captive_portal:
web_server:
port: 80
auth:
username: !secret web_server_username
password: !secret web_server_password
api:
encryption:
key: !secret api_encryption_key
ota:
password: !secret ota_password
sensor:
- platform: wifi_signal
name: "WiFi Signal"
# update_interval: 60s # default is 60s
binary_sensor:
- platform: status
name: "API/MQTT Connection"
id: apimqtt_connection
# ESPHome Package for hardware setup of Shelly Plus 1
# example use:
# substitutions:
# esphome_name: living-room
# friendly_name: Living Room
# packages:
# board: !include .sonoff-s31.yaml
# network: !include .network.yaml
substitutions:
model_name: "Sonoff S31"
esphome:
name: ${esphome_name}
friendly_name: ${friendly_name}
esp8266:
board: esp01_1m
logger:
baud_rate: 0 # (UART logging interferes with cse7766)
uart:
rx_pin: RX
baud_rate: 4800
switch:
- platform: gpio
name: "${model_name} Relay"
pin: GPIO12
id: relay
restore_mode: ALWAYS_OFF # The default is ALWAYS_OFF
binary_sensor:
- platform: gpio
pin:
number: GPIO0
mode: INPUT_PULLUP
inverted: True
name: "${model_name} Button"
id: button
on_press:
- switch.toggle: relay
sensor:
- platform: wifi_signal
name: "${model_name} WiFi Signal"
update_interval: 60s
- platform: cse7766
current:
name: "${model_name} Current"
accuracy_decimals: 1
voltage:
name: "${model_name} Voltage"
accuracy_decimals: 1
power:
name: "${model_name} Power"
accuracy_decimals: 1
id: my_power
- platform: total_daily_energy
name: "${model_name} Daily Energy"
power_id: my_power
time:
- platform: sntp
id: my_time
status_led:
pin:
number: GPIO13
inverted: True
substitutions:
esphome_name: homeassistant-watchdog
friendly_name: HomeAssistant Watchdog
api_loss_threshold: "17min" # The default is 15 min
packages:
board: !include .sonoff-s31.yaml
network: !include .network.yaml
wathdog: !include .api-watchdog.yaml
logger:
level: VERBOSE
#wifi:
# use_address: 192.168.20.?
@mpflaga
Copy link
Author

mpflaga commented Dec 30, 2023

Added "off_relay" delay to control proper off time as a managed variable. It is possible that reboot toggled power supply faster than supplies capacitors drain time.

@mpflaga
Copy link
Author

mpflaga commented Dec 30, 2023

Added to Readme

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment