Compare 2 dates, ignoring the time part

Hi

Why wont this work?
now().date() > strptime(states('input_datetime.saveddate'), '%Y-%m-%d %H:%M:%S.%f')

Is it at all possible to compare 2 dates and ignore the time portion?
I have an automation that i want to trigger once, every day when i get home from work after 4 pm.
I have the trigger configured, but it triggers everytime i leave the house, and come back after 4 pm.
I only want it to trigger once every day.

When my automation triggers i save the current date to input_datetime.saveddate.
Next time it triggers i want it to check if todays date is greater than the date stored in input_datetime.saveddate

I have 10 years of coding experince in C#, java, SQL and so on. But for the life of me i cant figure out the syntax of this

Make a Template Condition that checks if the last time the automation triggered was more than 24 hours ago.

 {{ now() - state_attr(this.entity_id, 'last_triggered') > timedelta(hours=24) }}

If the automation is new then it has no value for last_triggered and the template will fail with an error. To guard against this, you can do this:

 {{ now() - state_attr(this.entity_id, 'last_triggered') | default(today_at(), true) > timedelta(hours=24) }}

The idea here is if the automation hasn’t triggered in the last 24 hours then it is allowed to trigger now. All subsequent attempts to trigger are blocked for 24 hours.

I see what you are getting at, but then if one day, im not home before 10pm. It wont trigger at 4pm then next day, but only at 10pm…

Is it really impossible to just compare 2 dates?

Easy peasy. Use this in the automation’s condition. If the automation’s last_triggered date is the same as today, the automation’s action won’t be executed.

condition: "{{ (state_attr(this.entity_id, 'last_triggered')).date() != now().date() }}"

If you want to test the template in the Template Editor, use this version (use the automation’s actual name).

{{ (state_attr('automation.example', 'last_triggered')).date() != now().date() }}"
1 Like

Almost works.
But now if i come home before 4pm. It saves the last triggered date.

And if then then leave and come back after 4pm, it wont run to the end, because it has already been triggered.

Oh sweet baby jesus i finally got it!

I was missing | as_datetime, as my typecast.

In C# i am used to do it something like this
((datetime)input_datetime.saveddate).date

Below is how you compare the date from a datetime helper, with the current date

(states('input_datetime.saveddate') | as_datetime).date() < now().date()

1 Like

It’s an easy fix but you appear to be steadfastly determined to use a helper (to store the trigger datetime) so I won’t keep trying to convince you otherwise.