Random MQTT disconnects on Hass.io

Hi
I am wanting to know how to access the logs of mosquito on Hass.io as I am having a few issues.

I am fairly new to MQTT, I have about 3 wemos d1 minis, 1 x RF Bridge and 2 magichome Led strips flashed with a mixture of Espurna and Tasmota, Randomly 1 or 2 will refuse to connect and it takes a few reboots of things to get them back online. There seems to be no pattern to why and the only logs I get are from the devices itself. Hence I want to see what mosquitto says what is going on.

example of connection error

tasmota
06:58:13 MQT: Attempting connection…
06:58:18 MQT: Connect failed to 192.168.1.5:1883, rc -2. Retry in 10 sec

espurna
[658671] [MQTT] Connecting to broker at 192.168.1.5:1883
[658673] [MQTT] Client ID: strip1
[658674] [MQTT] QoS: 0
[658675] [MQTT] Retain flag: 1
[658676] [MQTT] Keepalive time: 30s
[658677] [MQTT] Will topic: kitchen-strip-1/status

might sound like a stupid thing, but make sure your client IDs are unique across the MQTT network…

try to take a look at this issue: https://github.com/arendst/Sonoff-Tasmota/issues/2605 and revert back to 5.12.0 to see if everything is OK

do you have any problem with your espurna devices (it’s not clear in what you wrote)?

also check the user/password of your MQTT broker

I have confirmed all devices have unique ids. The issue is both with espurna and tasmota. One device I even flashed from espurna to tasmota to test and both times it would not connect… various, reboots of pi, devices and router saw no change then it came back up on its own but has been on and off ever since along with a few others. My Wifi is strong… the errors I pasted above were pulled off the devices over wifi when they were not connecting to MQTT. I have also tested both with usernames and passwords and without.

to access the mosquitto log on hassio:

  • go to hass.io (left panel) in home assistant
  • in the add-ons menu click on mosquitto broker
  • scroll all the way down

Thanks. I was hoping to find a bit more informative log than what is there. I am going to do a bit more testing soon and maybe set up mqtt on another pi I have laying around and maybe try a simpler mqtt device like bruh’s multisensor arduino code to see if there is some sort of pattern to it all.

Did your devices showed up in mosquitto log?
Did you try to uninstall and reinstall mosquitto broker? Can I see your mosquitto config, your espurna debug log after a reboot of the device?

I have uninstalled and reinstalled the broker plugin several time. I even did a full re-image of Hass.io (due to unrelated SD card issue). I currently have 1 device that was running an older espurna version turned off and everything has been ok for the last day and a half. I will monitor it for a few more days and see if any change, I will report back my findings then. It’s a pity the log for mosquitto is not longer.

note I previously has user and pass on my devices but I turnt off for testing but it made no difference.

mosquitto config
{
“plain”: true,
“ssl”: false,
“anonymous”: true,
“logins”: [],
“customize”: {
“active”: false,
“folder”: “mosquitto”
},
“certfile”: “fullchain.pem”,
“keyfile”: “privkey.pem”
}

configuration.yaml
mqtt:
broker: core-mosquitto

espurna log after reboot
[077788] [WEBSOCKET] #1 connected, ip: 192.168.1.27, url: /ws
[115560] [MQTT] Received kitchen-strip-1/relay/0/set => 0
[115615] [MQTT] Received kitchen-strip-2/relay/0/set => 0
[258215] [MQTT] Received kitchen-strip-1/relay/0/set => 0
[258343] [MQTT] Received kitchen-strip-2/relay/0/set => 0
[306502] [MQTT] Sending kitchen-strip-2/app => ESPURNA (PID 1)
[306504] [MQTT] Sending kitchen-strip-2/version => 1.12.6 (PID 1)
[306506] [MQTT] Sending kitchen-strip-2/board => MAGICHOME_LED_CONTROLLER_20 (PID 1)
[306511] [MQTT] Sending kitchen-strip-2/host => kitchen-strip-2 (PID 1)
[306517] [MQTT] Sending kitchen-strip-2/ip => 192.168.1.34 (PID 1)
[306523] [MQTT] Sending kitchen-strip-2/mac => 60:01:94:AF:D2:CD (PID 1)
[306529] [MQTT] Sending kitchen-strip-2/rssi => -75 (PID 1)
[306534] [MQTT] Sending kitchen-strip-2/uptime => 306 (PID 1)
[306540] [MQTT] Sending kitchen-strip-2/datetime => 2018-05-26 02:08:34 (PID 1)
[306547] [MQTT] Sending kitchen-strip-2/freeheap => 19648 (PID 1)
[306552] [MQTT] Sending kitchen-strip-2/relay/0 => 0 (PID 1)
[306558] [MQTT] Sending kitchen-strip-2/rgb => #1C6464 (PID 1)
[306563] [MQTT] Sending kitchen-strip-2/hsv => 180,72,25 (PID 1)
[306569] [MQTT] Sending kitchen-strip-2/brightness => 162 (PID 1)
[306574] [MQTT] Sending kitchen-strip-2/mired => 153 (PID 1)
[306580] [MQTT] Sending kitchen-strip-2/channel/0 => 28 (PID 1)
[306585] [MQTT] Sending kitchen-strip-2/channel/1 => 100 (PID 1)
[306591] [MQTT] Sending kitchen-strip-2/channel/2 => 100 (PID 1)
[306596] [MQTT] Sending kitchen-strip-2/channel/3 => 0 (PID 1)
[306603] [MQTT] Sending kitchen-strip-2/vcc => 3242 (PID 1)
[306607] [MQTT] Sending kitchen-strip-2/status => 1 (PID 1)
[306612] [MQTT] Sending kitchen-strip-2/loadavg => 1 (PID 1)

