Issue with "Disconnected from MQTT (1)"

Hey, i just managed to setup my new Sonoff wifi-switch (using the guideline ). I managed to get everything up and running, but the MQTT-service disconnects for some reason. And I can’t figure out why…

“(Thread-10) [homeassistant.components.mqtt] Disconnected from MQTT (1). Trying to reconnect in 1 s”

Setup in Sonoff:

Code in Home Assistant:
port: 1883
client_id: home-assistant-1
username: XXX
password: XXX

switch 2:
platform: mqtt
name: “XXXXXXX”
command_topic: “cmnd/sonoff/power”
state_topic: “stat/sonoff/POWER”
qos: 1
payload_on: “ON”
payload_off: “OFF”
retain: true

Any ideas?


The log of your broker normally tells why a client is disconnected.

Where can I find the log files?

It depends on which broker you have, and how it was installed.

My mosquitto ones are in /var/log/mosquitto

Ahh, okey. Well I installed mqtt via sudo apt-get install mosquitto on my raspberry pi (

I went to the folder “/var/log/mosquitto/”. The folder exist but i cant see any files in the folder…

Check where the log file is being written to, which is defined in the configuration file.

The configuration file being used is defined in the startup line. To find this do

$ systemctl status mosquitto.service
● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
   Loaded: loaded (/etc/init.d/mosquitto)
   Active: active (running) since Fri 2017-04-28 11:35:13 BST; 3 days ago
  Process: 397 ExecStart=/etc/init.d/mosquitto start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mosquitto.service
           └─415 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Then my log file is

$ cat /etc/mosquitto/mosquitto.conf
# Place your local configuration in /etc/mosquitto/conf.d/
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /var/run/

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d
1 Like

hmm, I added in my mosquitto.conf

log_type all
log_dest file /var/log/mosquitto/mosquitto.log

But it seems like there is no log-file generated…

How are you starting mosquitto? The location of the configuration file needs to be specified in the command line.

Well, good question :slight_smile: (feel like this is partly out of my league…)

I followed the guidline here in order to install.

When I run “systemctl status mosquitto.service” I get this (it it helps)

● mosquitto.service - Mosquitto MQTT Broker daemon
Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled)
Active: activating (start) since Mon 2017-05-01 20:41:49 CEST; 4s ago
Control: 1235 (mosquitto)
CGroup: /system.slice/mosquitto.service
└─1235 /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

That all looks good. Did you restart mosquitto after changing the configuration file?

sudo systemctl restart mosquitto.service

yupp, did both a restart of the service and a complete reboot of system…

mmm… Maybe the log file isn’t being produced because it has nothing to report as nothing is connected. Mine only reports connections, disconnections and backups of the db. Try connecting with

mosquitto_sub -t "#" -v -u username -P password

I get this when I run “mosquitto_sub -t “#” -v -u username -P password”

owntracks/ftn/ftn-iphone {“tst”:XX,“acc”:XX,"_type":“location”,“alt”:XX,“lon”:XXXX,“vac”:14,“p”:XXX,“lat”:XXXX,“batt”:XX,“conn”:“w”,“tid”:“fn”}
tele/sonoff/LWT Online
cmnd/sonoff/power ON

Well at least your sonoff is connecting. Is there anything in the log file after doing that?

And just for completeness, you have set up a static ip address for your PI,so its address is still I’m running out of ideas here.

Exactly, the sonoff is connecting. And I can toggle the switch on, but then the “slider” in HASS just goes does not stay in the on state (i.e. I can’t turn it off…I have to do it from the sonoff web interface). Also, for some reason it switches itself on after ~30 s after I have toggled off in the web interface. So there is something fishy…

Still no log file though…

IP address is static yes.

Frustrating that I can’t figure out how to get the log file up and running…

And this is what it looks like when it turns itself on: (which I guess happens when MQTT reconnects?)

“tele/sonoff/LWT Online
cmnd/sonoff/power ON
tele/sonoff/LWT Online
cmnd/sonoff/POWER (null)
stat/sonoff/RESULT {“POWER”:“ON”}
stat/sonoff/POWER ON”

Sorry, I lost internet connection last night.

But after a nights sleep I still can’t see why mosquitto fails to create a log file. You may have an older version. Can you check the example file listed in my config to see if it the options for the config are the same? Maybe also check the level of logging that is default.

But I’m not seeing anything from HA in the mosquitto_sub response. Can you add a birth and a last will message to HA. That willl show that HA is connecting.

So this is what my mqtt code looks like now,

    port: 1883
    client_id: home-assistant-1
    username: XXX
    password: XXX

      topic: 'hass/status'
      payload: 'online'
      topic: 'hass/status'
      payload: 'offline'

When I run subscribe to the topic I see this output when restarting HASS:

pi@raspberrypi:~ $ mosquitto_sub -v -t ‘hass/status’ -u XXX -P XXX
hass/status online
hass/status online
hass/status online

So the brooker is online ? But it keeps disconnecting…

17-05-02 21:20:44 WARNING (Thread-10) [homeassistant.components.mqtt] Disconnected from MQTT (1). Trying to reconnect in 1 s
17-05-02 21:20:45 WARNING (Thread-10) [homeassistant.components.mqtt] Disconnected from MQTT (1). Trying to reconnect in 2 s