Watchdog device using Sonoff with Tasmota

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: ""
3 Likes

Nice, but wouldn’t it be better to get to the bottom of why your machine is crashing?

That argument would apply to every single watchdog implementation, right? And yet, there’s a built-in hardware watchdog on every Raspberry Pi and every ESP8266 device. If a device crashes randomly every 3 or 6 months, my life is too short to start debugging it.

1 Like

I agree, for that frequency of crashes life is far too short!!

1 Like

Thanks for this. I am running a pi3 and have also had random crashes at very infrequent times. Life I definitely too short.

I currently has a sonoff S20 with original firmware running the pi so I can use the ewelink app to power cycle the pi when I can’t access HA.

Using this will likely mean It would restart without me even knowing.

Is there any way to set remote access to the tasmota firmware to perform a power cycle remotely (I realize this can be done when on the same network using its IP address.)

Use a VPN or set up a reverse proxy to expose the Tasmota page to the Internet.

1 Like

VPN would sound a much safer option to me given the old wisdom “In IoT, the letter S stands for security”… :wink: It’s probably quite easy to perform e.g. a DoS attack on the poor little ESP8266 trying to serve the Tasmota UI. I wouldn’t want my home automation to go belly up because of that.

Did finally get this up and running.

However I seem to get more random restarts than I ever did crashes.

Is there any way to know if the restart was from a power cycle or from home assistant restarting its self.

I could add another sonoff inline or power an esphome sensor with the same supply then do something with the uptime sensor to see if home assistant restarted or the power cycled.

Only want to know where to start looking for the cause.

That’s weird. For me that setup has worked extremely reliably. It’s only when I tweak with something and quit Home Assistant when it tends to restart on me. :smiley:

Keep in mind that a wifi / network issue will also result in a detected crash. i.e. Home Assistant might be still working, but the message was never received by Tasmota.