Subtract two dates, problem with format

I’ve tried my best to try to find a solution, in threads and on documentation, but I can’t get it…

I just want to subtract 2 dates in format MM/DD/YY… and get the days between the 2 dates

Things that I tried…

{% set vacuum_last_used = as_timestamp(states('sensor.rockrobo_vacuum_v1_last_clean_end')) | timestamp_custom('%D')  %}

{% set fecha_hoy = as_timestamp(states('')) | timestamp_custom('%D')  %}

# vacuum_last_used= 05/03/22
# fecha_hoy= 05/05/22

 {{ (as_timestamp(states(''), "%D")) - (as_timestamp(vacuum_last_used, "%D")) | int}}

{{ relative_time((strptime(fecha_hoy, '%D'))) - relative_time((strptime(vacuum_last_used, '%D'))) }}

(a){{ ((as_datetime(states('')) - as_datetime(states('sensor.rockrobo_vacuum_v1_last_clean_end'))) | int /60/1440) | round(0) }}

{{ (strptime(fecha_hoy, '%D')) - (strptime(vacuum_last_used, '%D')) }}

I tried using as_timestamp and as_datetime… but bothing, I get errors that I can use the operato - with str, other error was TypeError: can't subtract offset-naive and offset-aware datetimes in condition (a)

If you could help me…

Thank you!

It looks like you are making it way more complicated than is probably necessary…
Is it absolutely necessary to have the MM/DD/YY step?

If not, you can use today_at() to create a datetime object for the start of the current day:

{% set vacuum_last_used = states('sensor.rockrobo_vacuum_v1_last_clean_end') | as_datetime | as_local  %}
{{ (today_at() - vacuum_last_used).days }}
1 Like

That works indeed, but the problem is because it calculates the time in the operatio too, and even if the dates are different by 1 day, because it takes in account the hour, it says the difference is 0 days

Copy-paste the following template into the Template Editor and confirm it produces the desired result (it reports the number of days).

{{ (now().date() - (states('sensor.rockrobo_vacuum_v1_last_clean_end') | as_datetime | as_local).date()).days }}


Correction. Misplaced parenthesis.


Gives this error:
TypeError: unsupported operand type(s) for -: '' and 'datetime.datetime'

One of the open parentheses needs to be moved:

{{ (now().date() 
- (states('sensor.rockrobo_vacuum_v1_last_clean_end') 
| as_datetime | as_local).date()).days }}
1 Like

I adapted that template from a similar one I posted elsewhere. In the process I misplaced the location of a parenthesis (as Didgeridrew quickly identified). I have corrected the example posted above.

1 Like

Thank you so much, working perfect!

1 Like

@Didgeridrew How can I do exactly this with hours instead of days?

Because timedelta() stores its values as a combination of days and seconds, you have to combine those into a single unit and do a little math to get whole hours.

{% set diff = now() - states('sensor.vaccuum_last_ran') | as_datetime | as_local %}
{{ (diff.total_seconds()/3600) | int(0) }}

If you want to include fractions of the hour you can remove the | int(0) from the end.

1 Like

Thanks. Appreciated