I use MQTT discovery with HA, all work fine, but after restart HA, all sensors disappear (sensor and binary_sensor). I have to restart RSI gateway to “re-register” all sensors.
Diagram: rsi_gateway -> mosquitto -> HA
When I restart rsi gateway, I can see my sensors (through MQTT discovery):
After HA restart:
I tried to add retain=True in first state MQTT message, but it not works…
Set a sensor retain message when publishing to the broker? Then HA will pick it up immediately on restart. That’s what I do in Tasmota with some temp and pix sensors.
Thanks for your quick answer !
how do you do that ? with retain=True for each MQTT publish (not only for first state) ? Or into HA configuration directly ?
Yes not in ha but with the publish command. I do this with a shell script that publishes events from a totally unrelated Pi. Otherwise the disappear when HA restarts.
Hi @Burningstone
Yes I already tried this method.
I send retain=True on the first MQTT state (default state), but not for the config.
Maybe I have to send retain=True when I publish de config no ? Or for config and state …
++
Well, according to the standard “retain” means that the last message sent on a topic is retained. So send all your messages with “retain” and HA picks up the entities and restores the state. Actually, the state is even maintained while HA is not reachable.
In essence, it is more complicated, as the QoS flag and clean session also play a role. With QoS = 0 (at most once delivery) messages may be deleted by the broker. Brokers typically have settings to persist QoS 0 messages.
@Mickaelh51, I know it’s been a long time since you posed the above question here in the community. I would rather have the MQTT sensor defined in the configuration.yaml in HA than loose them after each restart.