Ok, so this might be obvious but it wasn’t to me and it took me a while to work out what was going wrong with my config. So I offer this with good intentions, no need to shout if I should have known (or been able to work it out quicker!).
If you have an automation with an MQTT trigger:
- platform: mqtt
and the topic has a retained message then that automation will be triggered at HA start up.
Something else you should know is that you can tag your post with MQTT rather than putting it in the title.
I’ve updated it
Not obvious. I appreciate seeing little hints and tips like this.
It isn’t limited to automations.
The whole point of
retain: true is you’re instructing the MQTT Broker to retain a copy of the topic’s value. In other words, please save a copy for future reference.
retain: false, the MQTT Broker does not save a copy for future use. When a device (like Home Assistant) connects to the MQTT Broker, it’s not informed of things that have happened in the past.
retain: true like meeting notes. You missed a meeting but, later, if you read the meeting’s notes you know what happened. With
retain: false, there are no meeting notes. If you missed the meeting, you don’t know what was discussed.
retain: true is best used for state_topics. For example, when Home Assistant restarts, it connects to the MQTT Broker and subscribes to various topics. For topics that are retained, HASS gets their last recorded value. Now it knows which lights are on or off, etc.
retain: false is best used for command_topics. Otherwise, upon restart with
retain: true, Home Assistant will get the last recorded value of commands to open garage doors, turn on pumps, activate scripts and automations, etc. Most of the time, this is not the behavior you want so command_topics should be published with
retain: false. However, there are rare occasions when you do want actions to be performed the moment HASS or an MQTT device (re)connect to the Broker. The key word here is ‘rare’.