Hi all - Sorry this is basic question, but its driving me crazy as I can’t work out the solution.
I’m trying to configure an if statement within a sensor template. I need it to detect if its been more than 24 hours since another sensor’s time stamp.
Currently I have the following configured, but its not working… I’ve tried lots of things, but have obviously got something wrong in the IF statement and can’t work it out.
Without running the states() function on 'sensor.on_peloton_end_time' first, you are esentially trying to turn a non-datetime string into a timestamp… which doesn’t work.
{% if as_timestamp(now()) - as_timestamp(states('sensor.on_peloton_end_time')) > 86400 -%}
......
Another option is to used datetime objects and timedelta instead of timestamps:
{% set last = states('sensor.on_peloton_end_time') | as_datetime | as_local %}
{% if now() - last >= timedelta(hours=24) %}
.....
{% else %}
.....
{% endif %}
For completeness, this is my final sensor, which I think does what I need…
If the work out is more than 24hours ago, it will change the workout time to 00:00:01. This will ensure that number of days since the workout will be updated at midnight, and not the same time of day as the when workout occurred (for example, mid-afternoon).
If the workout is less than 24hours ago, it will use the unaltered workout time.
This sensor uses the relative_time function, so that it uses ‘minutes’ or ‘hours’ if less than 24 hours ago or ‘days’ if above. It will also use singular or plural in the suffix as required (eg. day vs days), which allows for a clean presentation.
Finally the icon for the sensor is configured to reflect the workout type.