Error - Disconnected from MQTT server core-mosquitto:1883

I’m trying to set up an mqtt broker using the official addon, but I keep getting this same error and my ESPHome device cannot connect to it. Logs and steps below:

I downloaded the mqtt broker addon, set up a unique user called mqtt-user, allowed them to log in, local only, and gave them a password. I wnt into integrations and added the mqtt broker, input the username and password, proceeded to the next screen where I left everything default for now, and then attempted to listen to a room (button, for example) and I publish to that same room using the UI and I see the message pop up. When I go to check my ESPHome device, I have provided it with the same username and password via a secrets file, and it continues to pop up the error:

[18:40:04][C][mqtt:030]: Setting up MQTT...
[18:40:04][I][mqtt:175]: Connecting to MQTT...
[18:40:34][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[18:40:34][I][mqtt:175]: Connecting to MQTT...
[18:41:04][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[18:41:04][I][mqtt:175]: Connecting to MQTT...
[18:41:34][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[18:41:34][I][mqtt:175]: Connecting to MQTT...
[18:42:04][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[18:42:04][I][mqtt:175]: Connecting to MQTT...

I check the Home Assistant OS logs and I see the following error pop up every time I restart the mqtt broker addon:

Disconnected from MQTT server core-mosquitto:1883

and the log from the mqtt broker addon itself:

[s6-init] ensuring user provided files have correct perms...exited 0.
[fix-attrs.d] applying ownership & permissions fixes...
[fix-attrs.d] done.
[cont-init.d] executing container initialization scripts...
[cont-init.d] mosquitto.sh: executing... 
[17:20:03] INFO: SSL is not enabled
[cont-init.d] mosquitto.sh: exited 0.
[cont-init.d] nginx.sh: executing... 
[cont-init.d] nginx.sh: exited 0.
[cont-init.d] done.
[services.d] starting services
[17:20:03] INFO: Starting NGINX for authentication handling...
[services.d] done.
[17:20:03] INFO: Starting mosquitto MQTT broker...
1644798003: mosquitto version 1.6.12 starting
1644798003: |-- *** auth-plug: startup
[17:20:03] INFO: Successfully send discovery information to Home Assistant.
[17:20:03] INFO: Successfully send service information to the Supervisor.

This same error happens if I use the host name or if I use the IP address for HA. I’ve checked IP addresses and host names, changed the username and password multiple times and checked via the UI to confirm that I can listen and receive the messages from the test, I’ve uninstalled and reinstalled the addon, deleted the config, nothing is working to resolve this.

That might be your issue.

The integration uses a special user (homeassistant). You should not have to specify a username and password for the integration if you are using the Mosquitto addon. With the broker running try removing the mqtt integration, restart home assistant and then try adding the integration again.

https://github.com/home-assistant/addons/blob/89112f96e0db8f207adfb1c988f2d5a26bac9877/mosquitto/DOCS.md#how-to-use

Awesome! Thank you! This removed the HA error but my ESPHome device is still unable to connect. I used the username and password I set up before and the log loops:

[21:06:18][I][mqtt:175]: Connecting to MQTT...
[21:06:49][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[21:06:49][I][mqtt:175]: Connecting to MQTT...
[21:07:18][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[21:07:18][I][mqtt:175]: Connecting to MQTT...
[21:07:48][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[21:07:48][I][mqtt:175]: Connecting to MQTT...
[21:08:19][W][mqtt:260]: MQTT Disconnected: TCP disconnected.

mqtt broker logs show the following:

1644810408: Socket error on client <unknown>, disconnecting.
1644810528: New connection from 172.30.32.2 on port 1883.
1644810528: Socket error on client <unknown>, disconnecting.
1644810554: Saving in-memory database to /data/mosquitto.db.
1644810648: New connection from 172.30.32.2 on port 1883.
1644810648: Socket error on client <unknown>, disconnecting.
1644810768: New connection from 172.30.32.2 on port 1883.
1644810768: Socket error on client <unknown>, disconnecting.
1644810888: New connection from 172.30.32.2 on port 1883.
1644810888: Socket error on client <unknown>, disconnecting.
1644811008: New connection from 172.30.32.2 on port 1883.
1644811008: Socket error on client <unknown>, disconnecting.

Can you share your esphome mqtt config and your Mosquiito addon config?

Redact the password in both.

Sure thing!

ESPHome device yaml:

esphome:
  name: "iotbutton001"
  platform: esp8266
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "<snip>"
  
  
mqtt:
  broker: <HA IP>
  username: "<snip>"
  password: "<snip>"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Iotbutton001 Fallback Hotspot"
    password: "<snip>"

And my mqtt config (Options → Addons → mqtt broker → config):

certfile: fullchain.pem
customize:
  active: false
  folder: mosquitto
keyfile: privkey.pem
logins: []
require_certificate: false

I don’t have any usernames or passwords in my mqtt config because I made a Home Assistant user by the name of mqtt-user and the documentation says that all HA users should work automatically. I can log in to a third party app on a different LAN PC using that username and password and IP.

That dash could be an issue. Try a new user with an underscore instead mqtt_user

Don’t forget to change and update the ESP too.

Same issue:

[21:57:51][W][mqtt:260]: MQTT Disconnected: TCP disconnected.
[21:57:51][I][mqtt:175]: Connecting to MQTT...

But I can use mqtt_user and the pass to log in via MQTT Explorer on a different PC on the LAN.

It does not look like you are using this, so remove it:

Or this happens:

Screenshot 2022-02-14 at 15-44-32 MQTT Client Component

https://esphome.io/components/mqtt.html

But that’s not your issue. I’m out of ideas.

Actually this error isn’t due to your ESP:

It’s the supervisor watchdog and is expected. 172.30.32.2 is the internal docker network. See:

Good catch on the api portion. I have it left out of my other ones but forgot it on this one. I am getting this error now in ESPHome wifi logs:

ERROR Cannot connect to MQTT broker: [Errno 113] No route to host

I moved the mqtt section down to the bottom but maybe I need a longer delay before I call it to allow time to connect to the network?

Do you have Deep Sleep enabled?

Also double check this is correct:

mqtt:
  broker: <HA IP>
1 Like

Not yet but the idea is to wire the button to the reset pin, have the device reboot when the button is pressed, send the mqtt message then go into deep sleep.

I’m not sure why but typing my HA IP in plaintext worked even though my secrets has it correct and all my other ESPHome devices use it!

1 Like

You don’t actually need to obscure local IP addresses. They are not routable over the internet.

1 Like

I know, I just like to keep some of it secret in case, even though I apply that rule to internal IP addresses kind of willy nilly LOL