Help working out difference between two dates on template sensor

Hey Guys,

Would anyone be able to assist with my code, I would like to work out how many days there are between the current date and the data specified in my input_datetime helper.

The code below seems to give me a correct answer however the output is:

2 days, 15:13:27.207750 

How would I go about cleaning up the output so it just gives me the “2” and drops the rest.

{% set duedate = now().fromtimestamp(state_attr('input_datetime.rubber_plant_feed_date', 'timestamp')).astimezone() %}

{% set currentdate = now().astimezone() %}

{% set days =  currentdate - duedate %}

{{days}} 

The result (i.e., days) is a Python timedelta object. That object has a total_seconds() method, which, of course, returns the total number of seconds the delta represents. If you use that you can divide by the number of seconds in a day, then round or whatever.

Or you can simplify the template:

{% set duedate = state_attr('input_datetime.rubber_plant_feed_date', 'timestamp') %}

{% set currentdate = as_timestamp(now()) %}

{% set seconds =  duedate - currentdate  %}

{{(seconds / 86400)|round()}} Days

or even:

{% set seconds = state_attr('input_datetime.rubber_plant_feed_date', 'timestamp') - as_timestamp(now()) %}

{{(seconds / 86400)|round()}} Days

or:

{{ ((state_attr('input_datetime.rubber_plant_feed_date', 'timestamp') - as_timestamp(now()))  / 86400) | round()}} Days
5 Likes
{% set days = (now() - now().fromtimestamp(state_attr('input_datetime.rubber_plant_feed_date', 'timestamp')).astimezone()) | string %}
{{ days.split()[0] if 'day' in days else 0 }} 

EDIT

If rubber_plant_feed_date is typically a future date then reverse the order of the subtraction (otherwise the result will be a negative value).