Expire sensors after inactivity

There are quite a few use cases for expiring sensors after some (configurable) time.

I have two of them:

  1. I have a few temperature sensors which are not as reliable as I would like them to be. Currently, whenever they stop sending temperatures, Home Assistant just keeps showing their last temperature forever.
  2. I have a rain sensor reporting the total amount of rain it has seen so far. To get this into a binary sensor showing whether it’s raining right now, I use a binary trend sensor - it turns on, as soon as it starts raining, but then just stays on forever.

There are some workarounds for this problem, but they are all very hacky.

Would it be possible to add a field “expire_after” to the sensors which, when used, sets the state to UNKNOWN if there hasn’t been an update to this sensor for this amount of time?

At least the MQTT sensor already supports expire_after.