RPI3 Watchdog using an ESPHome and a Tasmota Plug

My HA RPI3 system sometimes get stuck and as it is not easily accessible to do a power reset I plugged it into a Tasmota Plug lying around to do it remotely.
I then realized that using one of my already existing ESPHome Temperature sensors I could have implemented a rude watchdog logic.
In particular if the ESPHome is not connected to HA Server, it assumes it is due to HA no more responding and than it toggle the power to the RPI to restart it.
It is not rocket science and it has a loot of limitations but it is working well, it uses only components already existing in my network and it was super quick to be coded.
The current main limitation is that if the API connection is not accessible for any reason different from an RPI3 problem (e.g. HA is restarting, server is updating) the watchdog wrongly unpower the RPI3. Below the code added to the ESPHome:

#...
#...existing ESPHome code (in my case to read temperature and humidity via DHT22
time
  - platform: sntp
    on_time:
      # Every half an hour it check if the sensor is connected to HA server
      - seconds: 0
        minutes: /30
        then:
          - if:
                  condition:
                    api.connected  #the condition to met in order to avoid resetting HA power
                  then:
                    - logger.log: "HA Alive! Reset Skipped."
                  else:
                    - logger.log: "Start Reset!"
                    - http_request.post: http://192.168.1.xxx/cm?cmnd=Power%20TOGGLE #the TASMOTA plug is accessible at IP: 192.168.1.xxx
                    - delay: 5s
                    - http_request.post: http://192.168.1.xxx/cm?cmnd=Power%20TOGGLE #the TASMOTA plug is accessible at IP: 192.168.1.xxx
                    - logger.log: "Reset done."

One of my needs was to avoid modifying the Tasmota plug to keep it unmodified, in principle I could have implemented the watchdog in the plug itself.

Also found this Watchdog device using Sonoff with Tasmota having a similar concept behind but modfying the tasmota itself. I’ll review to improve my own.

You might have a read of these posts on software watchdog for raspberry pi. A couple caveats, it is a software solution which not as good ‘petting a hardware dog’ and I think the watchdog tools and name have changed between linux and raspberry pi version, so be on lookout for current version/solution. I am using this on one rpi that seemed to drop off wifi too often, and it seems to do a pretty good job of rebooting this machine:

https://linux.die.net/man/5/watchdog.conf
RUNNING FOREVER WITH THE RASPBERRY PI HARDWARE WATCHDOG
https://diode.io/raspberry%20pi/running-forever-with-the-raspberry-pi-hardware-watchdog-20202/

not clear, based what version of pi and os is needed to setup
but I did the watchdog install above
https://raspberrypi.stackexchange.com/questions/108080/watchdog-on-the-rpi4

WatchDog for Raspberry Pi
24. September 2016 von Hödlmoser
https://blog.kmp.or.at/watchdog-for-raspberry-pi/

Enabling Watchdog on Raspberry Pi
Arslan Zahid
Dec 30, 2019 · 2 min read
https://medium.com/@arslion/enabling-watchdog-on-raspberry-pi-b7e574dcba6b

Thanks a lot. I’ll study it and I’ll compare the solutions.
On the other side I’m slowly investigating the root causes of my RPI3 freeze, but actually without big success. I think it is purely because I’m asking too much to 1Gb of RAM without having time to optimize the system.

I ran Home Assistant, MQTT, Zigbee2MQTT, NPM connection to Smarthings and some custom pythons stuff in Docker on a Raspberry Pi 3 1 GB for over a year. I was very pleased with it’s reliability, but it ‘hit the wall’ sometimes. I have since moved my stack to a beefer intel Ubuntu system, but raspberry pi’s continue to be a great tool in my quiver. Just need to ‘know it’s limits’ have have tools like these watchdogs.
Good hunting! and Happy New Year!