A way to handle this. Not always a better way. Not always even a desirable way.
We can go back and forth on this for many posts. But I already know you will not convinced me. I sent months using retain=true for everything. It caused far more problems then it solved in my use cases.
I’m fully aware of LWT and use it regularly. I’m not talking about detecting a device has failed, though LWT implies that the only failure that occurs is that the device loses connection with the broker.
I have not and will never argue that retain=false shouldn’t be used. But saying it is there only valid option is simply not true.
Yes, but your situation isn’t there only use of MQTT. There are tons of use cases and systems models where it makes no sense.
But in all of those cases, from OH’s perspective, the state of the Item may properly be undefined (i.e. NULL) until a new value is posted.
Motion sensors, push button actuators, stuff like that.
When the only tool you have is a hammer, all problems look like nails.
That isn’t the goal. And as I said, LWT only indicates loss of connection to the broker for a given time. The goal is to let the state of Items remain undefined until a new value is posted. retain=false makes that impossible without a whole lot of extra work.
I’ll close with a few concrete examples from my setup.
-
I chose to model my garage door opener to work just like the physical remote. When the controller received a message it simulates a button press on the door (it’s physically wired to the wall button with a relay). Receipt of the ON message is interpreted as the command to press the button. There is no other message. retain=false would mean the button would be pressed every time there is a reconnection to the broker.
-
I control me house fan based on the temperature readings from 4 different sensors. I do not want to turn the fan on based on any reading more than 10 minutes old. If a sensor hasn’t reported for over ten minutes that Item should be NULL because as far as the algorithm is concerned that is the indicator that the value shouldn’t be used. Oh, and not all of the sensors are MQTT and using retain=false let’s the MQTT sensors behave the same as the Nest and Zwave sensors which also remain NULL until they’re is a new reading. Oh, and if a LWT is receive from over of them then all the Items that get proposed from that device are set to NULL as well.
-
This is one I’ve seen but is not mine. Let’s say I have a mix of zwave and DIY motion sensors. The Zwave ones just report ON when motion is detected, never OFF after a timeout. In order to have the two sets of motion sensors behave the same, retain=false has to be used. Otherwise every time OH connects to the broker it will think the MQTT morning sensors went off.
In all three cases above could I use retain=true? Absolutely. Is it more work? Absolutely. Does retain=true actually provide any value in these use cases? No, it just adds more work.
Switch item=aGarageOpener1 icon="garagedoorclosed" mappings=[ON=Open] visibility=[vGarageOpener1 == CLOSED]
Switch item=aGarageOpener1 icon="garagedooropen" mappings=[ON=Close] visibility=[vGarageOpener1 == OPEN]
Switch item=aGarageOpener2 icon="garagedoorclosed" mappings=[ON=Open] visibility=[vGarageOpener2 == CLOSED]
Switch item=aGarageOpener2 icon="garagedooropen" mappings=[ON=Close] visibility=[vGarageOpener2 == OPEN]
An alternative implementation for Timers using the Expire binding. I can’t remember if the Motion Sensor timer DP includes an example.
Use the Expire binding to set the Item to NULL or some other value to indicate an error.
There is also the following DP which applies that concept to generically since most technologies OH interfaces with do not support LWT type capabilities.
I need to rewrite this DP so all questions if you use it and have problems. If all you have is MQTT, the logging becomes way easier because of the LWT.