Creating weather template combining data from weather service and local data including forecast with get_forecasts

I want to create a weather template that combines information I gather form local sensors with weather services.

I have the following template code in my configuration.yaml - but get_forecast does not work (it returns )… I’ve spent hours…

weather:
  - platform: template
    name: "Weather Hometown"
    unique_id: 9397f709-8934-46a9-aa61-31ae2bf5a012
    condition_template: "{{ states('weather.weather_meteo_swiss') }}"
    temperature_template: "{{ states('sensor.weatherstation_outside_temperature') | float }}"
    temperature_unit: "°C"
    humidity_template: "{{ states('sensor.weatherstation_outside_humidity') | float }}"
    forecast_daily_template: "{{ state_attr('weather.weather_meteo_swiss', 'forecast') }}"
    pressure_template: "{{ state_attr('weather.weather_meteo_swiss', 'pressure') | float }}"
    attribution_template: "Meteo Swiss and local sensors"
    wind_speed_template: "{{ state_attr('weather.weather_meteo_swiss', 'wind_speed') | float }}"
    wind_bearing_template: "{{ state_attr('weather.weather_meteo_swiss', 'wind_bearing') | float }}"

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start

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

    sensor:
      - name: Daily Forecast
        unique_id: weather_forecast_daily
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ daily['weather.weather_meteo_swiss'].forecast }}"

  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start

    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id:
            - weather.weather_hometown
        response_variable: hourly
    sensor:
      - name: Hourly Forecast
        unique_id: weather_forecast_hourly
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ hourly['weather.weather_meteo_swiss'].forecast }}"

What am I doing wrong?

Why are you calling for a forecast from the weather service weather.weather_hometown but trying to access a response from a different weather service weather.weather_meteo_swiss?

What do you mean by this…?

Because I want to use it as data source for elements on dashboards (which support only one entity as source). I want to show the forecast for my hometown combined with data (e.g. current temperature) from my own sensors.

if I run the action weather.get_forecast for this entity I get []as reply - so no forecast.
If I remove forecast_daily_template I get an error.

Make sure you are using the correct action weather.get_forecasts

Your template weather entity should be using the entity ID of the template sensors not Meteo weather for forecast_daily_template and forecast_hourly_template. The forecast attribute for weather entities was remove 10 months ago, so {{ state_attr('weather.weather_meteo_swiss', 'forecast') }} will return nothing.

weather:
  - platform: template
    name: "Weather Hometown"
    unique_id: 9397f709-8934-46a9-aa61-31ae2bf5a012
    condition_template: "{{ states('weather.weather_meteo_swiss') }}"
    temperature_template: "{{ states('sensor.weatherstation_outside_temperature') | float }}"
    temperature_unit: "°C"
    humidity_template: "{{ states('sensor.weatherstation_outside_humidity') | float }}"
    pressure_template: "{{ state_attr('weather.weather_meteo_swiss', 'pressure') | float }}"
    attribution_template: "Meteo Swiss and local sensors"
    wind_speed_template: "{{ state_attr('weather.weather_meteo_swiss', 'wind_speed') | float }}"
    wind_bearing_template: "{{ state_attr('weather.weather_meteo_swiss', 'wind_bearing') | float }}"
    forecast_daily_template: "{{ state_attr('sensor.daily_forecast', 'forecast') }}"
    forecast_hourly_template: "{{ state_attr('sensor.hourly_forecast', 'forecast') }}"

#... The rest of the config for the template sensors...

Don’t forget to restart HA so your trigger-based template sensors populate their forecast attributes.

1 Like

That’s simply not how programming works. You cannot ask HASS to get a forecast from one weather service (the one you’ve defined yourself) but then try to access a response from a completely different service. And since your own weather service is not correctly setup to generate a forecast, well the response you’re getting will be empty…

I’m trying to pass the query for a forecast to my weather service on to another weather service that has that information… Something like inheritance in programming…

Let me explain better what I want to achieve:

  • On my dashboard I want to show a weather-card (weather-chart-card)
  • I want it to use the weather-data from a local weather-service (MeteoSwiss)
  • As I measure the current temperature outside my house I want to show the measured temperature instead of the temperature I get from the weather-service in the card on the dashboard.

So - the same idea as described here: Template Weather Provider - Home Assistant
But if I do it according to this description the card obviuously can’t show any forecasts.

Can anybody point me in the right direction?

Completely untested, I have no idea whatsoever if this works or not, but from reading the documentation this is my understanding of how it should be done:

weather:
  - platform: template
    name: "Weather Hometown"
    unique_id: 9397f709-8934-46a9-aa61-31ae2bf5a012
    condition_template: "{{ states('weather.weather_meteo_swiss') }}"
    temperature_template: "{{ states('sensor.weatherstation_outside_temperature') | float }}"
    temperature_unit: "°C"
    humidity_template: "{{ states('sensor.weatherstation_outside_humidity') | float }}"
    forecast_daily_template: "{{ state_attr('sensor.weather_meteo_swiss', 'forecast_daily') }}"
    forecast_hourly_template: "{{ state_attr('sensor.weather_meteo_swiss', 'forecast_hourly') }}"
    pressure_template: "{{ state_attr('weather.weather_meteo_swiss', 'pressure') | float }}"
    attribution_template: "Meteo Swiss and local sensors"
    wind_speed_template: "{{ state_attr('weather.weather_meteo_swiss', 'wind_speed') | float }}"
    wind_bearing_template: "{{ state_attr('weather.weather_meteo_swiss', 'wind_bearing') | float }}"

template:
  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.weather_meteo_swiss
        response_variable: forecast_daily
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.weather_meteo_swiss
        response_variable: forecast_hourly
    sensor:
      - name: Weather Meteo Swiss
        device_class: timestamp
        state: "{{ now().isoformat() }}"
        attributes:
          forecast_daily: "{{ forecast_daily['weather.weather_meteo_swiss'].forecast }}"
          forecast_hourly: "{{ forecast_hourly['weather.weather_meteo_swiss'].forecast }}"

Yes… read what I have already posted above.

(post deleted by author)

You Guys rock! Thank you very much @Mayhem_SWE and @Didgeridrew.