Cannot get Sonoff basic switch working in Home Assistant via MQTT

Folks,
I’ve got a problem defining my sonoff basic switch in home assistant.
In specific, I cannot find the right MQTT settings to get it working.
I got Hassbian 0.69.1 (not hass.io) running on a Rasberry Pi 3.
I want to use the embedded MQTT server in Home Assistant.
Sonoff switch has Tasmota firmware 5.12 running.

The MQTT settings of the SONOFF switch are:

Host: IP address from my Raspberry Pi
Port: predefined, not changed
Client: predefined
Benutzer = User: the gui username of my homeassistant installation
Passwort = password: the gui password of my homeassistant installation
topic: changed to a unique name for my sonoff switch
full topic: predefined or default, not changed

Question 1:
is “homeassistant” the correct username?
When I login into the web gui, I only need to enter the password without username.

The corresponding code in my configuration.yaml:

mqtt:

switch:
– platform: mqtt
name: “GH-Lampe-233”
state_topic: “stat/sonoff_gh/POWER”
command_topic: “cmnd/sonoff_gh/POWER”
availability_topic: “tele/sonoff_gh/LWT”

A look into the console of the sonoff switch shows this kind of messages:

08:53:17 RSL: tele/sonoff_gh/STATE = {“Time”:“2018.05.17 08:53:17”,“Uptime”:“1 18:00:37”,“Vcc”:3.427,“POWER”:“OFF”,“Wifi”:{“AP”:1,“SSId”:“FRITZ!Box 7490”,“RSSI”:100,“APMac”:“22:68:11:3C:54:32”}}
08:58:17 RSL: tele/sonoff_gh/STATE = {“Time”:“2018.05.17 08:58:17”,“Uptime”:“1 18:05:37”,“Vcc”:3.427,“POWER”:“OFF”,“Wifi”:{“AP”:1,“SSId”:“FRITZ!Box 7490”,“RSSI”:100,“APMac”:“22:68:11:3C:54:32”}}

Question 2:
Are the path settings correct?
In some tutorials, there can other path settings be found like home/sonoff/…

I will be very grateful if anyone could give me a hand in solving the problem. Thank you!

Regards,
Martin

Look like you missing the the MQTT bit

mqtt:
  broker: localhost
  discovery: true
  discovery_prefix: homeassistant

must install it first

The default settings for the embedded broker are here

As you can see, homeassistant is the correct username.

The messages show that the sonoff has connected to the broker correctly, and is sending state messages with topic tele/sonoff_gh/STATE.

Ok, I set…

mqtt:
embedded:
broker: 192.168.178.228
discovery: true
discovery_prefix: homeassistant

and got no error message.

But the sonoff switch is shown as “not available” in the dashboard:
homeassistant-gui

The working “SZ-Fernsehlampe” is connected with a AVM DECT200 switch.
The other switches are other sonoffs, I try to implement after one of them is working correctly.

Do you want to use the embedded broker or an external broker?

did you try broker: localhost

@gpbenton: I want to use the embedded broker.

@myle: I also tried “localhost” instead of the IP address - same result

If you want to use the embedded broker, you just need mqtt: in your configuration, as you had it originally. The configuration suggested by @myle requires you to set up an external broker.

At that point, the sonoff correctly connected to the broker. The only thing left was to determine the topics to be sent.

Can you give me a hand please?

I don’t have a sonoff, but the documentation is here

That says

The relay can be controlled with cmnd/sonoff/power on, cmnd/sonoff/power off

Interpreting this to mean the default value for the middle word, means your command_topic should be

command_topic: “cmnd/sonoff_gh/power”

The state topic seems to match, so I would leave that, and see if using that configuration at least causes HA to turn on and off the switch.

I think your missing the “payload”. I have a few Sonoff basics that I set up following this video by BRUH Automation. Here’s how my configuration looks for the sonoff.

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

I also notice you have “POWER” capitalized in both in both the command and state topics. I don’t fully understand mqtt enough to know if that matters or if just to help tell them apart? But the example I followed did have one in all caps and the other all lower.

1 Like

It is essential. Topics are case sensitive. But you are correct about the payload - I should have spotted that.

Alright…
First I changed the topic back to default = sonoff.
In the configuration.yaml I already had the payload etc etc in - dont ask me, why I didnt typed and posted here the complete config, sorry, my mistake.
After some trial boot ups, I had success, but realized, that it is not a must, that the default topic is “sonoff”.
It is possible and needed to give here a “unique” name, also in case of more switches.
Otherwise, all of the sonoff switches will turn on with one “switch” in the homeassistant dashboard.
In my case it is now “sonoff_gh” and “sonoff_wz” (two separate sonoff switches).

Now I have this in my configuration.yaml:

switch:
– platform: mqtt
name: “GH-Lampe-233”
state_topic: “stat/sonoff_gh/power”
command_topic: “cmnd/sonoff_gh/POWER”
// availability_topic: “tele/sonoff_gh/STATE”
payload_on: “ON”
payload_off: “OFF”
// payload_available: “Online”
// payload_not_available: “Offline”
qos: 1
retain: true

– platform: mqtt
name: “WZ-Fernsehlampe-232”
state_topic: “stat/sonoff_wz/power”
command_topic: “cmnd/sonoff_wz/POWER”
// availability_topic: “tele/sonoff_wz/STATE”
payload_on: “ON”
payload_off: “OFF”
// payload_available: “Online”
// payload_not_available: “Offline”
qos: 1
retain: true

// means, that the line is commented out with #.

With that, the dashboard shows my two sonoff switches and I can turn it on.
Only on!
Not out again :wink:
Also, when I commented in the availability_topic, hassbian is not starting after reboot.

Alright, satisfied right now.
The next step is to figure out, why I can only turn the switches on, not out.
The dashboard switch shows the animation for turning on, followed by the animation for turning off (switch button switches back to off). But the lamps, which are connected to the sonoffs, keep powered on.

Any further ideas?

You need to post your yaml snippets using the instructions in the blue box at the top of the post.

This means that the state message sent by the sonoff has not been decoded correctly by HA, so HA assumes it is still off. I notice in your configuration that you have changed the state_topic: to lower case. I believe that should be upper case, to match the configuration supplied by @troy.

It is interesting that the command_topic: works as upper case, but it could be the sonoff converts it. But if it works like that, leave it be.

@gpbenton You were right. Converting state_topic to upper case did the trick.
Now, the state will not change back to off, when I turn it on.

In addition, I put my Sonoff-Dual also in my config, same style, but with POWER1 / POWER2 for the two relays of the Sonoff-Dual.

Thank you all for your help!

1 Like