Hi,
I’m running the latest version of HA, with MQTT discovery turned on. My ESPHome devices do get discovered and work as expected. however, the commands fired via HA to control the devices does not set the retain flag.
If I add the device manually, I have an option to explicitly set the retain flag. How can I set retain for the command messages for auto-discovered devices?
I am not sure if I quite understand the question but if this is a ‘command’ topic i.e. one used to request the associated ESPHome device change state then normally it should not be sent ‘retained’ to MQTT
As others have already explained, this falls into the category known as a “bad idea”.
However, if you really, really want to publish retained messages to a command topic then here are two possible approaches:
I’m unfamiliar with ESPHome but you should investigate and determine if it offers a programming option to publish retained messages for the command_topic. I’d start by reading this section: MQTT client component
You can manually publish additional information to an entity’s configuration topic. Read through the documentation for MQTT Discovery to learn how a construct the appropriate payload (including the retain: true option) to publish to a configuration topic.
ESP is configured via ESPHOME which sends a discovery message to HA via MQTT
HA registers the device.
3.You can control the device from HA via the command topic of the ESP
Now lets look at the scenario, lets assume there is an automation running which fires an MQTT command to turn on a light; Light turn on (YaY!) and send a status message back via MQTT
For some reason the ESP reboots, On reboot the ESP Does not turn on the light because the command topic does not have a retain flag.
Now if HA sends the command with retain=true; even if esp reboots it will honour the command when it comes online and post the status back.
Let me know if there is a better idea to recover from reboots.
Do not retain the command message. What happens if the state is changed by something other than Home Assistant?
Retain the state message. This will ensure home assistant stays in sync after it restarts.
To get the ESP to retain a switch or light state through reboots use the restore_mode: option DEFAULT_OFF or DEFAULT_ON. The off and on only refers to state the entity should assume if restore fails (I’ve never seen it fail, but handy to know my fridge will always be on if it does, and my lights off if it does).
But how can you get HA to allow state changes while device is offline? I want to change the state and the esp should wake up, check the state, handle it and go back to sleep as fast as possible
Thank you very much for your answer tom!
Can I configure my discovered entity to do this or do I have to create an entity manually in the config? I found this discussion where you said you should not retain the command topic (MQTT discovery with retain flag for command topic)
If the state of the device can only be controlled through the MQTT command topic this isn’t a concern and it will assume the last state ‘commanded’ when it reconnects to MQTT. However if it can be controlled other ways e.g. locally by a button or event then when it connects to MQTT it would revert to the last state commanded on MQTT, which might be incorrect.