Just to be clear, you deliberately configured these Items so they are not restored on startup. The default behavior is to always restoreOnStartup all Items for most persistence add-ons.
Here’s a rundown on how retention works.
When a publisher posts a message with the retained flag the MQTT broker will store that message. If there was already retained message on that topic, that existing message gets replaced.
When a client connects to the broker and subscribes to a topic, if there is a retained message on that topic, that message is delivered to the client.
It doesn’t matter if the client has already received that message on a previous connection. Every time the client reconnects it will get that retained message. QOS does not change this behavior.
A non-retained message does not replace a retained message. Even if there are 100 non-retained messages sent after a retained message, that retained message remains and will be delivered.
Here’s a rundown on how QOS works.
QOS 0: fire and forget; the broker sends the message and assumes it got there
QOS 1: broker sends the message and waits a certain amount of time for the subscriber to acknowledge receipt of the message, sending it again if no ack is received in that time
QOS 2: sets up a series of handshake messages between the subscriber and the broker to ensure delivery of the message only once
Notice how this exchange is only between the broker and the subscriber.
So if you have a retained message with QOS 2, when the subscriber connects to the broker it will receive that retained message only once through the QOS message exchange protocol. But the broker doesn’t keep track that it’s sent that message to that client before. On every connect for every client, every time, it will send the retained message.
I believe that Homie will only publish sensors and status with retained. It doesn’t make sense to publish commands to actuators as retained.
I don’t think that the retained, by itself, is the root cause of this problem. But based on what you described restoreOnStartup + retained messages + timing might play into it. restoreOnStartup only happens once during startup and the retained message is only sent once when OH connects so that wouldn’t explain the loop.
Do you have any rules that trigger on these Items?