mosquito log after reboot of above
1527293014: New connection from 192.168.1.34 on port 1883.
1527293014: Client strip2 disconnected.
1527293014: New client connected from 192.168.1.34 as strip2 (c0, k30).

One tasmota has gone down again and I don’t know why. reboot the device, start stop mosquitto. reboot hass.io no difference.

tasmota log
00:00:00 Project sonoff Bedroom (Topic bedroom, Fallback bedroom, GroupTopic sonoffs) Version 5.14.0-2_3_0
00:00:00 WIF: Connecting to AP1 xxy-router in mode 11N as bedroom…
00:00:05 WIF: Connected
00:00:05 DNS: Initialized
00:00:05 HTP: Web server active on bedroom.local with IP address 192.168.1.17
00:00:06 MQT: Attempting connection…
23:29:55 MQT: Connect failed to 192.168.1.5:1883, rc -2. Retry in 10 sec
23:30:11 MQT: Attempting connection…
23:30:17 MQT: Connect failed to 192.168.1.5:1883, rc -2. Retry in 10 sec

only thing in mosqitto log
1527805193: Saving in-memory database to /data/mosquitto.db.

I really don’t know how to help you. Here is my config for a wemos D1 mini:

Tasmota log:

00:00:00 WIF: Connecting to AP1 WiFi-xxxx in mode 11N as wemos-6194...
00:00:04 WIF: Connected
00:00:04 DNS: Initialized
00:00:04 HTP: Web server active on wemos-6194.local with IP address 192.168.1.26
00:00:05 RSL: stat/wemos/STATUS = {"Status":{"Module":18,"FriendlyName":["Wemos"],"Topic":"wemos","ButtonTopic":"0","Power":0,"PowerOnState":3,"LedState":1,"SaveData":1,"SaveState":1,"ButtonRetain":0,"PowerRetain":0}}
00:00:05 RSL: stat/wemos/STATUS1 = {"StatusPRM":{"Baudrate":115200,"GroupTopic":"sonoffs","OtaUrl":"http://sonoff.maddox.co.uk/tasmota/sonoff.ino.bin","RestartReason":"External System","Uptime":"0T00:00:05","StartupUTC":"","Sleep":0,"BootCount":237,"SaveCount":252,"SaveAddress":"F7000"}}
00:00:05 RSL: stat/wemos/STATUS2 = {"StatusFWR":{"Version":"5.14.0","BuildDateTime":"2018-05-15T15:29:54","Boot":31,"Core":"2_3_0","SDK":"1.5.3(aec24ac9)"}}
00:00:05 RSL: stat/wemos/STATUS3 = {"StatusLOG":{"SerialLog":2,"WebLog":2,"SysLog":0,"LogHost":"","LogPort":514,"SSId":["WiFi-xxxx",""],"TelePeriod":300,"SetOption":["00008009","55818000"]}}
00:00:05 RSL: stat/wemos/STATUS4 = {"StatusMEM":{"ProgramSize":526,"Free":476,"Heap":20,"ProgramFlashSize":1024,"FlashSize":4096,"FlashMode":3}}
00:00:05 RSL: stat/wemos/STATUS5 = {"StatusNET":{"Hostname":"wemos-6194","IPAddress":"192.168.1.26","Gateway":"192.168.1.1","Subnetmask":"255.255.255.0","DNSServer":"192.168.1.1","Mac":"BC:DD:C2:0A:B8:32","Webserver":2,"WifiConfig":2}}
00:00:05 RSL: stat/wemos/STATUS6 = {"StatusMQT":{"MqttHost":"hassio.lan","MqttPort":1883,"MqttClientMask":"DVES_%06X","MqttClient":"DVES_0AB832","MqttUser":"homeassistant","MqttType":1,"MAX_PACKET_SIZE":1000,"KEEPALIVE":15}}
00:00:05 RSL: stat/wemos/STATUS7 = {"StatusTIM":{"UTC":"Thu Jan 01 00:00:05 1970","Local":"Thu Jan 01 00:00:05 1970","StartDST":"Thu Jan 01 00:00:00 1970","EndDST":"Thu Jan 01 00:00:00 1970","Timezone":1,"Sunrise":"07:43","Sunset":"16:03"}}
00:00:06 RSL: stat/wemos/STATUS10 = {"StatusSNS":{"Time":"1970-01-01T00:00:05","AM2301":{"Temperature":26.2,"Humidity":48.6},"TempUnit":"C"}}
00:00:06 RSL: stat/wemos/STATUS11 = {"StatusSTS":{"Time":"1970-01-01T00:00:06","Uptime":"0T00:00:05","Vcc":2.700,"Wifi":{"AP":1,"SSId":"WiFi-xxxx","RSSI":86,"APMac":"EE:08:6B:33:78:00"}}}
00:00:06 MQT: Attempting connection...
00:00:06 MQT: Connected
00:00:06 MQT: tele/wemos/LWT = Online (retained)
00:00:06 MQT: cmnd/wemos/POWER = 
00:00:06 MQT: tele/wemos/INFO1 = {"Module":"Generic","Version":"5.14.0","FallbackTopic":"DVES_0AB832","GroupTopic":"sonoffs"}
00:00:06 MQT: tele/wemos/INFO2 = {"WebServerMode":"Admin","Hostname":"wemos-6194","IPAddress":"192.168.1.26"}
00:00:06 MQT: tele/wemos/INFO3 = {"RestartReason":"External System"}
00:07:34 MQT: stat/wemos/STATUS = {"Status":{"Module":18,"FriendlyName":["Wemos"],"Topic":"wemos","ButtonTopic":"0","Power":0,"PowerOnState":3,"LedState":1,"SaveData":1,"SaveState":1,"ButtonRetain":0,"PowerRetain":0}}
00:07:34 MQT: stat/wemos/STATUS1 = {"StatusPRM":{"Baudrate":115200,"GroupTopic":"sonoffs","OtaUrl":"http://sonoff.maddox.co.uk/tasmota/sonoff.ino.bin","RestartReason":"External System","Uptime":"0T00:00:08","StartupUTC":"2018-06-03T23:07:26","Sleep":0,"BootCount":237,"SaveCount":252,"SaveAddress":"F7000"}}
00:07:34 MQT: stat/wemos/STATUS2 = {"StatusFWR":{"Version":"5.14.0","BuildDateTime":"2018-05-15T15:29:54","Boot":31,"Core":"2_3_0","SDK":"1.5.3(aec24ac9)"}}
00:07:34 MQT: stat/wemos/STATUS3 = {"StatusLOG":{"SerialLog":2,"WebLog":2,"SysLog":0,"LogHost":"","LogPort":514,"SSId":["WiFi-xxxx",""],"TelePeriod":300,"SetOption":["00008009","55818000"]}}
00:07:34 MQT: stat/wemos/STATUS4 = {"StatusMEM":{"ProgramSize":526,"Free":476,"Heap":19,"ProgramFlashSize":1024,"FlashSize":4096,"FlashMode":3}}
00:07:34 MQT: stat/wemos/STATUS5 = {"StatusNET":{"Hostname":"wemos-6194","IPAddress":"192.168.1.26","Gateway":"192.168.1.1","Subnetmask":"255.255.255.0","DNSServer":"192.168.1.1","Mac":"BC:DD:C2:0A:B8:32","Webserver":2,"WifiConfig":2}}
00:07:34 MQT: stat/wemos/STATUS6 = {"StatusMQT":{"MqttHost":"hassio.lan","MqttPort":1883,"MqttClientMask":"DVES_%06X","MqttClient":"DVES_0AB832","MqttUser":"homeassistant","MqttType":1,"MAX_PACKET_SIZE":1000,"KEEPALIVE":15}}

