theclue
(Gabriele Baldassarre)
April 9, 2024, 5:47pm
1
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
theclue
(Gabriele Baldassarre)
April 9, 2024, 9:30pm
4
the fact that logical expressions can be used for string variables is nonetheless still…weird to me tnx allot!
Troon
(Troon)
April 9, 2024, 9:35pm
5
“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:
2024-04-09T20:21:37+00:00 UTC+00:00 [refresh]
ISO 8601 is an international standard covering the worldwide exchange and communication of date and time-related data. It is maintained by the International Organization for Standardization (ISO) and was first published in 1988, with updates in 1991, 2000, 2004, and 2019, and an amendment in 2022. The standard provides a well-defined, unambiguous method of representing calendar dates and times in worldwide communications, especially to avoid misinter...