Template Weather [NWS]: moving from forecast_template to forecast_hourly_template

I started getting deprecation warnings for forecast_template. I am using the NWS integration and this is my current forecast_template setup.

forecast_template: "{{ state_attr('weather.kxxx_hourly', 'forecast') }}"

I tried swapping this to forecast_hourly_template: "{{ state_attr('weather.kxxx_hourly', 'forecast') }}" but this resulted in the following error 'Only valid keys in Forecast are allowed, unallowed keys: ({'detailed_description'})

I modified my forecast_hourly_template to

forecast_hourly_template: >-
      {%- set forecast = namespace(value=[]) -%}
      {%- for hf in  state_attr('weather.kxxx_hourly', 'forecast') -%}
          {%- set new_hf = {'datetime': hf.datetime, 'precipitation_probability': hf.precipitation_probability, 'condition': hf.condition, 'wind_bearing': hf.wind_bearing, 'temperature': hf.temperature, 'dew_point': hf.dew_point, 'wind_speed': hf.wind_speed, 'humidity': hf.humidity} -%}
      {%- set forecast.value = forecast.value + [new_hf] -%}
      {%- endfor -%}
      {{ forecast.value }}

This renders without an error, but the weather card no longer shows the forecast icons.

Looking at the attributes, using forecast_template I see.

supported_features: 0
temperature: 43
temperature_unit: °F
humidity: 79
pressure: 28.9
pressure_unit: inHg
wind_bearing: 210
wind_speed: 7
wind_speed_unit: kn
visibility: 10
visibility_unit: mi
precipitation_unit: in
forecast: 
- detailed_description: ''
  datetime: '2024-01-28T19:00:00-05:00'
  precipitation_probability: 47
  condition: rainy
  wind_bearing: 270
  temperature: 39
  dew_point: 36
  wind_speed: 8.69
  humidity: 89
 [... Extra entries removed ]

Looking at the attributes when using forecast_hourly_template I get

supported_features: 2
temperature: 43
temperature_unit: °F
humidity: 79
pressure: 28.9
pressure_unit: inHg
wind_bearing: 210
wind_speed: 7
wind_speed_unit: kn
visibility: 10
visibility_unit: mi
precipitation_unit: in

Any ideas what I’m missing here?

Back in 2023.9 Release they announced that for a weather entity, the forecast attribute is being deprecated.

The last config actually works, it turned out to be a frontend caching issue.

First time I’ve looked at Forecast template. Nice way you put this one together.

Just be aware that the forecast attribute here for the weather entity kxxx_hourly will go away in March.

And what is the secret sauce to get the twice daily or hourly forecasts now? I’m trying to update my template weather provider to use the NWS forecasts, and nothing I have tried so far gets the forecast into the template weather provider.

Here’s my template weather provider that doesn’t provide the forecast:

- platform: template
  name: "My Weather Station"
  unique_id: '0a7476cc-d6c1-40ba-8ae1-606518c3497f'
  condition_template: "{{ states('weather.kphl_daynight') }}"
  temperature_template: "{{ states('sensor.outdoor_temperature') | float(0.0) }}"
  temperature_unit: "°F"
  dew_point_template: "{{ states('sensor.kphl_dew_point') | float(0.0) }}"
  pressure_template: "{{ states('sensor.outdoor_pressure') | float(0.0) }}"
  pressure_unit: "inHg"
  humidity_template: "{{ states('sensor.outdoor_humidity') | float(0.0) }}"
  forecast_twice_daily_template: "{{ state_attr('weather.kphl_daynight', 'forecast') }}"
  wind_speed_template: "{{ states('sensor.kphl_wind_speed') | float(0.0) | round(0) }}"
  wind_speed_unit: "mph"
  wind_bearing_template: "{{ states('sensor.kphl_wind_direction') }}"

and a trigger sensor to get the high and low temperate, which also don’t work because I can’t get the forecast:

- trigger:
    - platform: state
      entity_id: weather.my_weather_station
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.my_weather_station
      response_variable: response
  sensor:
    - name: Today High
      device_class: Temperature
      unit_of_measurement: °F
      icon: mdi:thermometer-high
      state: "{{ response['weather.my_weather_station'].forecast[0].temperature }}"
    - name: Today Low
      device_class: Temperature
      unit_of_measurement: °F
      icon: mdi:thermometer-low
      state: "{{ response['weather.my_weather_station'].forecast[1].temperature }}"

Here’s the response to the service call to my template weather provider:

service: weather.get_forecasts
data:
  type: twice_daily
target:
  entity_id: weather.my_weather_station

weather.my_weather_station:
  forecast: []

And the same thing if I call the NWS provider:

service: weather.get_forecasts
data:
  type: twice_daily
target:
  entity_id: weather.kphl_daynight

weather.kphl_daynight:
  forecast:
    - detailed_description: Sunny, with a high near 64. Southeast wind 5 to 10 mph.
      datetime: "2024-04-26T11:00:00-04:00"
      precipitation_probability: 0
      is_daytime: true
      condition: partlycloudy
      wind_bearing: 135
      temperature: 64
      dew_point: 32
      wind_speed: 7.5
      humidity: 41
    - detailed_description: Mostly clear, with a low around 41. Southeast wind 0 to 10 mph.
      datetime: "2024-04-26T18:00:00-04:00"
      precipitation_probability: 0
      is_daytime: false
      condition: partlycloudy
      wind_bearing: 135
      temperature: 41
      dew_point: 35
      wind_speed: 5
      humidity: 79
    - detailed_description: Mostly sunny, with a high near 63. Southeast wind 0 to 10 mph.
      datetime: "2024-04-27T06:00:00-04:00"
      precipitation_probability: 0
      is_daytime: true
      condition: partlycloudy
      wind_bearing: 135
      temperature: 63
      dew_point: 37
      wind_speed: 5
      humidity: 79
    - detailed_description: >-
        A slight chance of rain showers after 2am. Mostly cloudy, with a low
        around 49. South wind 5 to 10 mph. Chance of precipitation is 20%.
      datetime: "2024-04-27T18:00:00-04:00"
      precipitation_probability: 20
      is_daytime: false
      condition: rainy
      wind_bearing: 180
      temperature: 49
      dew_point: 43
      wind_speed: 7.5
      humidity: 77
    - detailed_description: >-
        A slight chance of rain showers before 8am. Partly sunny, with a high
        near 79. Southwest wind 5 to 10 mph. Chance of precipitation is 20%.
      datetime: "2024-04-28T06:00:00-04:00"
      precipitation_probability: 20
      is_daytime: true
      condition: rainy
      wind_bearing: 225
      temperature: 79
      dew_point: 56
      wind_speed: 7.5
      humidity: 77
    - detailed_description: Partly cloudy, with a low around 60.
      datetime: "2024-04-28T18:00:00-04:00"
      precipitation_probability: 0
      is_daytime: false
      condition: partlycloudy
      wind_bearing: 270
      temperature: 60
      dew_point: 59
      wind_speed: 7.5
      humidity: 93
    - detailed_description: Mostly sunny, with a high near 86.
      datetime: "2024-04-29T06:00:00-04:00"
      precipitation_probability: 0
      is_daytime: true
      condition: partlycloudy
      wind_bearing: 315
      temperature: 86
      dew_point: 62
      wind_speed: 7.5
      humidity: 87
    - detailed_description: Partly cloudy, with a low around 59.
      datetime: "2024-04-29T18:00:00-04:00"
      precipitation_probability: 0
      is_daytime: false
      condition: partlycloudy
      wind_bearing: 135
      temperature: 59
      dew_point: 59
      wind_speed: 7.5
      humidity: 83
    - detailed_description: >-
        A chance of showers and thunderstorms after 2pm. Partly sunny, with a
        high near 81. Chance of precipitation is 30%.
      datetime: "2024-04-30T06:00:00-04:00"
      precipitation_probability: 30
      is_daytime: true
      condition: lightning-rainy
      wind_bearing: 135
      temperature: 81
      dew_point: 58
      wind_speed: 7.5
      humidity: 84
    - detailed_description: >-
        A chance of showers and thunderstorms before 2am. Mostly cloudy, with a
        low around 61. Chance of precipitation is 40%.
      datetime: "2024-04-30T18:00:00-04:00"
      precipitation_probability: 40
      is_daytime: false
      condition: lightning-rainy
      wind_bearing: 225
      temperature: 61
      dew_point: 58
      wind_speed: 7.5
      humidity: 90
    - detailed_description: >-
        A slight chance of rain showers after 2pm. Mostly sunny, with a high
        near 81.
      datetime: "2024-05-01T06:00:00-04:00"
      precipitation_probability: 0
      is_daytime: true
      condition: rainy
      wind_bearing: 270
      temperature: 81
      dew_point: 58
      wind_speed: 7.5
      humidity: 87
    - detailed_description: >-
        A slight chance of rain showers before 8pm. Partly cloudy, with a low
        around 59.
      datetime: "2024-05-01T18:00:00-04:00"
      precipitation_probability: 0
      is_daytime: false
      condition: rainy
      wind_bearing: 225
      temperature: 59
      dew_point: 57
      wind_speed: 7.5
      humidity: 87
    - detailed_description: >-
        A slight chance of rain showers after 2pm. Mostly sunny, with a high
        near 80.
      datetime: "2024-05-02T06:00:00-04:00"
      precipitation_probability: 0
      is_daytime: true
      condition: rainy
      wind_bearing: 270
      temperature: 80
      dew_point: 56
      wind_speed: 10
      humidity: 81
    - detailed_description: >-
        A slight chance of rain showers before 8pm, then a slight chance of
        showers and thunderstorms. Partly cloudy, with a low around 58.
      datetime: "2024-05-02T18:00:00-04:00"
      precipitation_probability: 0
      is_daytime: false
      condition: lightning-rainy
      wind_bearing: 270
      temperature: 58
      dew_point: 52
      wind_speed: 10
      humidity: 72

