MQTT Broker Fails to connect on Windows Docker new install

I installed HomeAssistant core as a docker container on Windows. When I try adding MQTT integration (broker: localhost, port: 1883) I get an error saying “Failed to connect”. Nothing gets logged in HA logs.

I tried installing mosquitto as both a docker container, with and without username/password and then as a native windows service instead of docker. Mosquitto is working in both cases, tested it through powershell as well as MQTTExplorer application. Added it to path as well. So I don’t think it’s the problem on mosquitto’s side.

I was able to add simple integrations such as Google Cast and Local IP Address. I was also able to make ZHA work with my smart devices. So HA must be working as well. I’m just not able to add MQTT.

Any suggestions how to proceed?

MQTT integration

Can you connect to the Mosquitto using something like MQTT Explorer? This would prove that Mosquitto is working and eliminate a variable.

I was able to connect with MQTT Explorer. It’s in the initial post.

And you have used the same credentials and connection details in the MQTT intergration?

What do the Mosquitto logs show? They should show a connection from Home Assistant when Home Assistant starts up.

I first installed mosquitto without credentials so I didn’t enter anything for username/password in MQTT integration. Then I also tried adding authentication for mosquitto but HA integration still gave the same error.

When I connect to mosquitto with MQTT Explorer I get these logs in mosquitto:

2024-07-20T12:05:39: mosquitto version 2.0.18 running
2024-07-20T12:06:56: New connection from 172.20.0.1:33236 on port 1883.
2024-07-20T12:06:56: New client connected from 172.20.0.1:33236 as mqtt-explorer-1e9f8564 (p2, c1, k60).
2024-07-20T12:06:56: No will message specified.
2024-07-20T12:06:56: Sending CONNACK to mqtt-explorer-1e9f8564 (0, 0)
2024-07-20T12:06:56: Received SUBSCRIBE from mqtt-explorer-1e9f8564
2024-07-20T12:06:56: # (QoS 0)
2024-07-20T12:06:56: mqtt-explorer-1e9f8564 0 #
2024-07-20T12:06:56: Sending SUBACK to mqtt-explorer-1e9f8564
2024-07-20T12:06:56: Received SUBSCRIBE from mqtt-explorer-1e9f8564
2024-07-20T12:06:56: $SYS/# (QoS 0)
2024-07-20T12:06:56: mqtt-explorer-1e9f8564 0 $SYS/#
2024-07-20T12:06:56: Sending SUBACK to mqtt-explorer-1e9f8564
2024-07-20T12:06:56: Sending PUBLISH to mqtt-explorer-1e9f8564 (d0, q0, r1, m0, '$SYS/broker/version', ... (24 bytes))

But when I try to connect from HA, I don’t get ANY new logs in mosquitto. No logs get added to HA as well.

Instead of using localhost try the IP address of the machine running the docker instance.

1 Like

I did that (again) and it worked.

I could swear I had used all kinds of addresses, including my internal static IP. Probably something else got in the way the previous time I tried.

Thanks a lot!

Glad you got it working.

I found with Mosquitto once you have set it up and got it working never touch it again. Also backup your configs to save heartache.

1 Like

Home assistant likes to be installed with network_mode: host sharing the network with the host.

You will have to put that in your mosquitto’s compose file as well because otherwise mosquitto is gonna be listening on its own private localhost address inside the docker bridged network. And you must also drop any port bindings you may have defined for mosquitto because in host network_mode you are gonna be sharing the host’s network and listening on the the host’s real interfaces (local or public or what ever you have defined in your mosquitto config).