Problems with MQTT configuraion

Hello All!

I am trying to get into Home Assistant and I’m having a hard time configuring mqtt. Every time HA starts, I am getting this in my logs:

2018-04-24 00:30:00 INFO (MainThread) [homeassistant.setup] Setting up mqtt
2018-04-24 00:30:00 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_registered[L]: domain=mqtt, service=publish>
2018-04-24 00:30:00 INFO (MainThread) [homeassistant.setup] Setup of domain mqtt took 0.2 seconds.
2018-04-24 00:30:00 INFO (MainThread) [homeassistant.core] Bus:Handling <Event component_loaded[L]: component=mqtt>
2018-04-24 00:30:00 ERROR (Thread-2) [homeassistant.components.mqtt] Unable to connect to the MQTT broker: Connection Refused: not authorised.
.....
2018-04-24 00:30:23 DEBUG (MainThread) [homeassistant.components.mqtt] Subscribing to stat/sonoff/POWER
2018-04-24 00:30:23 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 258, in _async_add_entity
    await entity.async_added_to_hass()
  File "/usr/lib/python3.6/site-packages/homeassistant/components/switch/mqtt.py", line 115, in async_added_to_hass
    self._qos)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 241, in async_subscribe
    topic, msg_callback, qos, encoding)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 559, in async_subscribe
    await self._async_perform_subscription(topic, qos)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 594, in _async_perform_subscription
    _raise_on_error(result)
  File "/usr/lib/python3.6/site-packages/homeassistant/components/mqtt/__init__.py", line 680, in _raise_on_error
    'Error talking to MQTT: {}'.format(mqtt.error_string(result_code)))
homeassistant.exceptions.HomeAssistantError: Error talking to MQTT: The client is not currently connected.

My MQTT configuration looks like this:

mqtt:
    broker: 192.168.1.38
    port: 1883
    client_id: my-prefix-hassio
    username: hassio
    password: hassio

If I ssh into my hassio installation and run

mosquitto_sub -h 192.168.1.38 -p 1883 -u hassio -P hassio -I my-prefix -t "stat/sonoff/POWER"

I am able to connect and see updates.

I know that I am missing something silly… just can’t put a finger on it.

Oh, I should probably state that I am running:

H/W: Raspberry Pi 3
OS: Hass.io
HA Version: 0.67.1
Mosquitto version: 1.4.12 (build date 2017-06-01 13:03:46+0000) (running in a docker container on my Synology NAS)

Any help is appreciated!

Thanks.

I was trying to see where mqtt python module lives on hass.io, but could not find it. My thought process was to write a quick python script that would connect to my mqtt broker. Just trying to rule out one thing at a time.

any suggestions?

with HASSiO mqtt configuration has been made really simple and you no longer need to ssh it or direct it to your broker ip, just enable it from your "HASS.io tab from your front end.

yaml file entry is then…

mqtt:
  broker: core-mosquitto
  username: username
  password: password

this could help…

1 Like

so, is core-mosquitto an internal broker or an external one? I have a broker configured on another machine and I would like to use that and not a broker on the same machine as hass.

Here’s a switch that I currently have:

switch:
    - platform: mqtt
      name: "test sonoff"
      command_topic: "cmnd/sonoff/power"
      state_topic: "stat/sonoff/POWER"
      qos: 1
      payload_on: "ON"
      payload_off: "OFF"
      retain: true

I guess you’d have to direct your yaml to the external mqtt broker and there would be no need for any additional set up on the HASS RPI but I’m no expert on this…

assuming your broker is on the following ip:

mqtt:
  broker: 192.168.1.38
  port: 1883
  client_id: ?
  keepalive: 60
  username: username
  password: password
  protocol: 3.1

and then the switch like your “sonoff” would work directing it to the platform: mqtt.

Is there a reason why you prefer a separate broker?

1 Like

I have tried your suggested setup (keepalive and protocol) and I’m having the same issue :frowning:
Interestingly enough, when I look at my mosquitto logs, all I see is this:

1524675709: Socket error on client <unknown>, disconnecting.
1524676392: New connection from 172.17.0.1 on port 1883.

Which leads me to believe that connection is made. (Socket error line is when I restarted hass)

So, the question is: why does hass report:

2018-04-25 10:13:12 ERROR (Thread-2) [homeassistant.components.mqtt] Unable to connect to the MQTT broker: Connection Refused: not authorised.

As for your question re: separate broker:
I have a synology nas and I’ve dockorized a lot of service I am using within my home setup (various web servers, etc). Since all my infrastructure is tied to that nas, it would be:

  • Easier to manage (everything’s in one place)
  • Easier to monitor logs (same reason as above)
  • When I drill holes in my firewall for external access, fewer internal IPs are exposed.

…could be user/password error but i have only ever used a local broker so not sure

no, the password is correct, but it does have a special character in it.

I’ve tried to put the whole thing in ", but it didn’t do anything. i’m going to try to escape my special character now.

hmmm…
I’ve added another user to my broker, but same results…

Then, I’ve decided to comment out my config line that requires a certain client id prefix

clientid_prefixes my-prefix

and it started working…

Thanks for your help!

I am sooooo confused…
I’ve just re-enabled clientid_prefixes setting and it is still working… :confused: