Template Sensor Days Since a date

Hello Guys,

I wonder if you could help me, Ive had a search around but I can’t seem to find out what it is I have done wrong here.

I have set up a template that I am trying to get to calculate the the number of days that have passed since that date.

Problem 1:

When I set todays date, I am expecting an an answer of 0 but im getting -1

Problem 2:

I get a minus number for any date set in the past, is there any way I could reverse this or remove minus? In this situation id like it to read “11”

  - platform: template  
    sensors:
      chore_feed_patch_days_since_last_completed:
        friendly_name: "Days Since Last Completed"
        value_template: '{{ ((( states.input_datetime.chore_feed_patch_last_completed.attributes.timestamp - as_timestamp(now()) )) | int /60/1440) | round(0) }}'

Thanks

  - platform: template  
    sensors:
      chore_feed_patch_days_since_last_completed:
        friendly_name: "Days Since Last Completed"
        value_template: >
          {% set dt = states('input_datetime.chore_feed_patch_last_completed')
            | as_datetime | as_local %}
          {{ (dt.date() - now().date()).days | abs }}

Stating the obvious, have you set your timezone correctly?

1 Like

that worked perfectly, thanks!

1 Like