Thanks, That all looks similar. I am in a testing phase at the moment trying to find a pattern… (Bit hard when it works for a day or 2 and then shows up offline). I believe RC 2 = “Connection Refused, identifier rejected”. I tried both embedded broker and hass.io mosquitto addon… same error. I also can all devices connect in the log with a unique id. I went back to having a user and pass and found if I hit save on the mqtt parameters page without changing anything when it was in fault it would come online, but a reboot by itself did nothing.

My current test is a fresh install of rasbian and mosquitto on a spare pi I had laying about. I have 5 tasmotas and HA connected to it ok at the moment

See bottom of this page GitHub - arendst/Tasmota: Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at :

RC -2: MQTT_CONNECT_FAILED - the network connection failed

Some people say the problem it’s the firewall or homebridge… Your sonoffs are on the same network as your MQTT broker?

There is only 1 network. My network is pretty much default settings the D-link router came with. The wifi is strong at all devices. It’s been 2 days now since moving mosquitto to my old pi and I have had no hickups so far.

1st Timer on HA I just set-up my Sprinkler System it run’s on esp8266 tho MQTT.
lastnight it worked

today it stop working I don’t know if I broken it because I chagen something when playing around with groups.

MQTT cofig

{
  "logins": [],
  "anonymous": false,
  "customize": {
    "active": false,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

MQTT Log

1542126073: Socket error on client <unknown>, disconnecting.
1542126073: New connection from 192.168.0.247 on port 1883.
1542126074: Socket error on client <unknown>, disconnecting.
1542126075: New connection from 192.168.0.247 on port 1883.
1542126077: Socket error on client <unknown>, disconnecting.
1542126078: New connection from 192.168.0.247 on port 1883.
1542126079: Socket error on client <unknown>, disconnecting.
1542126195: New connection from 192.168.0.247 on port 1883.
1542126196: Socket error on client <unknown>, disconnecting.
1542126197: New connection from 192.168.0.247 on port 1883.
1542126199: Socket error on client <unknown>, disconnecting.
  customize:
    switch.zone_1:
      friendly_name: Z1
      entity_picture: "/local/water3.png"
    switch.zone_2:
      friendly_name: Z2
      entity_picture: "/local/water3.png"
    switch.zone_3:
      friendly_name: Z3
      entity_picture: "/local/water3.png"
  
group:
####VIEW####
  living_room_view:
    view: true
    name: Living Room
    icon: mdi:home
    entities:
      - group.living_room_lights
      - group.tV_on_off
  garden_view:
    view: true
    name: Garden
    icon: mdi:home
    entities:
      - group.sprinker
###Groups###
  living_room_lights:
    name: Lights
    icon: mdi:account-multiple
    entities:
      - light.yeelight_rgb_34ce0084cf08
  tv_on_off:
    name: Remote
    icon: mdi:account-multiple
    entities:
      - switch.hifi
      - switch.projector
  sprinker:
    name: Sprinker
    icon: mdi:account-multiple
    entities:
      - switch.zone_1
      - switch.zone_2
      - switch.zone_3

#Sprinker System1
switch SprinklerSystem1:
  - platform: mqtt
    name: "Zone 1"
    command_topic: "MKSmartHouse/utilities/MkSprinklerSystem"
    state_topic: "MKSmartHouse/utilities/MkSprinklerSystem/state"
    payload_on: "Z1ON"
    payload_off: "Z1OFF"
    retain: true
    
  - platform: mqtt
    name: "Zone 2"
    command_topic: "MKSmartHouse/utilities/MkSprinklerSystem"
    state_topic: "MKSmartHouse/utilities/MkSprinklerSystem/state"
    payload_on: "Z2ON"
    payload_off: "Z2OFF"
    retain: true
    
  - platform: mqtt
    name: "Zone 3"
    command_topic: "MKSmartHouse/utilities/MkSprinklerSystem"
    state_topic: "MKSmartHouse/utilities/MkSprinklerSystem/state"
    payload_on: "Z3ON"
    payload_off: "Z3OFF"
    retain: true

Here is how i fixed this problem.
My setup:

  • multiple custom NodeMcu with arduino sketch.
  • Hass.io with Mosquitto Add-on.

How i fixed all errors. First i made sure to update all my Arduino IDE library especially made sure that PubSubClient library is up to the last version and update the sketch on all devices…

Also i use a different Client name, user name and password for each NodeMCu to connect to Mosquitto.

After doing all this everything work without a glitch for 3 weeks now.

Did this keep on working for you? I’m facing the same issue.