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
610 stars 140 forks source link

Last Message timestamp #162

Closed the-jeffski closed 1 year ago

the-jeffski commented 1 year ago

Any thoughts on adding in a last message timestamp to the data set for home assistant? I get patches where I find the connection has dropped which I only spot hours later when the sensor stops showing. (have started running in docker which has made it a bit more robust and will send a update for the dockefile soon once I am comfortable its working).

I have been using the temperature value to pick up on a update time to show on a card but it would be ideal to get a timestamp for the overall message. In the code there is a timestamp set in the wirenboard loop so assume we can just add in the relevant value in the data set for homeassistant?

ThomDietrich commented 1 year ago

Hey, sounds reasonable to me. Could you please check the Home Assistant "MQTT Discovery" documentation and/or community forum for most conforming way to do this, then propose a PR? Thanks!

the-jeffski commented 1 year ago

So, I dug into this and creating the extra element in MQTT is simple enough however as I use the plant component in HA, this also needs to change to understand the new value and to write it to a sensor. All got to be too many dependencies so I have a half way house now of a template to check for any value change in the data coming back and use that. Not 100% perfect but it seems to generally report every update. I'll add an example template here for anyone else who searches for this:

  epipremnum_last_updated:
      friendly_name: "Epipremnum Last Updated"
      unit_of_measurement: timestamp
      icon_template: mdi:clock
      value_template: >
        {% set sensors = [
          states.sensor.epipremnum_battery,
          states.sensor.epipremnum_light,
          states.sensor.epipremnum_moisture,
          states.sensor.epipremnum_conductivity,
          states.sensor.epipremnum_temperature]
          |reject('none')|list %}
        {% set target = sensors | map(attribute='last_updated') | max %}
        {% set target = sensors | selectattr('last_updated', 'eq', target) | list | first %}
        {{ target.last_updated }} 

Closing this off for now.