Sensor If Statement - More than 24hours

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.

sensor_name:
   value_template: >-
      {% if as_timestamp(now()) - as_timestamp('sensor.on_peloton_end_time') > 86400 -%}
         ......
      {%- else -%}
         ......
      {%- endif %}

For info, the sensor sensor.on_peloton_end_time has a class of timestamp

Thanks for any help/advice!

Two issues:

  • your sensor state is what you should be trying to convert to a timestamp: as_timestamp(states'sensor.on_peloton_end_time')
  • this will only work if your timestamp sensor is in UTC format - eg: 2022-11-09T10:31:00

Didgeridrew gave you a solution a few days ago. If you compare it with your code above, you can see the difference.

What should happen, when the if statement becomes true?

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 %}

Thanks all, appreciate all the help.

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.

Thanks again for your help!

      time_since_workout:
          friendly_name: Time Since Workout
          unit_of_measurement: 'ago'
          value_template: >-
            {% set last = states('sensor.on_peloton_end_time') | as_datetime | as_local %}
            {% if  now() - last >= timedelta(hours=24) %}
              {% set last = (states('sensor.on_peloton_end_time') | as_datetime | as_local).replace(hour=0, minute=0, second=1) %}
              {{ relative_time(last) }}
            {% else %}
              {{ relative_time(last) }}
            {% endif %}
          icon_template: >-
            {% if is_state_attr('binary_sensor.on_peloton_workout', 'Workout Type', 'strength') %}
              mdi:dumbbell
            {% elif is_state_attr('binary_sensor.on_peloton_workout', 'Workout Type', 'cycling') %}
              mdi:bike
            {% elif is_state_attr('binary_sensor.on_peloton_workout', 'Workout Type', 'running') %}
              mdi:run-fast
            {% elif is_state_attr('binary_sensor.on_peloton_workout', 'Workout Type', 'cardio') %}
              mdi:run
            {% elif is_state_attr('binary_sensor.on_peloton_workout', 'Workout Type', 'yoga') %}
              mdi:yoga
            {% else %}
              mdi:shoe-sneaker
            {% endif %}