MQTT configuration with a SONOFF relay and using the Espurna Firmware

The Sonoff basic is a neat device but it becomes an AWESOME device when you blow out it’s firmware and install the Espurna firmware on it. This is not How to do that, this is how to get it working with your HASS install once you do have it flashed and ready to go.

FIRST, in the Espurna firmware on the SONOFF Configure the MQTT settings to point at your broker, if using the internal HASS broker use your web UI password with the user of homeassistant.

Now in your configuration.yaml add the following.

mqtt: # this uses the built in MQTT broker

light:
  - platform: mqtt
    command_topic: "/office/switch/CANS/relay/0"
    state_topic: "/office/switch/CANS/relay/0"
    payload_on: "1"
    payload_off: "0"
    optimistic: false
    name: "Office CANS"

Those are for my office cans. in the firmware I have the MQTT topis set as /office/switch/CANS The espurna firmware uses a 1 and a 0 for payload and the Sonoff Basic have only one relay so we will only choose to poke at relay/0 and that is our state so we get instant notification if the state changes.

Hope this get’s you moving along with getting Sonoff relays with espurna firmware on them working in HASS. Some other topics here about them were close but not 100% accurate as they do not tell you to use the /0 for the relay in the Sonoff basic. I had to search a lot of different places to finally find that last bit of information.

I’m having a little trouble getting the mqtt part of ha up and running. I am running hassio and have mqtt setup below…

  broker: 192.168.1.100
  port: 1883
  username: ha
  password: ha

I have setup a switch with your config as follows…

  - platform: mqtt
    command_topic: "/office/switch/CANS/relay/0"
    state_topic: "/office/switch/CANS/relay/0"
    payload_on: "1"
    payload_off: "0"
    optimistic: false
    name: "Office CANS"

Any ideas as to what I am doing wrong?

Have you set up MQTT lens or such so you can see what if anything is happening?

If using the hass.io add-on your config only needs to be…

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

I’m using TASMOTA so the switches are configured slightly differently to that example so can’t comment on whether that’s still right, though there’s every chance it’s well out of date like most tutorials five minutes after they were written!

Had a quick Google, try following the official guide from here instead…

https://bitbucket.org/xoseperez/espurna/wiki/HomeAssistant.md

Which gives this as an example…

switch:
  - platform: mqtt
    name: "Test Switch"
    state_topic: "/test/switch/D1MINI/relay/0"
    command_topic: "/test/switch/D1MINI/relay/0/set"
    payload_on: 1
    payload_off: 0
    optimistic: false
    qos: 0
    retain: true

Yes, I had tried the switch example before going to the CANS config and was unsuccessful. I am running HASSIO and had mosquitto installed. After installing MQTT lens I tried to connect with the ha login you specified above and it would connect and disconnect immediately. So I uninstalled mosquitto and restarted now I get no connection so I am going to reinstall and begin again from there. Thanks for the help.

I’ve get a solid MQTT connection now through MQTT lens. I unplugged the switch and it has not come up since I plugged it back in, so I’m thinking that I have a flaky switch. Thanks again.

1 Like

Got mosquitto working but the 1.10.0 was not working properly for some reason. The switch wouldn’t reboot until I unplugged it, removed the mains from the input, put them back and plugged the switch back in. Uploaded Tasmota and it is working great.

1 Like

anyone can help with MQTT controll to Espurna firmware on sonoff pow?
i cannot get it to work not manually via PUB command and not from HA.

im trying with pub message via mosquitto: sudo mosquitto_pub -t /sonoff1/relay/0/ -m /sonoff1/relay/0/set/1
without any luck…what am i missing?(edited)

my configyraion:

  • platform: mqtt
    name: “sonoff1”
    state_topic: “/sonoff1/relay/0”
    command_topic: “/sonoff1/relay/0/set”
    payload_on: “1”
    payload_off: “0”
    qos: 0
    optimistic: false
    retain: false

I haven’t used that firmware, but this command doesn’t look correct. You shouldn’t need sudo, the topic has an extra trailing ‘/’ and the payload should be 0 or 1. So try

mosquitto_pub -t /sonoff1/relay/0 -m 1

which should turn the switch. Alternatively, you may need the trailing / if your firmware needs it, just update your configuration file if it does - MQTT is unforgiving, strings have to match exactly

This is also assuming that you have mosquitto (or a broker that doesn’t need a password) set up on the local computer to the command. If not, you may also need parameters for the
host, username and password. See mosquitto_pub --help for these.

hi thanks for help!
you are right, the mqtt command wasnt right, and the payload need to be without the " " on the 1/0,