Could MQTT -> QoS 1/2 or retain flag fix my problem? / ESP

Hiho folks & friends,
after expanding IoT devices @ my home more and more I notice a problem with my switches/relays:
They trun on and ignore the mqtt-command to turn off. This happens very seldom but its annoying if it happens before going to sleep. All my relays are soldered and programmed by myself (ESP8266) I guess it could be a bug somewhere deep in the code that I could search forever. (beside the work of disassemble stuff for this case).

Long story short: I guess at some point the ESP crashes and restarts and the MQTT message to switch the relay off gets lost somehow. I was thinking about if MQTT QoS 1 could fix this ? I am not sure if it works just to set the node red QoS option to 1 or do I have to implement more like sending back an answer or so ?

If it is ignoring repeated requests to turn off then it is not a matter of Retained flag or QoS but either the network connection has dropped out or there is a bug somewhere. You should be able to use the LWT message to determine whether the comms has dropped out. If it isn't that then you could add some debug to the device so that every time it gets a command it echoes it back via another topic then you will see whether it is getting through. When it is that situation how do you make it recover?

Thanx for your fast answer.
The idea with sending debug messages to another topic sounds nice.
Its very random and I do only notice it in one of fity times or so. I see the light stays turned on but the mqtt message to turn it off was send correctly (with QoS 0).
I dont need to recover or do not need to do something to make it work again. - The next message will then work correct again.

Ok, I don't think that is quite what you said the first time, which was

The first thing then is to set the QoS to 1. That guarantees the message will be delivered (it may be delivered more than once, but that is not an issue here I think). Next is to set the Retain flag. Then if the network drops out or the device reboots it will be sent the most recent value on startup. In addition, since you have written your own code, I would default the output to Off so that on boot it will start up in that state and require an command via MQTT to switch it on again, which will happen if it should be on, because of the Retain flag.
Also it is worthwhile configuring and monitoring the LWT topic so that you will know if the connection to the device fails.

ok my mistake. My first post was a bit unclear :slight_smile:
I did implement "QoS 1" to the ESP code and to node red mqtt nodes now.
The retain flag did mess things up for me in the past and resulted to endless switch commands. - So I am a bit careful with "retain = true"
Will the retained commands be deleted after send once ? - In my memory they didn´t or could this happen if the QoS is set wrong at the ESP code ?

EDIT: Just QoS 1 didn´t fixed it. Now I use retain flag.

Definitely not, if that happened and the device restarted again then it would not get the value the second time. If you had an mqtt loop then you just need to sort that. Often the solution is to clear the message pass through option in the switch node, if one is involved.