Check for being disconnected from MQTT and trigger action if so for more than 1min

Hello,

I’d like to trigger a sound via a piezo/buzzer, if my esphome running esp32 is disconnected for more than 1min from MQTT for whatever reason.

I’m aware that an MQTT disconnect also normally triggers a reboot after some time, which I have disabled by setting reboot_timeout: 0s.

I’ve seen the on_disconnect event for MQTT (MQTT Client Component — ESPHome) however I’m unsure about how to trigger an action after $time of that condition holding true.

Also esphome seems to go into some blocking state not reliably executing code when checking for it according to ESPHome Device as MQTT subscriber, detect MQTT loss without reboot - #7 by Linwood

Any help/hints/pointers appreciated - thanks!

You can do it within mqtt with conditions or using a script like this:

....
  on_connect:
    - script.stop: buzzer_script
  on_disconnect:
    - script.execute: buzzer_script

script:
  - id: buzzer_script
    then:
      - delay: 60s
      - switch.turn_on: buzzer_switch

That works like a charm, thanks a lot!

You’re welcome!