If I look at the startup messages in home assistant, I see:
Timer got out of sync. Resetting
21:38 core.py (ERROR)
Setup of tts is taking over 10 seconds.
21:38 bootstrap.py (WARNING)
When targeting Google Home, listening port has to be port 80
21:38 components/emulated_hue/init.py (WARNING)
Listen IP address not specified, auto-detected address is 192.168.192.79
21:38 components/emulated_hue/init.py (WARNING)
Setup of sensor is taking over 10 seconds.
21:37 bootstrap.py (WARNING)
Setup of mqtt is taking over 10 seconds.
21:37 bootstrap.py (WARNING)
When I click the switch toggle on the main states page, the toggle moves for a second and the reverts. Nothing happens with the switch.
I can control the switch directly through the tasmota web interface.
Your topics aren’t the same and they have to be exactly the same at both ends.
Your sonoff is publishing to ha/sonoff/STATE and HA is subscribing to stat/sonoff/POWER
and the sonoff is subscribing to ha/sonoff/cmnd/POWER and HA is publishing to cmnd/sonoff/POWER
The only issue with setting retain is that if you have any network connectivity issues, causing the sonoff to disconnect and reconnect, then when it reconnects it’s going to toggle according to the retained message.
I suppose this is OK for lights where it doesn’t really matter that much whether the light is on or off, but does matter for devices like garage doors, where you want to know for sure that the device is only going to react when you tell it to, and in the way you expect it to. For that reason in my view it’s better to leave retain off for command topics, though it is important for a device to publish state messages with the retain flag since then HASS won’t show the state as Unknown on startup. Just my 2 cents.
I don’t use sonoff devices or tasmota, but my understanding is that option in tasmota causes it to store its state and restore on restart. So e.g. if your light is on, and there is a power failure, when the power comes back on and the sonoff restarts, it will remember it was on and turn the light on.
What I’m talking about it a bit different. The retain: true option in HA causes HA to publish commands with the retain flag set. This means that once the message is at the broker, each subscribing client is going to get that message delivered to it on startup. Which means for example if you toggle the switch in HASS to ON, ON gets published with the retain flag set. Now, every time the Sonoff reconnects, it’s going to get the ON command and turn on. Which may be perfectly fine. Or, it may cause you to wonder why the device turns on even when HA is off, for example, or when you’ve manually turned it off via the switch.
Maybe I’m making a mountain out of a molehill. But to me its important to know that my devices are only going to change state or react when I intend them to.
I’m really derailing OP’s thread here but here’s a real world example where you don’t want to use retain:true.
Take an MQTT garage door opener. You can open the door through HA and also manually via a switch or remote control. You open the door with HA, which publishes OPEN to the command topic with the retain flag set. You go outside and do what you need to do in the garage, then close it via the pushbutton beside the door. No problem! Now you go to bed for the night. In the morning you wake up and the garage door is open and your lawnmower has been stolen. What happened? During the night there was a momentary wifi blip, which caused the garage door opener device to disconnect. When it reconnected to wifi and the MQTT broker, it subscribed to the command topic and received the retained message on that topic - OPEN. So it did what it was supposed to do, which is open the door when it receives that message.
Like I said, maybe not a big deal for lights, but to me it’s still better to not have retained messages on command topics.
Thinking about this again, and I don’t pretend to understand exactly what’s going on so this may be rubbish, will the switch not replace the ON that HA sent with an OFF when you press the button? Or am I confusing command and state topics?
These things are why I tell people having problems with MQTT NOT to try to understand it first and just go with whatever the guides say and why I myself avoided it for so long
You are confusing command and state topics. If what you are thinking was the case, there’d be no need for separate command and state topics. You’d just have one topic that reported status and accepted commands. But part of the reason for not doing this is that commands and states might be different (eg a lock that accepts LOCK and UNLOCK commands but returns the state of the device as LOCKED or UNLOCKED), and also to avoid a feedback loop where the device was constantly getting a command, then reporting state, which again became a command, causing the device to report its state…
Of course, the clearest way to avoid the whole problem of needing to replace the ON command message with OFF when the light turns off is just to not use retain when publishing to the command topic. Then the fact that ON was previously sent to the command topic won’t matter if the device disconnects and reconnects, since it will only receive messages that are published while it is connected.
Thought that might be the case, going to change mine over.
Ironically, I thought my outside lights had suffered from a dose of this in the early hours of this morning and spent ages checking logs to narrow down the cause. Turned out the missus had got up for a wee and decided she didn’t like the original light switch looking like it was on when the light was off, it’s now set as a toggle connected to the gpio, and had turned the light on with Alexa and then used the switch to turn it back off again. Oddly, two way switches never seem to bother her though…women are never happy!
I have spent more time than I care to admit researching and designing my system while trying to answer the question “What will my wife think/say about this?”. Also I have small kids so telling them to leave a light switch in the “on” position so that the light will turn on when you walk past the motion sensor… unfortunately a lot of smart tech just isn’t at that intuitive stage yet.
And is the biggest reason mugs pay through the nose for Hue stuff i guess, same principle as iPhones, may not be the cleverest but just works without having to think about it…or boring and expensive depending on your viewpoint.