The response_data: variable undefined when using a service action

I am trying to automate the update of a custom weather panel using the weather.get_forecasts service. No matter what I do, I always end up with the response_variable as described as undefined. I have even gone back to basics and copy/pasted this example: Template - Home Assistant

into both the template debugger (and read the thread here: Weather forecasts response variable undefined and know that won’t work) but also used the UI method of adding a template (Template - Home Assistant) and still get the error:

I’ve also tried putting the yaml in the configuration.yaml file to the same error.

I do have the weather.home entity and if I run the service request in the developer tools, it responds with the expected data OK.

Any ideas?

It needs to be in the config YAML; the UI doesn’t yet have the necessary functionality. See the example here:

Please don’t post screenshots. The indentation as shown is flat and thus wrong. Post the complete, properly-formatted YAML that you have is configuration.yaml. It needs to be under the template: header — check you don’t already have that header in an !include declaration.

Thanks. Now moved it to the configuration.yaml.

My configuration.yaml

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensor.yaml

#
# Customisations for home use
emulated_hue: !include emulated_hue.yaml

#
# Mail settings
notify:
  - name: "*******"
    platform: smtp
    sender: "****"
    recipient: "****"
    server: "****"
    port: 25
    encrytion: "none"
    verify_ssl: false

#
# Startup Zigbee home automation
zha:

#
# Templates:
template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.home
        response_variable: hourly
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: °C


The sensor shows in the entities list, but has a value of unknown. Restarted H/A but I can’t see any errors in the logs (Settings.logs with the weather.home logs enabled in settings->integrations, but I could be looking in the wrong place (I am a s/w dev by trade, but this is my first time looking at these templates in anger).

its going to be unknown until the trigger occurs, which is at the start of the hour. If you want it to update at startup, add a homeassistant startup event trigger.

1 Like

And it’ll only show the temperature. You’ll need to put back the forecast attribute like you had before if you need that.

    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.home'].forecast[0].temperature }}"
        unit_of_measurement: °C
        attributes:
          forecast: "{{ hourly['weather.home']['forecast'] }}"
1 Like

Thanks. Both repliers have helped me investigate. Not got time to look at it in detail now, but from a quick look around, I can see a way forward. The entity weather.home doesn’t have a forecast entity (I’m using the met.no integration), I have to use weather.forecast_home to get forecast info.

This was predicated by me losing the weekly forecast in the weather widget in Lovelace. I’m running 2024.4.2, so up to date. Will have to trawl the release notes to see if anything has changed here.

I’ll complete this as an exercise for the reader to gain some experience, but the two answers here have helped immensely (BTW, I spent the last 2 years of my working life working with Python on Django so understand the syntax, just not yaml and how it is put together and how everything interoperates… apologies for the simple questions)

I’ll post a complete answer when I have things working.

It looks like met.no are changing their integration. weather.home has gone completely from my setup in the last hour or two. Using the entity weather.forecast_home in the Lovelace card has current and forecast back again! Now trying it in the template…

So the sensor is now showing up and shows the right value for temperature. I can now work with the attributes to get the forecast and display that on screen.

What was I not doing right:

  • Not having the template loaded directly in configuration.yaml (or, I guess have a template file loaded with a template: !include template.yaml)
  • Using an entity that has undergone change in the last few weeks/days. The weather.home entity is no longer is supported by the Norwegian met service, and appears to have moved its forecast section to a newer entity (weather.forecast_home - which also includes the current values now).
  • Being a newbie and not understanding the template section in developer tools doesn’t work for these sorts of things nor does the UI template.

Thanks to both that responded - it would have taken me ages of thinking around before ending up in the right place. I’ll mark this as the solution simply because it was a combination of things in the end causing the issue.