Template action response_variable is undefined

Hey experts,

I’ve been stuck working this issue now for a few hours and looking to see if anyone might have a quick fix. Working to create a template that pulls in weather data using weather.get_forecasts and uses response variables to get both daily and hourly data. When using the template I get UndefinedError: ‘hourly’ is undefined even though my format is the exact same as is in the Home Assistant documentation for weather. Any ideas are greatly appreciated!

- trigger:
    - platform: time_pattern
      minutes: "0" # Runs hourly at minute 0
    - platform: homeassistant
      event: start # Triggers when Home Assistant starts
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.forecast_home
      data:
        type: hourly
      response_variable: hourly

    - service: weather.get_forecasts
      target:
        entity_id: weather.forecast_home
      data:
        type: daily
      response_variable: daily

    - service: persistent_notification.create
      data:
        title: "Forecast Output"
        message: "{{ hourly }}"
  sensor:
    - name: Weather Forecast
      unique_id: weather_forecast
      state: "{{ now().isoformat() }}"
      attributes:
        now_condition: "{{ hourly['weather.forecast_home'].forecast[0].condition }}"
        now_icon: >
          {% set weather = hourly['weather.forecast_home'].forecast[0].temperature %}
          {% if is_state('sun.sun', 'above_horizon') %}
          {{ '/local/weather' + weather|string + '.svg' }}
          {% else %}
          {{ '/local/weather' + weather|string + '_night.svg' }}
          {% endif %}
        now_temp: "{{ hourly['weather.forecast_home'].forecast[0].temperature }}"
        now_wind_speed: "{{ hourly['weather.forecast_home'].forecast[0].wind_speed }}"
        now_wind_bearing: "{{ hourly['weather.forecast_home'].forecast[0].wind_bearing }}"
        now_precipitation: "{{ hourly['weather.forecast_home'].forecast[0].precipitation }}"
        #now_precipitation_probability: "{{ hourly['weather.forecast_home'].forecast[0].precipitation_probability }}"
        now_humidity: "{{ hourly['weather.forecast_home'].forecast[0].humidity }}"
        now_feels_like: >
          {% set temperature = hourly['weather.forecast_home'].forecast[0].temperature %}
          {% set humidity = hourly['weather.forecast_home'].forecast[0].humidity %}
          {% set windspeedmps = hourly['weather.forecast_home'].forecast[0].wind_speed / 3.6 %}
          {% set e = 2.71828 %}
          {% set wvp = (humidity/100) * 6.105 * e**((17.27*temperature) / (237.7 + temperature)) %}
          {{(temperature + 0.33 * wvp - 0.70 * windspeedmps - 4.00)|round(2)}}
        now_wind_direction: >
          {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
          {% set degree = (hourly['weather.forecast_home'].forecast[0].wind_bearing | float) %}
          {{ direction[((degree + 11.25) / 22.5) | int] }}
        now_wind_desc: >
          {% from 'weather_wind.jinja' import get_wind_description %}
          {{ get_wind_description(hourly['weather.forecast_home'].forecast[0].wind_speed | float) }}
        today_condition: "{{ daily['weather.forecast_home'].forecast[0].condition }}"
        today_icon: >
          {% set weather = daily['weather.forecast_home'].forecast[0].temperature %}
          {{ '/local/weather' + weather|string + '.svg' }}
        today_temp: "{{ daily['weather.forecast_home'].forecast[0].temperature }}"
        today_wind_speed: "{{ daily['weather.forecast_home'].forecast[0].wind_speed }}"
        today_wind_bearing: "{{ daily['weather.forecast_home'].forecast[0].wind_bearing }}"
        #today_precipitation_probability: "{{ daily['weather.forecast_home'].forecast[0].precipitation_probability }}"
        today_humidity: "{{ daily['weather.forecast_home'].forecast[0].humidity }}"
        today_feels_like: >
          {% set temperature = daily['weather.forecast_home'].forecast[0].temperature %}
          {% set humidity = daily['weather.forecast_home'].forecast[0].humidity %}
          {% set windspeedmps = daily['weather.forecast_home'].forecast[0].wind_speed / 3.6 %}
          {% set e = 2.71828 %}
          {% set wvp = (humidity/100) * 6.105 * e**((17.27*temperature) / (237.7 + temperature)) %}
          {{(temperature + 0.33 * wvp - 0.70 * windspeedmps - 4.00)|round(2)}}
        today_wind_direction: >
          {% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
          {% set degree = (daily['weather.forecast_home'].forecast[0].wind_bearing | float) %}
          {{ direction[((degree + 11.25) / 22.5) | int] }}
        today_wind_desc: >
          {% from 'weather_wind.jinja' import get_wind_description %}
          {{ get_wind_description(daily['weather.forecast_home'].forecast[0].wind_speed | float) }}
        forc1_datetime: "{{ daily['weather.forecast_home'].forecast[1].datetime}}"
        forc1_condition: "{{ daily['weather.forecast_home'].forecast[1].condition }}"
        forc1_icon: >
          {% set weather = daily['weather.forecast_home'].forecast[1].temperature %}
          {{ '/local/weather' + weather|string + '.svg' }}
        forc1_temp: "{{ daily['weather.forecast_home'].forecast[1].temperature }}"
        forc1_precipitation: "{{ daily['weather.forecast_home'].forecast[1].precipitation }}"
        forc2_datetime: "{{ daily['weather.forecast_home'].forecast[2].datetime}}"
        forc2_condition: "{{ daily['weather.forecast_home'].forecast[2].condition }}"
        forc2_icon: >
          {% set weather = daily['weather.forecast_home'].forecast[2].temperature %}
          {{ '/local/weather' + weather|string + '.svg' }}
        forc2_temp: "{{ daily['weather.forecast_home'].forecast[2].temperature }}"
        forc2_precipitation: "{{ daily['weather.forecast_home'].forecast[2].precipitation }}"
        forc3_datetime: "{{ daily['weather.forecast_home'].forecast[3].datetime}}"
        forc3_condition: "{{ daily['weather.forecast_home'].forecast[3].condition }}"
        forc3_icon: >
          {% set weather = daily['weather.forecast_home'].forecast[3].temperature %}
          {{ '/local/weather' + weather|string + '.svg' }}
        forc3_temp: "{{ daily['weather.forecast_home'].forecast[3].temperature }}"
        forc3_precipitation: "{{ daily['weather.forecast_home'].forecast[3].precipitation }}"
        forc4_datetime: "{{ daily['weather.forecast_home'].forecast[4].datetime}}"
        forc4_condition: "{{ daily['weather.forecast_home'].forecast[4].condition }}"
        forc4_icon: >
          {% set weather = daily['weather.forecast_home'].forecast[4].temperature %}
          {{ '/local/weather' + weather|string + '.svg' }}
        forc4_temp: "{{ daily['weather.forecast_home'].forecast[4].temperature }}"
        forc4_precipitation: "{{ daily['weather.forecast_home'].forecast[4].precipitation }}"
        forc5_datetime: "{{ daily['weather.forecast_home'].forecast[5].datetime}}"
        forc5_condition: "{{ daily['weather.forecast_home'].forecast[5].condition }}"
        forc5_icon: >
          {% set weather = daily['weather.forecast_home'].forecast[5].temperature %}
          {{ '/local/weather' + weather|string + '.svg' }}
        forc5_temp: "{{ daily['weather.forecast_home'].forecast[5].temperature }}"
        forc5_precipitation: "{{ daily['weather.forecast_home'].forecast[5].precipitation }}"

Thanks again!

Where are you “using” this template?

Template sensors that use advanced functions like triggers need to be added to your configuration files.

If you are trying to test your templates using the Template Editor, you need to first use the Actions tool to retrieve the data (use the “Copy to Clipboard (Template)” button at the bottom of the response field.

Hi. I have the exact same issue. Yes, this is in the configuration.yaml file. Still says “variable is undefined”. I have read through half a dozen posts here about the same issue, no joy.

This is my config:

template:
  - trigger:
      - trigger: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    action:
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home
        response_variable: response
  - sensor:
      - name: "Weather Forecast Text"
        unique_id: "weather_forecast_text"
        state: "Condition: {{response['weather.forecast_home'].forecast[0].condition}}"

With my config, I get “variable ‘response’ is undefined”.

Note that I am using met.no so their forecast response looks a bit different:

weather.forecast_home:
  forecast:
    - condition: cloudy
      datetime: "2025-08-04T10:00:00+00:00"
      wind_bearing: 253.1
      uv_index: 6
      temperature: 21.7
      templow: 16.5
      wind_speed: 21.6
      precipitation: 0.6
      humidity: 60
    - condition: sunny
      datetime: "2025-08-05T10:00:00+00:00"
      wind_bearing: 255.5
      uv_index: 7.3
      temperature: 27.2
      templow: 15.3
      wind_speed: 16.9
      precipitation: 1.3
      humidity: 62
    - condition: rainy
      datetime: "2025-08-06T10:00:00+00:00"
      wind_bearing: 290.2
      uv_index: 6.5
      temperature: 24
      templow: 15.9
      wind_speed: 19.4
      precipitation: 3.4
      humidity: 64
    - condition: partlycloudy
      datetime: "2025-08-07T10:00:00+00:00"
      wind_bearing: 100.3
      uv_index: 0
      temperature: 27.7
      templow: 14
      wind_speed: 13.7
      precipitation: 0
      humidity: 48
    - condition: sunny
      datetime: "2025-08-08T10:00:00+00:00"
      wind_bearing: 71
      temperature: 30.3
      templow: 18.1
      wind_speed: 7.2
      precipitation: 0
      humidity: 56
    - condition: partlycloudy
      datetime: "2025-08-09T10:00:00+00:00"
      wind_bearing: 51.4
      temperature: 32.1
      templow: 21.4
      wind_speed: 7.6
      precipitation: 0
      humidity: 57

Oh and just to clarify, running this action in the developer tools works and produces the above output:

action: weather.get_forecasts
data:
  type: daily
target:
  entity_id: weather.forecast_home

because you have a - in front of sensor: which separates your - trigger: and action: from your sensor:.

1 Like

Oh my god.
Yes that’s the problem. Thanks a lot, I was banging my head against the wall here!

For reference, here’s my working config using met.no:

template:
  - trigger:
      - trigger: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
      - platform: event
        event_type: event_template_reloaded
    action:
      - action: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home
        response_variable: response
    sensor:
      - name: "Weather Forecast Text"
        unique_id: "weather_forecast_text_tts"
        state: >
           {% set forecast = response['weather.forecast_home'].forecast %} {% if forecast %}
             Today is {{ forecast[0].condition }} with {{ forecast[0].temperature | round(0) }} degrees celsius.
             and wind speeds of up to {{ forecast[0].wind_speed | round(0) }} kilometers per hour..
             Tomorrow will be {{ forecast[1].condition }} with {{ forecast[1].temperature | round(0) }} degrees celsius.
             and wind speeds of up to {{ forecast[1].wind_speed | round(0) }} kilometers per hour.
           {% else %}
             Forecast unavailable.
           {% endif %}