I’m using Home Assistant to control the heating in our house. The computer running HA has crashed a couple of times, resulting in an awkward morning because the inside temperature is around 16°C instead of the regular 21°C. To prevent this, I have wanted for some time to create a watchdog device for my HA computer, automatically flicking the power switch if HA becomes unresponsive.
Getting inspiration from this posting, I created my watchdog device using an inexpensive Sonoff S20 smart plug flashed with Tasmota firmware. Getting Tasmota flashed on the device is a bit involved, requiring soldering header pins on the smart plug PCB and then flashing the firmware using an external flasher device. None of the supporting hardware cost more than a few euro and the process is not difficult, but some determination is still required. The Tasmota Sonff S20 wiki page provides decent instructions for the process.
Tasmota is controlled over MQTT. The neat trick in this setup is that you can set the device to run a sequence of commands. So, the smart plug is set to wait for two minutes and then turn the power off for a few seconds. A HA automation running every minute clears the previous command and sets a new sequence again two minutes from now. All vital components are tested: as long as the computer, Home Assistant and the MQTT service are running fine, the watchdog is never triggering, but if any of these are down, the computer reboots. During the reboot the watchdog is inactive because the smart plug command backlog is empty. Also, if HA is shut down in a controlled fashion, another automation clears the watchdog backlog to prevent accidental reboots while tweaking the setup.
Here are my automations:
- alias: Watchdog: Ping
trigger:
platform: time
minutes: '/1'
seconds: 00
condition: []
action:
- service: mqtt.publish
data:
topic: cmnd/hal_watchdog/Backlog
payload: ""
- service: mqtt.publish
data:
topic: cmnd/hal_watchdog/Backlog
payload: "delay 1200; Power Off; delay 50; Power On"
- alias: Watchdog: Disable when exiting
trigger:
platform: homeassistant
event: shutdown
condition: []
action:
- service: mqtt.publish
data:
topic: cmnd/hal_watchdog/Backlog
payload: ""