MQTT Event Trigger on Home Assistant Startup but device is OFF

I have OctoPi on all my 3D printers, and they are running MQTT.
I have OctoPi set to send MQTT event messages either if it disconnects from the printer or there is an error.
In Home Assistant, I have this set up in /config/automations.yaml

MQTT

Which tells the Sonoff switch to turn the power off, thus shutting down the printer.

However, when Home Assistant Boots up, I’m getting these events in the LogBook, even when the printer and OctoPi is OFF:

And the BAD part is, if that printer actually is ON and printing, this ‘fake’ event happen anyway when Home Assistant restarts and it shuts down the power to the printer in the middle of a print job!

Any ideas how to fix this and/or why its happening?

Thanks!

P.S. I think this may be happening because the last trigger was a ‘Disconnect’ topic from the printer. So maybe MQTT is still holding that topic and resending it at startup? If so, how do I clear that out after my ‘action:’ ?

Since this happens during HA reboot you can try to put a condition to check if the HA has rebooted very recently. This can be done by first enabling the uptime sensor and the iso time tensor and using a template condition like below.

{{ as_timestamp(states.sensor.date_time_iso.state) - as_timestamp(as_timestamp(states.sensor.uptime.state) | timestamp_local) >120 }}

This condition will check if the HA had booted in the past 2 minutes. You can change this by changing 120 in the template which is the seconds.

That’s a hack that I will try for now, but it creates a window of time where if there was an error from the printer, and it needed to be shut down (otherwise FIRE), it wouldn’t happen and there would be cinders all around…