Entity: remember last state for an hour and then go to unavailable if update() fails for a long period

I would like to implement that the last successful state is reported for an hour even if the last update() fails. However after that timeout the state should go to “unavailable”. This behavior would be for a flower care sensor (miflora) where the values don’t change rapidly, so it would be OK if not every update() query is successful. Are there other sensors which have implemented a similar logic?

I’m thinking about:

  • Is it possible to update last_update only if the update() is successful? So, I could check in avaiable():
    if self.last_update < now() - last_successful_timeout: return False
  • Do I have to implement my own last_successful_update variable? Is there a recommended way - for example using device_state_attributes() ?

Thanks!

I’d suggest taking a look at this:

@jimz011: Thanks, but until now I don’t figure out how to store an old value in a template sensor. As far as I can see I can only return a value / attributes. I don’t see a way to remember the time when available() is going to false.
Do you have any idea?

In the template you can first load the current value.
Then test the source,
If it does not match your requirements,
Just reload the old value