Skip to content

Instantly share code, notes, and snippets.

@EpicLPer
Last active January 5, 2024 17:38
Show Gist options
  • Save EpicLPer/84172ee9ee5ce47e70de52ad28c4ff13 to your computer and use it in GitHub Desktop.
Save EpicLPer/84172ee9ee5ce47e70de52ad28c4ff13 to your computer and use it in GitHub Desktop.
Restart Zigbee2MQTT Home Assistant Add-On upon Adapter Crash

I'm using both Home Assistant and Zigbee2MQTT as my main Smarthome drivers, however I'm having a bit of an issue with my Sonoff Zigbee-P Dongle where it tends to crash randomly, often at night, leaving people in the house without any lights responding.

I've created a small automation which automatically restarts the Z2M addon in order to reconnect to the stick. Hope it helps!

As a note: This is of course not a "fix". The real fix is to get better hardware/fix the crashing stick issue. This is more a workaround to get things rolling without a huge downtime!

# -----------------------------------------------------------
# --- You might have to set Zigbee2MQTT to "debug" output ---
# ---      in "Settings" > "Advanced" > "Log level"!      ---
# -----------------------------------------------------------

alias: AUTO - Zigbee2MQTT Restart upon adapter crash
description: Automatically restarts the Z2M addon when an adapter crash has been detected.
trigger:
  - platform: mqtt
    topic: zigbee2mqtt/bridge/logging
    payload: "on"
# --- These are the most common error messages upon the stick crashing/disconnecting.
    value_template: >
      {% if ('SRSP - AF - dataRequestExt after 6000ms' in value_json['message']) %}
        on
      {% elif ('SRSP - AF - dataRequest after 6000ms' in value_json['message']) %}
        on
      {% elif ('SRSP - ZDO - mgmtPermitJoinReq after 6000ms' in value_json['message']) %}
        on
      {% else %}
        off
      {% endif %}
    id: error-occured
    alias: When MQTT log output via Z2M throws error messages
condition: []
action:
  - alias: When ID "error-occured" is received
    if:
      - condition: trigger
        id:
          - error-occured
        alias: "Trigger: \"error-occured\""
    then:
# --- Add your own mobile app here if you want! Or, if you don't want to be notified simply just remove it in the GUI.
      - service: notify.mobile_app_XXXXX
        data:
          message: Restarting Z2M...
          title: ⚠️ Zigbee Adapter crashed! ⚠️
        alias: "Service Call: Mobile App Notification"
      - service: hassio.addon_restart
        data:
# --- You have to edit this (can also be done in th GUI) as the value in front will be different for you!
          addon: XXXXX_zigbee2mqtt
        alias: "Service Call: Restart Add-On (Zigbee2MQTT)"
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment