However as you can see “stundenverhersage” contains the hourly forecast for the next 24 hours.
It should be easy but I don’t get it running. I want to find the maximum temperature value out of these data. With the side permission, filtered by the current date, so that I get the maximum temperature out of the data of the remaining hours of the current day.
What I did is this:
- name: Maximale Temperatur des verbleibenden Tages
unique_id: temperature_maximum_remaining_day
state: >
{% set max_temp = -999 %}
{% set today = now().date().isoformat() %}
{% for forecast in stundenvorhersage['weather.forecast_home'].forecast %}
{% set forecast_date = forecast.datetime[8:10] %}
{% if forecast_date == today[8:10]%}
{% set max_temp = [max_temp, forecast.temperature] | max %}
{% endif %}
{% endfor %}
unit_of_measurement: °C
Anyone any idea? Probably easy. Unfortunately I am new to home assistant and coding.
I am using the standard met.no weather service.
What I get is the value of the entity = “-999”.
This is a scoping issue. You need to use a namespace to extract the values out of the loop. However, it is more efficient to use the built-in filters than loops wherever possible.
Are you sure? This method does not limit the values to those from the current day, so the value returned is the max of all forecasts for the next 24 hours, so sometimes it will be the current day and sometimes it will not.
You can use index slicing to get the rest of the day’s values based on the number of hours left in the day.
{% set h_remain = 24 - now().hour %}
{{ stundenvorhersage['weather.forecast_home'].forecast[:h_remain+1]
| map(attribute='temperature') | max }}
As the date is given from the weather forecast as 2025-01-05T00:00:00+00:00 I start ignoring the first 8 characters and just look for the “05” to compare it with the current “day number”. (First I had some issues comparing the whole date, maybe because of different “-” / “–”, but I don’t know, because I realized, that for 24 hours comparing the day is enough.)
You did it the other way round, that would have been my next try.
Thank you very much!