ThomDietrich / miflora-mqtt-daemon

Linux service to collect and transfer Xiaomi Mi Flora plant sensor data via MQTT to your smart home system, with cluster support 🌱🌼πŸ₯€πŸ‘🌳
MIT License
606 stars 141 forks source link

Added retain=True for homeassistant #149

Closed apauly closed 1 year ago

apauly commented 2 years ago

I've added the retain flag for publications for home assistant. This will make sure that the latest values are present after a reboot when HA initially subscribes to the topics.

Please let me know if there's anything missing

ThomDietrich commented 2 years ago

Hey! Thanks for your addition. The obvious reason why it's not implemented this way is the chance of old outdated values on the broker. Not it has been some time since I last worked with the code, I know that there are expiry settings and lwt messages... How does the setup work for inactive sensors?

apauly commented 2 years ago

For my personal use case, the only way a sensor could get inactive is by running out of battery - so I'd rather have the latest value than no value. But I see that there might be other use cases where this might be different.

What do you think about a configurable option so that the default behaviour would not change?

ThomDietrich commented 2 years ago

Hey! Had another look at this. I agree that the current behavior is annoying, the new behavior would be too carefree. I would like numbers right after restart too, but super old readings should not be shown indefinitely when one sensor was eaten by the dog.

Maybe the solution is simple: Could you try adding an expiry time the config of all sensors? You can choose a high one, just so we are safeguarded from values that are super old https://www.home-assistant.io/integrations/sensor.mqtt/#expire_after Be sure to exclude the battery sensor ;)

ThomDietrich commented 1 year ago

Did you test the alternative approach?

iia commented 1 year ago

@ThomDietrich Most probably I'm missing some details here but why having retain=true set, for example for wirenboard-mqtt and mqtt-smarthome is not a problem but for Home Assistant it is?

Retain is a MQTT feature which would behave the same for wirenboard and mqtt-smarthome. It is not Home Assistant specific. I don't see how it is going to be "too carefree" for Home Assistant.

ThomDietrich commented 1 year ago

My concern is with outdated sensor reading retained forever. However, with lwt and the expire_after setting added recently, the retain flag should be a meaningful improvement without the issue of outdated sensors. All good!