The general practice is:

  1. Use a “trigger” based “template sensor” which uses the weather.get_forecasts call to your weather.kphl_daynight to build a sensor with all the twice_daily forecast as an attribute. There have been several Forum threads on this topic on how to do this. Here is an example:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: twice_daily
        target:
          entity_id: weather.krdu_daynight
        response_variable: twice_daily_response_is

    sensor:
      - name: Weather Forecast Twice Daily
        unique_id: weather_forecast_twice_daily
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ twice_daily_response_is['weather.krdu_daynight'].forecast }}"
  

This produces a sensor sensor.weather_forecast_twice_daily.

  1. For your template weather, at places where you want to use forecast information, you should be able to use the sensor.weather_forecast_twice_daily sensor instead of weather.kphl_daynight. But I haven’t actually tried this on a template weather, so not sure if it actually works or not.

Unfortunately, it does not. With this updated code, I can see the forecast sensor populate, but using it in the template weather provider doesn’t give me a forecast in my custom provider. Thanks for the help though. I’m just going to go back to OpenWeatherMap (which works with my template weather provider) and hope someone updates that integration before OpenWeatherMap shuts down the 2.5 API.

This is the solution I came up with to get NWS weather working again.

Template sensor 1:

- trigger:
    - platform: time_pattern
      hours: /1
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      data:
        type: hourly
      target:
        entity_id: weather.kxxx_daynight
      response_variable: hourly_forecast
  sensor:
    - name: KXXX Houly Weather Forecast
      unique_id: uuid
      state: "{{ now().isoformat() }}"
      attributes:
        forecast: "{{ hourly_forecast['weather.kxxx_daynight']['forecast'] }}"
- trigger:
    - platform: time_pattern
      hours: /1
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: weather.get_forecasts
      data:
        type: twice_daily
      target:
        entity_id: weather.kxxx_daynight
      response_variable: td_forecast
  sensor:
    - name: KXXX Twice Daily Weather Forecast
      unique_id: uuid
      state: "{{ now().isoformat() }}"
      attributes:
        forecast: "{{ td_forecast['weather.kxxx_daynight']['forecast'] }}"

Template Sensor 2:

