I've broken it (Hass.io MQTT Mosquitto ESPurna) it was half working before

I’m really sorry to have to ask for help, i’ve tried my hardest (was up till 4am last night fiddling) and i’ve utterly messed it up so i need to reach out.

So i’ve got Hass.IO installed on a Pi2 and i’ve flashed some Sonoff devices with ESPurna - 5 devices in total.

I got the AP html page for each of the devices and configured it how i thought it should be and got two of them working nicely but just couldn’t get the other three to trigger so i played and played and played and broke everything trying to fix it. Completly do not understand what the issue is or what i’m doing wrong. I’m sure it’s something simple but i can’t figure it out.

Pi2 with Home Assistant v0.58.1
Mosquitto broker running on above v3.2.2 (possibly, three versions come up in Hass.IO)

My Mosquitto settings within Hass.io:

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

My conf.yaml for MQTT:

port: 1883
keepalive: 60
username: !secret mqtt_username
password: !secret mqtt_password

My switch settings within conf.yaml


  • platform: mqtt
    name: “Kitchen Lights”
    state_topic: “/sonoff/kitchenlights/relay/0”
    command_topic: “/sonoff/kitchenlights/relay/0/set”
    payload_on: 1
    payload_off: 0
    optimistic: false
    qos: 0
    retain: true

Logs from within Hass.IO
starting version 3.2.2
1512490784: mosquitto version 1.4.12 (build date 2017-06-01 13:03:48+0000) starting
1512490784: Config loaded from /etc/mosquitto.conf.
1512490784: Opening ipv4 listen socket on port 1883.
1512490784: Opening ipv6 listen socket on port 1883.
1512490784: Opening ipv4 listen socket on port 8883.
1512490784: Opening ipv6 listen socket on port 8883.
1512490784: Warning: Mosquitto should not be run as root/administrator.
1512490784: New connection from on port 1883.
1512490784: Client esp8266b4e11e disconnected.
1512490784: New client connected from as esp8266b4e11e (c0, k30, u’homeassistant’).
1512490806: New connection from on port 1883.
1512490806: Client esp826607bce1 disconnected.
1512490806: New client connected from as esp826607bce1 (c0, k30, u’homeassistant’).
1512490836: New connection from on port 1883.
1512490836: New client connected from as 82b77bd8-0cfc-45b8-826e-78d105ca2eae (c1, k60, u’homeassistant’).
1512492585: Saving in-memory database to /data/mosquitto.db.

Sonoff settings:
Hostname: Kitchen Lights
MQTT Broker:
MQTT Port: 1883
MQTT User: homeassistant
MQTT Password: (same as !Secret password)
MQTT Root Topic: sonoff/kitchenlights

One immediately obvious error is you haven’t set a username/password for MQTT and yet you’ve put one in your config yaml and the Sonoff for some reason.

Hi Bobby,

Yeah i’d tried it with and without and i don’t think it makes any difference. I’m sure, from memory that when i did have two switches working the username and password wasn’t in the MQTT script but it was late last night and i was pulling my hair out trying to get the others to work so i might be wrong. That said, i’ve just now added UN and PW details to the script, rebooted and it didn’t make a difference.

Do you know how i should configure the "plain", "ssl" and "anonymous" aspects of the MQTT script? I’ve searched for an explaination on this and i can’t find much.

Not sure but there’s no need to change anything at all in the MQTT add on other than if you want to add username and password.

Set your config yaml like this…

  broker: core-mosquitto
  username: mqttusername
  password: mqttpassword

Not sure about the ESPurna settings as I used Tasmota and the topics are a bit different.

I’ve tried it every which way Bobby, using core-mosquitto and using the IP of the HA but within the Sonoff i was struggling to get it to connect using the broker name rather than IP. Using the IP address I can see that the device connects to the broker with the correct credentials. If i remove the username and password details from the sonoff and reboot it, then it will not reconnect and i’d prefer to use credentials to try and keep things as secure as possible.

starting version 3.2.2
1512679501: mosquitto version 1.4.12 (build date 2017-06-01 13:03:48+0000) starting
1512679501: Config loaded from /etc/mosquitto.conf.
1512679501: Opening ipv4 listen socket on port 1883.
1512679501: Opening ipv6 listen socket on port 1883.
1512679501: Warning: Mosquitto should not be run as root/administrator.
1512679524: New connection from on port 1883.
1512679524: Client esp8266b4e11e disconnected.
1512679524: New client connected from as esp8266b4e11e (c0, k30, u’homeassistant’).
1512679555: New connection from on port 1883.
1512679555: New client connected from as 682d5c53-aae4-4dc8-b79a-677961bedf1e (c1, k60, u’homeassistant’).
1512679930: Client esp8266b4e11e disconnected.
1512679930: New connection from on port 1883.
1512679930: Socket error on client , disconnecting.

The logs from it connecting and then me removing the credentials frrom the sonoff. I think my issue is around the the topic but for the life of me i just can’t figure it out. I wanted to attach a screenshot but the forum won’t let me. I’m just not sure where slashes should be used. Looking through the documentation i believe i’m creating the right topics but i’m not sure about slashes or if i need this identifier or the placement of the identifier as it doesn’t state in the documentation.

This is the root topic for this device. A trailing slash will be added if not preset. The {identifier} placeholder will be replaced by the device hostname.

  • /relay/#/set Send a 0 or a 1 as a payload to this topic to switch it on or off. You can also send a 2 to toggle its current state. Replace # with the switch ID (starting from 0). If the board has only one switch it will be 0.
  • /status The device will report a 1 to this topic every few minutes. Upon MQTT disconnecting this will be set to 0.
  • Other values reported (depending on the build) are: firmware and version, hostname, IP, MAC, signal strenth (RSSI), uptime (in seconds), free heap and power supply.

Well, while the misses was out i used my time wisely to play with marthocoo GarHAge to flash myself a garage controller. I’d already bought the hardware so i had a ESP8266 and relay and have spent the last hour flashing it and getting the code into home assistant and have got it working without issue and first time so that’s cheered me up. :slight_smile: Proof if nothing else that the MQTT broker is working fine but i’ve not tried to tackle the topics for the ESPurna Sonoff’s.

I feel like i’m talking to myself a little here but nevertheless i have spent the past half an hour working through my ESPurna topics and had a brainwave. So the /{identifier} within the MQTT page translates to the hostname you set the device, this hostname can be upper and lowercase but i believe topics are case sensitive. So after considering this i went back to the drawing board with some sensible hostnames that i can copy in config.yaml.

for instance, in the ESPurna flashed sonoff settings, select your chosen hostname, let’s use Big_TV in this example. within the MQTT settings of the flashed sonoff you need to define the topic and the /{identifier} in this instance will be Big_TV

So, you can chose your own structure for these topics but it’s in my opinion better to keep these following a sensible pattern. So you could chose something like /sonoff/switch/{identifier}

Within configuration.yaml you should copy ESPurana wiki page and use the settings provided but for the topic these need to match the sonoff you have flashed and defined with your MQTT broker details and topic. In this instance it would be the following;

state_topic: "/sonoff/switch/Big_TV/relay/0"
command_topic: "/sonoff/switch/Big_TV/relay/0/set"

Take care with your spelling and with the use of upper and lowercase as this is important and much match your devices hostname and the topic you set. Hopefully this helps someone that falls fowl of being a bit gun ho and tries to configure 5 at a time and gets muddled up.