How to configure "auto discovered" MQTT switches?

My Sonoff S20 (Tasmota) was discovered correctly and works, but after every restart of HA, the state of the switch is “off”, which is not correct.
Only if I restart the Sonoff, HA detects the state correctly.
So, where can I configure this switch, to parse the state from json output?
thanks in advance

Configure your sonoff to use the retain flag in it’s MQTT setup.

Configure your sonoff to use the retain flag in it’s MQTT setup.

Seems, its set already…
sonoff-s20-retained

I have no idea what your picture represents.

That the Retain flag is already used?
However, is there no chance to configure the switch in HA manually?

I still have no idea what that picture is of.

It seems that the retain is set on the discovery topic, but you need to look at the status topic I think.

Ok, I have now set the MQTT messages to every 10 sec.
So why doesn’t the switch update, at least after the “state” is sent?
state message:
13:43:19 MQT: tele/sonoff-S20/STATE = {"Time":"2018-10-24T13:43:18","Uptime":"0T00:22:13","Vcc":3.174,"POWER":"ON","Wifi":{"AP":1,"SSId":"secret","BSSId":"D8:50:E6:B3:46:D8","Channel":1,"RSSI":100}}

You are now asking a completely different question, which should be in a new thread.

I don’t think so.
The question was how to configure the auto-discovered switch?
That question wasn’t answered (and no, “retain” does NOT solve the problem).
If I would be able to configure the switch, the problem, (HA does not update switch state), wouldn’t probably exist.

So if somebody could please answer my question: Can this switch be configured from inside HA and if yes, where?
Thanks.

What are the command or steps to use retain option in MQTT setup? I also have similar issue

The point about MQTT discovery is that components do not need changes to your configuration, they are automatically configured by the MQTT messages sent to them when HA starts.

The state will be updated when the message that is configured as state is received with the appropriate payload. If the state is not updated by the messages you posted, then they don’t match the configuration sent. You will have to post the configuration messages before anyone can resolve why.

You will have to post the configuration messages before anyone can resolve why.

15:58:54 MQT: verbunden
15:58:54 MQT: tele/sonoff-S20/LWT = online (beibehalten)
15:58:54 MQT: cmnd/sonoff-S20/POWER = 
15:58:54 MQT: tele/sonoff-S20/INFO1 = {"Module":"Sonoff S2X","Version":"6.2.1.16","FallbackTopic":"DVES_131D6B","GroupTopic":"sonoffs"}
15:58:54 MQT: tele/sonoff-S20/INFO2 = {"WebServerMode":"Admin","Hostname":"sonoff-S20-7531","IPAddress":"192.168.2.66"}
15:58:54 MQT: tele/sonoff-S20/INFO3 = {"RestartReason":"Software/System restart"}
15:58:54 MQT: stat/sonoff-S20/RESULT = {"POWER":"ON"}
15:58:54 MQT: stat/sonoff-S20/POWER = ON (beibehalten)
15:58:54 MQT: homeassistant/light/sonoff-S20_1/config =  (beibehalten)
15:58:54 MQT: homeassistant/switch/sonoff-S20_1/config = {"name":"Sonoff-S20","command_topic":"cmnd/sonoff-S20/POWER","state_topic":"stat/sonoff-S20/RESULT","value_template":"{{value_json.POWER}}","payload_off":"OFF","payload_on":"ON","availability_topic":"tele/sonoff-S20/LWT","payload_available":"online","payload_not_available":"offline"} (beibehalten)
15:58:54 MQT: homeassistant/light/sonoff-S20_2/config =  (beibehalten)
15:58:54 MQT: homeassistant/switch/sonoff-S20_2/config =  (beibehalten)
15:58:54 MQT: homeassistant/light/sonoff-S20_3/config =  (beibehalten)
15:58:54 MQT: homeassistant/switch/sonoff-S20_3/config =  (beibehalten)
15:58:54 MQT: homeassistant/light/sonoff-S20_4/config =  (beibehalten)
15:58:54 MQT: homeassistant/switch/sonoff-S20_4/config =  (beibehalten)
15:58:54 MQT: homeassistant/light/sonoff-S20_5/config =  (beibehalten)
15:58:54 MQT: homeassistant/switch/sonoff-S20_5/config =  (beibehalten)
15:58:55 MQT: homeassistant/light/sonoff-S20_6/config =  (beibehalten)
15:58:55 MQT: homeassistant/switch/sonoff-S20_6/config =  (beibehalten)
15:58:55 MQT: homeassistant/light/sonoff-S20_7/config =  (beibehalten)
15:58:55 MQT: homeassistant/switch/sonoff-S20_7/config =  (beibehalten)
15:58:55 MQT: homeassistant/light/sonoff-S20_8/config =  (beibehalten)
15:58:55 MQT: homeassistant/switch/sonoff-S20_8/config =  (beibehalten)
15:59:03 MQT: tele/sonoff-S20/STATE = {"Time":"2018-10-24T15:59:03","Uptime":"0T00:00:13","Vcc":3.174,"POWER":"ON","Wifi":{"AP":1,"SSId":"ASUS_chatkaew2","BSSId":"D8:50:E6:B3:46:D8","Channel":1,"RSSI":100}}

The state topic does not match the topic of the message being sent, so the state of the component does not update. I don’t use tasmota, but from what I gather from other posts, it sends a different topic when the switch state changes. Does the state_topic match that message?

Yes, the “state_topic” matches when the switch state changes.

16:49:49 MQT: stat/sonoff-S20/RESULT = {"POWER":"OFF"}
16:49:49 MQT: stat/sonoff-S20/POWER = OFF (beibehalten)

16:50:27 MQT: stat/sonoff-S20/RESULT = {"POWER":"ON"}
16:50:27 MQT: stat/sonoff-S20/POWER = ON (beibehalten)

That is interesting, in that it seems to send two messages. The first one matches the configuration message, but is not retained (I had to look up beibehalten in a german dictionary) and another message that is retained.

As was stated before, setting the retain flag for a topic is the normal solution for getting the state sent to HA when HA starts. The message is stored in the broker’s database, and sent whenever a client subscribes to that topic.

As I said, I don’t have tasmota, so I don’t know the best solution. You could either set the retain flag on the topic stat/sonoff-S20/RESULT or change the configuration message such that the HA sensor looks for the stat/sonoff-S20/POWER message. Note that if you use the second option, you need to remove the value_template part of the configuration, as the payload of that message is not json.

I would prefer the second option, but where is the configuration file for these discovered devices?

As I stated before, configuration for discovered devices is done in the configuration message, not in any HA files. You can (presumably) configure this message using the tasmota interface.

Since all “retain flags” didn’t work, at least I’ve set up a “workaround” via “automations”, that publishes the needed state message at HA startup
But of course, this is definetely not the purpose of an “auto discovery” feature…
thanks for your time anyway!

1 Like

For manual configuration you follow the MQTT switch component.