I disagree. Retained messages can lead to all sort of unwanted behaviours.
I use a workaround. Although not perfect, I restored all the MQTT items. They are all is a special group and I run the following rule after start up:
rule "Update MQTT2 Items"
when
System started //or
// Item TestSwitch received command ON
then
createTimer(now.plusSeconds(120), [ | // you may need to experiment to find the optimum wait time
MQTTv2.members.filter[ i | i.state == UNDEF ].forEach[ ii |
ii.postUpdate(ii.previousState(false,"mapdb").state.toString)
Thread::sleep(150)
]
])
end
Basically, the binding initialises every item as UNDEF, as it should, the problem is that the start up order is not fixed and the restoreOnStartup normally occurs before the MQTT binding starts so your items revert to UNDEF.
So, 2 mins in the startup, to give time for the MQTT binding to start, I filter all the items belonging to the group MQTTv2, if they have an UNDEF state then I restore them using the .previousState persistence method. The little sleep is to give time for the DB access and not skip any items.