Hello,
I’m trying to compare two times:
{{states(‘input_datetime.uur_douche_stijn’)}}
and {{now()}}.
This obviously won’t work because I’m comparing a string to a time.
How do I fix this?
Thanks for any feedback.
Hello,
I’m trying to compare two times:
{{states(‘input_datetime.uur_douche_stijn’)}}
and {{now()}}.
This obviously won’t work because I’m comparing a string to a time.
How do I fix this?
Thanks for any feedback.
To compare a date and time input_datetime to now() you need a timezone-aware, datetime object…
{{states('input_datetime.uur_douche_stijn')|as_datetime|as_local < now() }}
Input Datetimes that are just time or just date require different conversion steps.
Thanks for your reply.
Yes, I had already found that, but then I get an error message: “AttributeError: ‘NoneType’ object has no attribute ‘tzinfo’”
Don’t understand it either?
Is your helper date and time, just date, or just time?
{{states('input_datetime.uur_douche_stijn')}}
gives: 20:18:00
Since it only provides time, you need to turn it into a datetime object. The today_at() function is the easiest way to do it:
{{ today_at(states('input_datetime.ztest_time_only')) > now()}}
Or… compare it to the time string from now():
{{ states('input_datetime.ztest_time_only') > now().time()|string }}
Thank you so much
this solved my problem. ![]()
I find all that time and date stuff very tiring and difficult!! ![]()
![]()
It can be one of the most confusing parts of templating… there are a lot of formats, data types, and methods; plus timezones and daylight saving time. As someone without a programming background, I try to stick to datetime objects whenever possible.
Now the next problem. I only want to consider hours and minutes, not seconds, etc. What should I add?
The time and dates are driving me crazy. ![]()
Whilst it is indeed possible to perform the necessary UTC to local to time to string conversions in templates, Home Assistant has a much underrated Time and Date integration. Easy to set up using UI, sensors can be configured for time, date, date and time, UTC and local, and ISO timestamp format.
The local time sensor is very useful, state being a string with 24 hour format, hence “20:45” making comparison tests intuitive.
HA updates these sensors every minute, which also makes them great as triggers.
“now” for time (local) becomes just
states(‘sensor.time’)
That’s easier to do using datetime objects instead of strings because you have access to the replace method:
{{ today_at(states('input_datetime.uur_douche_stijn')).replace(second=0)
> now().replace(second=0, microsecond=0)}}
Sorry, but I actually have another question. I’d like to compare the {{states(‘input_datetime.uur_douche_stijn’)}} - 1 hour with {{now()}}.
Sorry, but I really can’t figure it out!
Use timedelta() to add or subtract time from a datetime object:
{% set x = today_at(states('input_datetime.uur_douche_stijn')).replace(second=0) - timedelta(hours=1) %}
{{ x > now().replace(second=0, microsecond=0) }}
I had already tried that, but I get the following error: ‘TypeError: ‘>’ not supported between instances of ‘datetime.timedelta’ and ‘str’’
Compare what you tried with what I posted above… by the error message it seems likely that you have mixed string methods with datetime methods.
I just copied your code.