MQTT trying to connect before network is ready

I noticed that I had MQTT errors in the logs:

[14:04:49.749][W][mqtt:244]: Couldn't resolve IP address for 'mqtt.<my_domain_name>.<tld>'
[14:04:49.765][W][mqtt:367]: Disconnected: Unknown

And that is because MQTT is trying to connect before wifi is initialised. It eventually catches on, but it takes about 15-20s (just eyeballing the time). And works from that point on.

But I am doing this:

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  min_auth_mode: WPA2
  domain: .<my_domain_name>.<tld>
  on_connect:
    - mqtt.enable:

mqtt:
  broker: !secret mqtt_hostname
  discovery: true
  username: !secret mqtt_username
  password: !secret mqtt_password
  enable_on_boot: false

And that feels like a workaround. I was not able to find any official solution for this. Disabling mqtt by default and only enabling it when wifi connects does work but I feel that this should be the default behaviour. Or at least an option like mqtt.wait_for_network: true flag or something like that.

Does anyone have thoughts on this? Is this the expected behaviour?

I’m interested in other experienced thoughts on the matter. I’m using Z2M for several months now, it’s all working quite well (better than ever, in fact). But possibly related to your observation. . .

When I restart HA it takes about 15-20 seconds for my ZigBee temperature sensors to report, after it appears that all integrations are loaded and the dashboard is ready. I presumed that wasn’t out of hand, what with the many integrations and a heckofa LOT of Z2M devices I have.

For whatever that’s worth.

when ha is restarted, all sensors values turn unavailable. data topics in mqtt are not retained (for good reason) so HA cannot read them just after start. HA waits for data refresh.

Z2M needs to refresh these data, though not sure it has to request all zigbee devices for update or provides it from its cache).

Nothing to worry imo. Add this time to system outage.

1 Like

I am guessing it would work better if it waited for the networking (zigbee in your case) to be back up before trying mqtt … that may be the same case. I am thinking I may try to change the esphome mqtt implementation to fix this. Either opening a feature/bug request or writing it myself and trying to get it merged - but I am not sure I am willing to fight the people politics within the project.

not sure you are commenting on bob’s issue or yours.

Imo these are completely different cases.
Z2M communicates with MQTT using tcpip. This stack has to be initialized. It really is not related to zigbee.

Your case is about hw restart as far as I understand. Bob’s issue us about HA restart. MQTT waits on readiness of HA in order to republish all data.

BTW if it’s about mqtt as you are suggesting, then has nothing to do with HA. It’s mosquito server. However… imo it’s not mqtt that connects to anything else. Mqtt (Mosquito) is a server. HA, Z2M, NR acts as mqtt’s clients. these connect MQTT. So what you are observing requires more precise description

2 Likes