[Feature request]: Retry the initial MQTT connection instead of exiting when the broker isn't up yet
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 42/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
- Ambito
- networking
Direzione di ricerca
The startup path is in lib/mqtt.ts: connect() makes a single connectAsync() call, and a failure goes straight to the 'MQTT failed to connect, exiting' exit path. Read how mqtt.js reconnects after the first successful connect, then decide with a maintainer whether to retry indefinitely or use a startup grace period. Done means connection-level errors (ECONNREFUSED, EHOSTUNREACH, ETIMEDOUT, ENOTFOUND) retry at a capped interval with a log line per attempt, while auth and TLS errors still exit.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Is your feature request related to a problem? Please describe
If the MQTT broker isn't reachable when Z2M starts, Z2M logs MQTT failed to connect, exiting... (connect ECONNREFUSED <broker>:1883) and exits. Once Z2M has connected, a broker outage is handled fine, because mqtt.js reconnects indefinitely. The only fatal case is the broker being late at startup, which is the normal case after a power cut or host reboot whenever the broker runs on another machine or in another container.
#4408 asked for this in 2020 and was closed because a Docker restart policy covered it. On Home Assistant OS that answer doesn't hold. The add-on's restart policy is the Supervisor watchdog, which allows 10 restarts per 30 minutes and then stops for good (home-assistant/supervisor#7190). Z2M fails in about 4 s, so all 10 are spent in under a minute.
This happened to me on 2026-11-13 (Z2M 2.14.3-1, HAOS 18.4, Supervisor 2026.11.0, Mosquitto 2.1.2 in Docker on a second host). Both hosts rebooted together. Z2M made its 10 attempts between 19:13:54 and 19:14:43, and the broker opened port 1883 at 19:17:29. Zigbee stayed down for 2 hours until I started the add-on by hand. The coordinator was healthy throughout and joined fine on every attempt; only MQTT failed.
A second effect made it much harder to spot. The bridge/state online message is retained, and offline is only the last will, which a client that never connects doesn't have. The broker replayed the online from before the reboot, so Home Assistant's bridge connection sensor read connected for the whole outage, while every device was unknown.
Describe the solution you'd like
Retry the initial MQTT connection on connection-level errors (ECONNREFUSED, EHOSTUNREACH, ETIMEDOUT, ENOTFOUND) at a capped interval, the same way mqtt.js already retries after the first connect, logging each attempt. Authentication and TLS errors could still exit as they do now. In lib/mqtt.ts, connect() currently makes one connectAsync() call, so a failure goes straight to the exit path.
If indefinite retry isn't wanted, a configurable startup grace period (for example mqtt.initial_connect_timeout) would also solve it.
For the stale state: publishing bridge/state with an MQTT 5 message expiry, or documenting that consumers shouldn't trust a retained online on its own, would stop it masking outages.
Describe alternatives you've considered
- A Home Assistant automation that starts the add-on when
binary_sensor.zigbee2mqtt_running(the Supervisor sensor, disabled by default) has been off for 2 minutes, checking every 2 minutes. This is what I run now. It works, but it's HA-specific and every user has to discover it after an outage. A version that trusted the bridge sensor didn't fire, because of the stale retained state above. Z2M_WATCHDOG: from the docs andindex.js, it only applies after a successful first start, so it doesn't cover this.- Starting the broker first: there's no way to declare a dependency on an external broker from an HA add-on, and ordering across hosts isn't something I can guarantee.
Additional context
Logs and timeline: https://github.com/danielrosehill/Bug-Reports-And-Fixes/tree/main/zigbee2mqtt/2026-11-13-broker-not-ready-at-boot-stale-online-state
Related: #4408, #33622 (the same "keep retrying" request for network coordinators).
- Lingua principale
- TypeScript
- Stelle
- 15.8k
- Fork
- 2.1k
- Merge medio
- 1g 6h
- PR unite (30g)
- 38
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di Koenkk/zigbee2mqtt
-
[Feature request]: OTA for generated definitions of devices with an OTA client clusterForse già presa @jabla l’ha presa 1 giorno fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 80/100
Koenkk/zigbee2mqtt#33684 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
new device support
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
Koenkk/zigbee2mqtt#33674 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Feature request]: Support espzb adapter (ESP Zigbee SDK v2, ESP32-S31)Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
Koenkk/zigbee2mqtt#33668 ·
I maintainer di solito rispondono entro 1 giorno
-
problem
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 76/100
Koenkk/zigbee2mqtt#33645 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
Koenkk/zigbee2mqtt#33613 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di Koenkk/zigbee2mqtt
Issue simili
-
area:docs documentation good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 12 giorni
-
area:docs good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
thainanluiz/TSSuper#30 ·
-
[analytics-core] getQueryParams does not decode `+` as space and truncates values containing `=`Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
amplitude/Amplitude-TypeScript#2179 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
ci good first issue severity: low
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
TMHSDigital/GH-Arcade#111 ·