Automatically Restart Z2M When it Crashes

I’ve been dealing with random Z2M crashes. I’m not sure what exactly to call it. Z2M is still accessible, but it stops controlling devices and they all appear offline. So I guess my stick is getting disconnected. I’ve been trying to fix this on and off for over a year and still haven’t gotten it. Its incredibly frustrating.

At this point, I’m willing to settle for a hacky approach. I’m looking for a method to automatically restart Z2M when the stick stops responding. Maybe a template that counts offline devices and when it reaches a certain number of them, it triggers a restart? I think this would work, but I don’t know how to setup this kind of template sensor.

I’d truly appreciate any help you guys have to offer. I dont know what to do at this point.

Thank you,

EDIT: I have Z2M running on a raspi 3 that is not the host of HA.

You will have to figure out what exactly is going on. It’s always easier to say than do.
But are you sure that this is not mqtt issue?

Z2M (running in docker) has never crashed for me.

Perhaps swap out the USB cable?

And is z2mqtt updated to the latest version? What model is your coordinator?

I’ve tried figuring out what the problem is. The part that makes it especially frustrating is how random it is. It happens once or twice per week. I can’t think of anything I did that made this start happening, but its been going on for over a year. I have two separate sticks (nortek zwave and zzh zigbee) plugged into the pi. Both are on 3 ft usb extension cables and the sticks are pretty far apart. I did not consider that there could be a problem with the cable, so I’ll swap that out and see if it helps.

I don’t think its an MQTT issue, but I suppose it could be. Whenever the problem happens, z2m is still running, but all of the devices have a flashing “Offline” in the availability column. I can restart z2m via MQTT and it responds by restarting. After restarting z2m, everything functions as it should until it stops again.

I’m using a zzh stick (CC2652R) with zstack3x0 coordinator firmware version 20230507. Z2m is version 1.33.2.

Just now, I checked dmesg on the pi to see if there were any undervoltage errors and found this: ch341-uart ttyUSB0: usb_serial_generic_read_bulk_callback - urb stopped: -32

Some googling makes me think it could be the cable as jchh suggested.

  • do not use usb 3.0 port
  • make sure to use a usb extension cable to avoid interference with usb port driver
  • maybe use a usb hub with external power?

To me this seems like a faulty coordinator either in hardware or firmware. Have you tried to upgrade or roll-back the firmware?

Try to shut down mqtt or just restart it and you will get the same result. I don’t say it is just look into it, preferably logs.

I created this thread because I was overly frustrated with this. This has been a real PITA and I was ready to just put a bandaid on it and move on. I wasn’t expecting all of the help to diagnose this, but I truly appreciate it.

I’m using a raspi 3 and I think that only has usb 2.0 ports. In any case, I switched the USB port that I was using.

I have updated the firmware, but have not tried to roll it back. I just swapped the usb extension cable that I was using. If I still have the problem, then I’ll try rolling back the firmware. There was a time when I did not have this issue, so maybe its firmware related (although I’m sure its still something specific to me since no one else is complaining about this).

If that all fails, then I have the options of a powered hub and a new stick.

I know what the OP is talking about. It’s not an MQTT issue but the coordinator apparently crashing (it’s not disconnecting on the USB level*). Restarting Z2M fixes the symptoms.

This has been reported on GitHub before, but debugging it has staled out. The prevailing theory is that it’s either HW or FW issue of the coordinator.

Edit: *Skipped over the part where there were lines about the device in the dmesg output. Still, it sound’s very much like the coordinator crashes that have been reported for some Sonoff sticks. If the dongle disconnected on the USB HW level, a simple restart of Z2M would not fix the issues.

So, I made it three days.
[ch341-uart ttyUSB2: usb_serial_generic_read_bulk_callback - urb stopped: -32

I guess I’ll roll back the firmware. See you soon…

So, I’ve tried rolling back the firmware, powered usb hub, different cables, new dongle (same type).

Back to the original question. Anyone know how I can automatically restart z2m? I would love to solve this problem, but its taking too much time. I’m fine with a band-aid.

- id: '1600584897191'
  alias: 'Zigbee Restart '
  description: Restart Zigbee
  - platform: state
    entity_id: sensor.zigbee_no_activity
    from: 'False'
    to: 'True'
  condition: []
  - service: mqtt.publish
      topic:  zigbee2mqtt/bridge/request/restart


  - platform: template
        friendly_name: "Zigbee Watchdog"
        value_template: "{{ state_attr('sensor.cc2531router_linkquality','last_seen') }}"


  - platform: template
        friendly_name: "Zigbee Sensor No Activity"
        value_template: >
          {% set x = states('sensor.time') %}
          {{ (now().timestamp() - states.sensor.zigbee_watchdog.last_changed.timestamp()) | int // 60 > 5 }}

Thank you for the reply. I have Z2M installed on a raspberry pi and the coordinator does not show up in home assistant as a device. However, I finally found the source of the original problem!

Solution: All I needed was a powered usb hub.

In my defense, I tried using a powered hub that I had laying around and it did not help. On a whim, I bought another powered hub and it seems to have fixed it. The coordinator hasn’t crashed in a couple of weeks.

1 Like