allangood / rtlamr2mqtt

Docker container to send rtlamr readings to a mqtt broker
MIT License
292 stars 47 forks source link

Add support for the MQTT integration's `expire_after` setting #200

Open airdrummingfool opened 1 year ago

airdrummingfool commented 1 year ago

First off, thanks for this great project! I have been using it for months to get usage from my gas and water meters into HA.

Recently I discovered that I was missing a couple weeks of data for my sensors - rtlamr2mqtt had stopped collecting and sending sensor information to MQTT. It apparently didn't lose its MQTT connection, because it didn't publish the LWT message. I didn't see any obvious issues and a restart solved it, so I focused on finding a way to alert and recover from this if it happened again.

It turns out, Home Assistant's MQTT integration has a setting called expire_after which is exactly what I am looking for:

If set, it defines the number of seconds after the sensor’s state expires, if it’s not updated. After expiry, the sensor’s state becomes unavailable. Default the sensors state never expires.

This PR doesn't change the default behavior, but if expire_after is provided for a meter, it will pass that option into the MQTT discovery payload, which HA will use to cause the sensor to become unavailable after expire_after seconds have passed without a sensor reading. This should thus be set to something larger than the expected period between sensor readings (e.g. larger than sleep_for).

Thanks again!