How to restart zigbee2mqtt automatically if it returns 502?

I have this issue when z2m restart for whatever reason, maybe i accidentally left it on auto update, or restart HA (I use HA yellow, so not the fastest thing in the world, but even on really really fast server it did happen occasionally)

There is pretty much nothing in the log
Log when HA of failed start - it returns 502, writes to log, but does NOT actually work - it does not control the devices

debug 2023-02-07 08:02:07: Loaded state from file /config/zigbee2mqtt-ttyAMA1/state.json
info  2023-02-07 08:02:07: Logging to console and directory: '/config/zigbee2mqtt-ttyAMA1/log/2023-02-07.08-02-07' filename: log.txt
debug 2023-02-07 08:02:07: Removing old log directory '/config/zigbee2mqtt-ttyAMA1/log/2023-02-02.00-01-39'
info  2023-02-07 08:02:07: Starting Zigbee2MQTT version 1.30.1 (commit #unknown)
info  2023-02-07 08:02:07: Starting zigbee-herdsman (0.14.89)
debug 2023-02-07 08:02:07: Using zigbee-herdsman with settings: '{"adapter":{"concurrent":null,"delay":null,"disableLED":false},"backupPath":"/config/zigbee2mqtt-ttyAMA1/coordinator_backup.json","databaseBackupPath":"/config/zigbee2mqtt-ttyAMA1/database.db.backup","databasePath":"/config/zigbee2mqtt-ttyAMA1/database.db","network":{"channelList":[12],"extendedPanID":[220,220,220,220,220,220,220,219],"networkKey":"HIDDEN","panID":6754},"serialPort":{"adapter":"ezsp","path":"/dev/ttyAMA1"}}'
debug 2023-02-07 08:07:07: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:12:07: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:17:07: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:22:07: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:27:07: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:32:07: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:37:07: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:42:08: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:47:08: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:52:08: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 08:57:08: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json
debug 2023-02-07 09:02:08: Saving state to file /config/zigbee2mqtt-ttyAMA1/state.json

Log of Z2m actually starting for realsies

debug 2023-02-07 23:21:20: Loaded state from file /config/zigbee2mqtt-ttyAMA1/state.json
info  2023-02-07 23:21:20: Logging to console and directory: '/config/zigbee2mqtt-ttyAMA1/log/2023-02-07.23-21-20' filename: log.txt
debug 2023-02-07 23:21:20: Removing old log directory '/config/zigbee2mqtt-ttyAMA1/log/2023-02-02.13-07-00'
info  2023-02-07 23:21:20: Starting Zigbee2MQTT version 1.30.1 (commit #unknown)
info  2023-02-07 23:21:20: Starting zigbee-herdsman (0.14.89)
debug 2023-02-07 23:21:20: Using zigbee-herdsman with settings: '{"adapter":{"concurrent":null,"delay":null,"disableLED":false},"backupPath":"/config/zigbee2mqtt-ttyAMA1/coordinator_backup.json","databaseBackupPath":"/config/zigbee2mqtt-ttyAMA1/database.db.backup","databasePath":"/config/zigbee2mqtt-ttyAMA1/database.db","network":{"channelList":[12],"extendedPanID":[220,220,220,220,220,220,220,219],"networkKey":"HIDDEN","panID":6754},"serialPort":{"adapter":"ezsp","path":"/dev/ttyAMA1"}}'
info  2023-02-07 23:21:23: zigbee-herdsman started (resumed)
info  2023-02-07 23:21:23: Coordinator firmware version: '{"meta":{"maintrel":"3 ","majorrel":"6","minorrel":"10","product":8,"revision":"6.10.3.0 build 297"},"type":"EZSP v8"}'
debug 2023-02-07 23:21:23: Zigbee network parameters: {"channel":12,"extendedPanID":220,"panID":6754}
info  2023-02-07 23:21:23: Currently 34 devices are joined:
info  2023-02-07 23:21:23: ZM2 Tuya Lidl LivingRoom TV Ledstrip (0x588e81fffefbd686): HG06104A - Lidl Livarno Lux smart LED light strip 2.5m (Router)
info  2023-02-07 23:21:23: ZM2 Tuya Lidl Bedroom light (0x04cd15fffee03036): HG08008 - Lidl Livarno Home LED ceiling light (Router)
info  2023-02-07 23:21:23: ZM2 Tuya Office Socket main (0xa4c1382792954704): TS011F_plug_1 - TuYa Smart plug (with power monitoring) (Router)

I do have HA watchdog enabled, but since Z2M is kinda running, it does nothing
image

I’m facing a similar issue, with the small difference that I’ve never been able to get Z2M past this 502-error.

  • HAOS - 2022.2.3
  • Z2M - 1.30.1
  • hardware - Odroid N2+ with ext. cable and Sonoff Zigbee USB Dongle+ (P, not E); I face the same issue with a SkyConnect stick
  • DuckDNS + Let’s Encrypt as an ssl setup

Z2M logging ‘Connecting to MQTT server at…’ and ‘Using MQTT login’ and then ‘Saving file state’ every 5 mins or so, but I can’t access the UI for whatever reason - I get 502: Bad Gateway.
The add-on looks to be in a zombie mode - outputing to the log but not actually running.

If I have to take a guess I will say that you didn’t configured your adapter correctly in z2m config file.

just solved it by changing the mqtt config.
Was:
server: hostname:port
solution:
server: mqtt://hostname:port
aaaargh!

I think your issue is different, i don’t gen any mqtt related messages in log