Esphome as watchdog

Hi, I just had the problem that my home assistant rpi4 hang after starting into holiday. I heard about watchdog and heartbeat.

So I think about creating a heartbeat over mqtt (running) and use esphome to check if system is still up. If not I want to turn off/on a Shelly plug.

So is it possible to use esphome to check for the heartbeat and if not getting the ping for x/minutes turn the Shelly plug off and after x/seconds on again so the system can restart.

I heard that home assistant os or rpi4 should have watchdog but because my system crashed (not sure if ha or rpi4 crashed) and not came back alone I plan a hardware switch to cut the power for a restart.

So is that possible or how would you do it or maybe solved it?

No idea or tips? Or is it just not possible with esphome?

So, you want to ping the RPI4 periodically to see if it is dead?
ESPHome devices can’t ping.
I would use an ESP (Wemos D1 Mini) running an Arduino sketch to ping the RPI every few seconds then pulse a relay off\on if it fails two or three pings.

1 Like

No, I will send a mqtt message every x/minutes from ha. And if this message does not come in x/minutes the esphome should turn off/on a Shelly plug s.

Use the api.connected() condition instead?

The danger with your scheme, of course, is that if you restart HA your watchdog might cut power as it is rebooting…

You are depending on HA to tell the ESP that HA is running. In my scheme, the ESP is completely independent of HA.

The mqtt message is like a heartbeat to show it is still alive.

How did you solve it? Do you use any kind of watchdog to check if your system is still running?

Use a Sonoff R2 or another plug that you can flash with ESPHome. Use the mqtt.connected condition and you’re good to go. Not an ESP and a plug, just flash the plug with ESPHome.

If your Pi isn’t running, the Mosquitto won’t be running as well. And there you go. :slight_smile:

EDIT. you don’t even need to check explicitly, ESPHome starts your ESPHome-flashed-plug automatically, if it can’t get a connection for 15 minutes (configurable). So the only thing you need is a plug, that you can flash with ESPHome. The rest is almost automatical. :slight_smile:

Do Automations Work Without a Network Connection

YES! All automations you define in ESPHome are executed on the ESP itself and will continue to work even if the WiFi network is down or the MQTT server is not reachable.

There is one caveat though: ESPHome automatically reboots if no connection to the MQTT broker can be made. This is because the ESPs typically have issues in their network stacks that require a reboot to fix. You can adjust this behavior (or even disable automatic rebooting) using the reboot_timeout option in the wifi component and mqtt component. (Beware that effectively disables the reboot watchdog, so you will need to power cycle the device if it fails to connect to the network without a reboot)

1 Like

Thank you exact what I was looking for. Now I need to find a plug that can be flashed for esphome and is available in Germany. :upside_down_face:

This is a very serious thing you need to consider. If you’re running a long update, e.g. because the database needs to be updated, you are pulling the plug from under it potentially destroying your setup. Same if MQTT is down but HA is not, or if you have a valid reason to stop services for some reason.

If you have frequent unexpected downtime you should investigate the cause, not make it worse by adding data corruption into the mix every time it happens. PTP should be a last resort when a graceful shutdown is no longer an option.

1 Like


Oder als fertige Dose:

Zu beachten: wenn Du zum flashen nicht löten willst, dann muss es bei der Dose die S20 sein, und nicht die S26. Bei den anderen Sonoffs würde ich auf jeden Fall den R2 und nicht den R3 nehmen. :slight_smile:

Meine persönliche Meinung: da das eh in der “Computer-Ecke” statt findet, würde ich den R2 nehmen, da kann man gleich mit entsprechenden Kabeln arbeiten, und muss nicht extra Dosen setzen. :slight_smile:

English translation:
The first two links are simply the two devices I’d recommend.

What I’d recommend is to use the S20, because you don’t need to solder (compared to the S26) and the Basic R2 against the R3. I’d use a R2, because you can put it in your setup without using additional power outlets. :slight_smile:

I’m an idiot, the mentioned Shelly Plug S is already flashable with ESPHome. :slight_smile:

1 Like

Danke :star_struck: super werde ich mir nach dem Urlaub mal ansehen.

That is a thing I definitely should have in mind. Thanks

Disaster in the making, would love to hear when/why the first reboot was triggered.