I use this in my yaml for state:
state_topic: “/Sonoff_2/#”
but this it now shown in the mosquitto_sub list.
The “#” sign in MQTT topics indicates ‘match any set of characters’, so your HA switch will changes state whenever it gets a message beginning with these characters ( and matches the payload_on:
parameter), which is probably not what you want.
You need to set the state_topic:
to the exact topic that your client switch is sending - such as /Sonoff_2/relais/STATE
So instead of “/Sonoff_2/#” in the setup of ESP easy I have to write "/Sonoff_2/relais/STATE and then the same in my yaml?
Yes, just make sure you include something that specifically identifies that Sonoff (maybe that it is the 2) so that you can have different topics for different switches.
I’m not sure what happens if you publish something with a ‘#’ in the topic, but its definitely something to avoid, just to stop confusion.
I get the following when I connect:
$mosquitto_sub -u tom -P xxxxxxxx -t "#" -v
cmnd/sonoff/power ON
tele/sonoff/LWT Online
Where do I enter this command?
Thanks all for your help!
Tom
Hi @eBoon, just type it in your browser, or browse to ip of the sonoff, go to ‘Console’ and type the Command PowerRetain 1
After monkey_of_hill’s tip, i found this about the commands.
THANK YOU! That did it. Is that something I will need to do everytime I power up the Sonoff? Guess I should probably modify the Sonoff code to set that by default.
Thanks again!
Tom
Found it…
user_config.h (MQTT_POWER_RETAIN) :
#define MQTT_BUTTON_RETAIN 0 // [ButtonRetain] Button may send retain flag (0 = off, 1 = on)
#define MQTT_POWER_RETAIN 1 // [PowerRetain] Power status message may send retain flag (0 = off, 1 = on)
#define MQTT_SWITCH_RETAIN 0 // [SwitchRetain] Switch may send retain flag (0 = off, 1 = on)
Hello, easier to use your web browser to connect to your sonoff module URL > " http://sonoff.ip.addrs ", then click on “Console” button, and enter the command " PowerRetain 1 " …
Yes you are absolutely correct. I was worried that the setting would not be retained across power cycles - but have confirmed that it is!
Yes, and while you are in the console, if you send a HASS command to your sonoff modules, you will see : MQTT: stat/sonoff1/POWER = ON (retained) ! ain’t that cute !
Thanks for this
I’m surprised this wasn’t enabled by default or was it?
@jeanrocco
Thank you very much for this tip. This has been nagging me since I installed the Sonoff and have tried other useless approaches.
I have enabled PowerRetain via the Console in the web interface of a sonoff switch, however it still doesn’t retain when HA is restarted. Is this because I’m using the internal MQTT broker?
Using an mqtt client on my phone, I can see the Sonoff stat messages say “not retained”.
Yes, the built-in broker does not seem to honor the retain flag.
Just to add, it works perfectly with the hassio add on though if you’re inclined to change
I installed mosquitto last night and connected my sonoffs and HA to it. Haven’t had a chance to properly test it but in my android client the messages still show up as not retained.
EDIT: Having messed about for a bit, it now works as expected. If a device is on when HA restarts then it will be know it’s on. So migrating to mosquitto solved the problem for me.
That’s always been ‘the cure’ but a guy came up with a fix using the built in broker the other day. Can’t comment on it myself as I use hassio though.
PowerRetain 1 seems to work perfect, on every HA restart it will keep last button states.
The problem now to solve is that if you do a hassio host reboot, the son off is not able to conect to mqtt host, you need to restart sonoff for all go back to normal
Anyway to solve this?
Hi I have the same problem but I notice that the problem come out when I reboot mqtt or the sonoff loose connection…