Template sensor to show maximum temperatur of remaining day

Hello everyone :slight_smile:
I created my first template to get weather based triggers for automations.
It works fine so far.

- trigger: 
  - trigger: time_pattern
    minutes: /1
  action:
    - action: weather.get_forecasts
      target:
        entity_id:
          - weather.forecast_home
      data:
          type: hourly
      response_variable: stundenvorhersage
  sensor:
    - name: Temperaturvorhersage der nächsten Stunde
      unique_id: temperature_forecast_next_hour
      state: "{{ stundenvorhersage['weather.forecast_home'].forecast[1].temperature }}"
      unit_of_measurement: °C

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”.

Thank you for your help!

On the one hand i would really like to know where my mistake in the above code is. It should be some how possible to do it that way, is it not?

On the other hand. Here is a solution, I found. It is way more elegant. And it works.

- trigger: 
  - trigger: time_pattern
    minutes: /1
  action:
    - action: weather.get_forecasts
      target:
        entity_id:
          - weather.forecast_home
      data:
          type: hourly
      response_variable: stundenvorhersage
  sensor:
    - name: Wettervorhersage der nächsten Stunde
      unique_id: forecast_next_1_hour
      state: "{{ now().isoformat() }}"
      icon: mdi:hours-24
      attributes:
        datetime: "{{ stundenvorhersage['weather.forecast_home'].forecast[1].datetime }}"
        temperature: "{{ stundenvorhersage['weather.forecast_home'].forecast[1].temperature }}"
    - name: Temperaturvorhersage der nächsten Stunde
      unique_id: temperature_forecast_next_hour
      state: "{{ stundenvorhersage['weather.forecast_home'].forecast[1].temperature }}"
      unit_of_measurement: °C
    - name: Vorhersage Temperatur Maximum verbleibender Tag
      unique_id: forecast_temperatur_maximum_remaining_day
      state: "{{ (stundenvorhersage['weather.forecast_home'].forecast | map(attribute='temperature') | max) }}"
      unit_of_measurement: °C

Maybe it is a help for someone.

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 }}

You are totally right. Obviously I was to tired to copy the right code after trying for hours.
My final result actually is:

    - name: weatherforecast_remaininghourstoday_temperatur_maximum
      unique_id: weatherforecast_remaininghourstoday_temperatur_maximum
      state: "{{ (stundenvorhersage['weather.forecast_home'].forecast | selectattr('datetime', 'search', '.{8}' + now().strftime('%d')) | map(attribute='temperature') | max) }}"
      unit_of_measurement: °C

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. :wink:
Thank you very much! :slight_smile: