core: MQTT does not start up properly (sometimes)

The problem

When restarting HA (to apply new settings, updates, etc.) I have a fair chance that MQTT is seemingly starting up properly - but missing certain functionalities. Most prominently, button events are not registered, even though they are clearly visible in Zigbee2MQTT.

The usual workaround: Restarting MQTT until it works again. Sometimes it works after restarting once, sometimes it need 2 or more restarts until all events are registered again.

In the logfile I cannot find anything remotely related. The events seem to just “vanish” silently.

What version of Home Assistant Core has the issue?

core-2024.4.3

What was the last working version of Home Assistant Core?

core-2024.1 (maybe)

What type of installation are you running?

Home Assistant OS

Integration causing the issue

MQTT

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

nothing I can remotely pin point to the issue

Additional information

This issue is fairly new. It probably started in Release 2024.3 or so.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 63 (26 by maintainers)

Commits related to this issue

Most upvoted comments

If you would like to try it as a custom component you can run the following:

It worked, thanks.

Thanks for testing. I opened a PR to the upstream library as well so we won’t have to use the workaround long term. https://github.com/eclipse/paho.mqtt.python/pull/843

@alexdelprete

I opened https://github.com/home-assistant/core/pull/117672 which should workaround the issue with websockets. Unfortunately I cannot test it myself at the moment because I am on limited emergency power due to the storms in Houston.

If you would like to try it as a custom component you can run the following:

cd /config
curl -o- -sSL https://gist.githubusercontent.com/bdraco/43f8043cb04b9838383fd71353e99b18/raw/core_integration_pr | bash /dev/stdin -d mqtt -p 117672

If that doesn’t solve it delete /config/custom_components/mqtt to get back to the core version

@umrath Based on the test I have done, I think https://github.com/home-assistant/core/pull/117267 will finally restore functionallity. In a larger setup it seems the default socket buffers are not big enough. Also we reverted increasing the initial subcribe cooldown, so we should wait less long before actually start subscribing to the broker.

Does the configuration of mqtt itself matter, e.g. the protocol version? For some reason it defaults to 3.1.1, which I haven’t changed so far.

That should be fine

Thanks. If this will be in .5 can I simply delete the custom_components/mqtt folder and restart HA to go back to core MQTT client?

Yes exactly

I think #117267 will finally restore functionallity.

.4 actually broke my HA->MQTT connection, going back to .3 works again. Here’s the error in the log:

2024-05-17 21:19:38.118 ERROR (MainThread) [homeassistant] Error doing job: Exception in callback MQTT._async_on_socket_open(<paho.mqtt.cl...x785546020e00>, None, <paho.mqtt.cl...x785546023dd0>)
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/asyncio/events.py", line 88, in _run
    self._context.run(self._callback, *self._args)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/client.py", line 582, in _async_on_socket_open
    self._increase_socket_buffer_size(sock)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/client.py", line 550, in _increase_socket_buffer_size
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, new_buffer_size)
    ^^^^^^^^^^^^^^^
AttributeError: 'WebsocketWrapper' object has no attribute 'setsockopt'

It looks like the paho passes the wrapper when using websockets and setsockopt method isn’t exposed on the WebsocketWrapper. That’s unfortunate. It looks like their example also has the same issue https://github.com/eclipse/paho.mqtt.python/blob/d45de3737879cfe7a6acc361631fa5cb1ef584bb/examples/loop_asyncio.py#L92

Let me see if I can come up with a workaround

I have been having issues with my Roborock S5 not reconnecting to HA and all of my Zigbee buttons being non-responsive after a restart. I actually had the same problem in the subsequent reboot after upgrading to 2024.5.2 but I just restarted again and everything worked.

@umrath How to restart MQTT to fix the issue after HA restart? I tried to reload and to disable/ enable MQTT without any success. Currently running on 2024.4.4, downgraded from 2024.5.1 - faced the issue first time not directly after the upgrades, it appeared after VM restart due to maintenance of the host system.

I reload the integration. In 2024.5.1 this fixed the problem reliably twice in a row (did not work in 2024.5).

@umrath clear, thanks!

cd /config
curl -o- -sSL https://gist.githubusercontent.com/bdraco/43f8043cb04b9838383fd71353e99b18/raw/core_integration_pr | bash /dev/stdin -d mqtt -p 116550

If you are adventurous and want to test as a custom component

You have to wait till 2025.5.1

That does sound like the exact problem we just fixed in https://github.com/home-assistant/core/pull/116471

There was a change in 2024.5.b0 causing MQTT not to publish a will. That is offline to topic homeassistant/status. This could cause that Z2M is not triggered to publish configs and states. It was fixed with 2024.5.0b2. May be you can check if that solves the issue?

I tested. It’s mentioned here https://github.com/home-assistant/core/issues/115958#issuecomment-2082916942