Sensor state change

Hi!

I’d like to configure a sensor that tracks my rain sensor its last state change agains the actual time.
With that information i know how long ago there was rain and i can build a better automation to start my garden irrigation.

Hope someone can help me with that.
Thanks in advance!

{{ states.sensor.rain_sensor.last_changed }}

Will give you the human readable time. The templating docs and the items linked from it can explain more. You can do things like convert it to a timestamp (number of seconds since midnight on 1 January 1970) and then subtract that from the current timestamp.

Thanks! Can you point me a little bit more into the right direction what that looks like?

Sure, here is a script that checks to see if an entity updated in the last two seconds:

      {{ 0 < ((as_timestamp(now()) - as_timestamp(states.image_processing.doods_front_camera.last_updated)) < 2 ) }}

You can do the same thing by replacing image_processing.doods_front_camera with the entity you’re tracking.

1 Like

thanks! i’ll go ahead and test with that!

So i have tried this and it works good! Only downside to this is when the sensor goes to unavailable, the state changes and the check is not reliable anymore. Is there a possibility to only allow this check between state changes ‘off’ and ‘on’?

So, what do you want to happen.
Do you need to know when it went unavailable (that would protect your garden worst case so maybe water early) or do you want to wait till its valid again (not sure what information that gives you.
Your best bet would be to get a more reliable sensor or maybe use 2 off them to double chances of reporting ???

I’d like to start the watering when there is no rain for a minimal of 5 hours.
So i started off with the template provided by @Tinkerer and adjusted it to my needs

      {{ 0 < ((as_timestamp(now()) - as_timestamp(states.binary_sensor.rain_sensor_garden.last_updated)) > 18000 ) }}

Problem is that this template also reports ‘false’ when it went from ‘off’ (dry) to ‘unavailable’ because the state changed, which makes sense.
Best case scenario would be that this sensor remains ‘true’ even if it went from ‘off’ to ‘unavailable’ and back to ‘off’, after a reboot or a short interruption for example. But that is not how last_updated works, right?
So i have no clue how to tackle this :slight_smile:
Hope this makes a bit sense.

No, your template is faulty, it says go to true if greater than 0 AND greater than 18,000
What’s the point in that ?

Well, there is not. My knowledge of templates is close to non existing, excuse me for that:). That’s why I’m trying to get help from you guys to accomplish that.

      {{ is_state('binary_sensor.rain_sensor_garden','on') and (0 < (as_timestamp(now()) - as_timestamp(states.binary_sensor.rain_sensor_garden.last_updated)) < 18000 ) }}

Might be what you want

Basically, require it to be on and for it to have updated in the last 18,000 seconds

i’ll try and work with that! Thanks!