How to read weather forecast in a template

I have (had) a template sensor which would show me the upcoming high temperature in the next 24 hours, using the long deprecated and recently removed “daynight” forecast attribute in the NWS integration. Here is my old sensor:

    day_max_temp:
      friendly_name: "Upcoming high temp"
      unique_id: upcoming_temp_unique_id
      #unit_of_measurement: "°F"
      value_template: >
        {% set ns = namespace(tomorrowHigh = -999, goalDay = now().day + 1) %}
        {% if now().hour < 12 %}{% set ns.goalDay = now().day %} {% endif %}
        {% for fcst in state_attr('weather.kbdn_daynight' , 'forecast')%}
        {% set dt = strptime(fcst.datetime, "%Y-%m-%dT%H:%M:%S%z") %}
        {% if ns.goalDay == dt.day and fcst.is_daytime %}
        {% set ns.tomorrowHigh = fcst.temperature %}
        {% endif %}{%- endfor %}
        {{ ns.tomorrowHigh | float }}

Unfortunately, with this recent commit: Remove deprecated forecast attribute from WeatherEntity (#110761) · home-assistant/core@6523090 · GitHub
the above no longer works. Looking at the state attributes in the dev tools shows this:

temperature: 39
temperature_unit: °F
humidity: 75
pressure: 30
pressure_unit: inHg
wind_bearing: 0
wind_speed: 0
wind_speed_unit: mph
visibility: 10
visibility_unit: mi
precipitation_unit: in
attribution: Data from National Weather Service/NOAA
friendly_name: KBDN
supported_features: 6

so nothing that could easily be used to get the same information. I see that there is a _async_forecast_twice_daily() function on the forecast integration itself, but I can’t seem to find any documentation anywhere on how to actually get that information out of my existing weather entity and into the logic to determine the upcoming high. Does anyone know how to do this?

Update: immediately after posting this I found this thread, which appears to have all my answers.

I’ll update here with whatever I get working after reading that thread.

Okay, here’s what I got working. Here’s the hacky workaround to get back to the previously working state of having the forecast data in the “attributes” of something so that it’s usable. in configuration.yaml I added template: !include templates.yaml, and the contents of templates.yaml is as follows:

- trigger:
  - platform: time_pattern
    minutes: "/15"
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.kbdn_daynight
      response_variable: daynight
    - variables:
        today: "{{ daynight['weather.kbdn_daynight'].forecast }}"
  sensor:
    - name: Weather Forecast Daily Daynight
      unique_id: weather_forecast_daily_daynight
      state: "{{ now() }}"
      attributes:
        forecast: "{{ daynight['weather.kbdn_daynight'].forecast }}"

This updates every 15 minutes, which is way more than necessary but w/e.

I then updated my sensor template to:

- platform: template
  sensors:
    day_max_temp:
      friendly_name: "Upcoming high temp"
      unique_id: upcoming_temp_unique_id
      unit_of_measurement: "°F"
      value_template: >
        {% set ns = namespace(tomorrowHigh = -999, goalDay = now().day + 1) %}
        {% if now().hour < 12 %}{% set ns.goalDay = now().day %} {% endif %}
        {% if now().month + 1 == strptime(state_attr('sensor.weather_forecast_daily_daynight' , 'forecast')[2].datetime, "%Y-%m-%dT%H:%M:%S%z").month %}{% set ns.goalDay = 1 %} {% endif %}
        {% for fcst in state_attr('sensor.weather_forecast_daily_daynight' , 'forecast')%}
        {% set dt = strptime(fcst.datetime, "%Y-%m-%dT%H:%M:%S%z") %}
        {% if ns.goalDay == dt.day and fcst.is_daytime %}
        {% set ns.tomorrowHigh = fcst.temperature %}
        {% endif %}{%- endfor %}
        {{ ns.tomorrowHigh | float }}