Forecast max temperature - Filter a collection by datetime in templates

I’m trying to set a sensor which reports the maximum temperature forecast for today. I’m using this block so far:

{%- set today = now().date() %}
{%- set tomorrow = today + timedelta(days=1) %}
{{ state_attr('sensor.hourly_weather_forecast_at_home', 'forecast') | selectattr('datetime', '>=', today) | selectattr('datetime', '<', tomorrow)  | list }}

However ‘datetime’ attribute in the collection is a ‘string’ not a ‘datetime.date’, thus the comparison cannot be done.

Is there a way to cast/transform an attribute of a collection in a filter statement?

{%- set today = now().date() %}
{%- set tomorrow = today + timedelta(days=1) %}
{{ state_attr('sensor.hourly_weather_forecast_at_home', 'forecast') 
| selectattr('datetime', '>=', today | string) 
| selectattr('datetime', '<', tomorrow | string)  | list }}
1 Like

the fact that logical expressions can be used for string variables is nonetheless still…weird to me :confused: tnx allot!

“z” is greater than “a”. Properly-ordered date strings can be sorted and compared a you would expect. See the General Principles section of the ISO8601 Wikipedia article: