Check if ZigBee2MQTT is working

Hi,

thanks to the advices I got here, I now almost finished the configuration of my first ESPHome switch.

The configuration is based on this article. The idea is following:

I have Zigbee lights, which I want to control with the switch. If everything works correctly, the switch is on and the lights are powered all the time. The switch only tells HA to turn the lights on and off using Zigbee2MQTT. If for some reason, HA is not working, then the switch will work in a normal “dumb” mode. This will allow me to turn on and off the lights, even if HA is down.

In the script, this is done by this condition:

script:
  - id: hass_light_toggle
    then:
      if:
        condition:
          api.connected:
        then:
          ...
        else:
          # When HA is unavailable, toggle the relay.
          - switch.toggle: relay

But in my setup for some reson quite often happens, that HA is running, but Zigbee2MQTTT is down, devices don’t communicate and I have to restart HA completely to make it work again.

The question is, if and how can I detect, that the lights I control are connected and working correctly. Anybody has an idea?

Z2M has a running entity which you can use to determine if it’s running or not:

Note that it might be disabled by default and you will probably have to enable it manually. I doubt you can get this entity’s state directly in your ESPHome code though - you might have to use an automation in HA as a workaround.

The above doesn’t solve the actual problem though. Z2M should not be going down for any period of time. I would start by reading this first and fixing the Z2M issue instead of trying to work around it.