- platform: template
  name: "Home Weather"
  unique_id: uuid
  condition_template: "{{ states('weather.kxxx_daynight') }}"
  temperature_template: "{{ states('sensor.lumi_lumi_weather_temperature_4') | float }}"
  pressure_template: "{{ states('sensor.lumi_lumi_weather_pressure_4') | float }}"
  humidity_template: "{{ states('sensor.lumi_lumi_weather_humidity_4') | float }}"
  wind_speed_template: "{{ state_attr('weather.kxxx_daynight', 'wind_speed') }}"
  wind_bearing_template: "{{ state_attr('weather.kxxx_daynight', 'wind_bearing') }}"
  visibility_template: "{{ state_attr('weather.kxxx_daynight', 'visibility') }}"
  forecast_hourly_template: >-
      {%- set forecast = namespace(value=[]) -%}
      {% if states('sensor.kxxx_houly_weather_forecast') != 'unavailable' %}
        {%- for hf in  state_attr('sensor.kxxx_houly_weather_forecast', 'forecast') -%}
            {%- set new_hf = {'datetime': hf.datetime, 'precipitation_probability': hf.precipitation_probability, 'condition': hf.condition, 'wind_bearing': hf.wind_bearing, 'temperature': hf.temperature, 'dew_point': hf.dew_point, 'wind_speed': hf.wind_speed, 'humidity': hf.humidity} -%}
            {%- if new_hf.datetime | as_datetime > utcnow() -%}
                {%- set forecast.value = forecast.value + [new_hf] -%}
            {%- endif -%}
        {%- endfor -%}
      {% endif %}
      {{ forecast.value }}

Don’t forget to exclude the template sensors from the recorder or you will get errors.

A total win for user friendless and ease of setup…

2 Likes

The NWS integration includes 11 sensor entities for current observed weather parameters. Does anyone know the syntax for how to trigger these to populate the entities?

Are you asking how to enable the entities?

In the Entities menu find the entity you want to enable and click it.

In the More Info pop-up, click the Settings menu button:
image

Click Enable:

I appreciate the response, but sorry, no. They’re all enabled.

When I posted the question, every NWS native entity was 'unknown.' The daily and hourly forecasts were being populated. I eventually performed a restart and can see them now. Something isn’t working as it should as I’ve seen it happen a few times already. If the condition persists, I’ll try to find a way to repeat the misbehavior and post an issue.

Thanks again.

Thank you, sir!

I was able to work with your examples to get mine working. I added in a daily template. Figured I’d share back. Cheers!

  forecast_daily_template: >-
    {%- set forecast = namespace(value=[]) -%}
    {% if states('sensor.kxxx_twice_daily_weather_forecast') != 'unavailable' %}
      {%- for hf in  state_attr('sensor.kxxx_twice_daily_weather_forecast', 'forecast') -%}
        {%- set hf_date = (hf.datetime | as_datetime).replace(hour=0, minute=0, second=0, microsecond=0).isoformat() -%}
        {%- if((hf.is_daytime) or loop.first) -%}    
          {%- set new_hf = {'datetime': hf_date, 'precipitation_probability': hf.precipitation_probability, 'condition': hf.condition, 'wind_bearing': hf.wind_bearing, 'temperature': hf.temperature, 'templow': hf.temperature, 'wind_speed': hf.wind_speed, 'is_daytime': hf.is_daytime} -%}
          {%- if((hf.is_daytime) and not loop.last) -%}
            {%- set night_item = state_attr('sensor.kxxx_twice_daily_weather_forecast', 'forecast')[loop.index] -%}      
            {%- set new_hf = {'datetime': hf_date, 'precipitation_probability': hf.precipitation_probability, 'condition': hf.condition, 'wind_bearing': hf.wind_bearing, 'temperature': hf.temperature, 'wind_speed': hf.wind_speed, 'is_daytime': hf.is_daytime, 'templow': night_item.temperature} -%}
          {%- endif -%}
          {%- if new_hf.datetime | as_datetime >= as_datetime(utcnow()).replace(hour=0, minute=0, second=0, microsecond=0) -%}
              {%- set forecast.value = forecast.value + [new_hf] -%}
          {%- endif -%}
        {%- endif -%}
      {%- endfor -%}
    {% endif %}
    {{ forecast.value }}  
1 Like