Paho MQTT Library breaks home assistant publishes

I’m trying to use a paho mqtt client with home assistant. However, when I subscribe to a topic that home assistant publishes to, it can’t receive it, and home assistant won’t publish any more messages until it is restarted. I also connected a mosquitto client to home assistant’s broker, and it sees all the messages that home assistant publishes until the paho client connects. After that, it sees one last message and then the home assistant broker hangs up.

Note that this only happens when the paho client is subscribed to a topic that home assistant publishes to. If it’s subscribed to a random other topic it works fine. (Works as in the mosquitto client continues to receive publishes from home assistant)

Heres the client code I’m using. It’s taken from the paho example. The only thing that was changed was the topic to subscribe to.

import paho.mqtt.client as mqtt

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))

    # Subscribing in on_connect() means that if we lose the connection and
    # reconnect then subscriptions will be renewed.
    client.subscribe("homeworks/dimmer/#")

# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost", 1883, 60)

# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()

And this is the relevant parts of the configuration.yml of my server.

mqtt:

light:
  - platform: mqtt_json
    name: Study Desk
    state_topic: homeworks/dimmer/1:4:2:7:3/state
    command_topic: homeworks/dimmer/1:4:2:7:3/command
    brightness: true

Does anyone have any ideas on how I could continue to debug this? Thanks!

Try using a local broker like mosquitto or mosca set up on your PC. Or try HA’s embedded broker. Rule out any weirdness with the current cloud broker you’re using. You say Home Assistant’s broker several times but it looks like your code is connecting to iot.eclipse.org… which is not HA’s broker…

@marthocoo Sorry, the code I posted was incorrect. The iot.eclipse.org is actually localhost in the code I’m running, I had just re-copied the example code for the forum post. It is HA’s embedded broker.

I will try using a different broker though.

I just tried it out on a mosquitto broker, and it works. I even configured home assistant to use the external mosquitto broker and everything works as expected, it’s just the internal broker that doesn’t. I think this may be a bug in home assistant, I’ll try to make a minimal reproducible environment tomorrow.

Ok, I made a docker container that reproduces the issue. Run it, and it will wait 20 seconds for home assistant to start up. Then it will wait 20 more seconds, during which time you can flip the switch on the home assistant interface and see mqtt messages printed out. Finally, it will wait one last 20 seconds with the python script also running and you’ll see that at most only one more mqtt message is sent as you flip the switch.

Could someone test it out and make sure I’m not just crazy / got something wrong? I’ll post this on the github issues if that’s not the case.

I’ve run into this issue as well. It so happens that Paho MQTT is sending a zero-length client id when none is provided at client creation time. The HA broker should be generating a random one and sending it back, but instead it appears to be choking.

You can get around this in the short term by just generating a client id yourself:

client = mqtt.Client("bogus-id")

The HA broker seems to handle this as it should.

I’ve submitted a one liner pull request to the hbmqtt project that fixes this:

Thank you @phooky! Hopefully I can get rid of mosquitto in the next home assistant update.