[PETITION] Don't delete the forecast attribute

Also, weather.get_forecast has been deprecated, you need to use weather.get_forecasts:

action:
  - service: weather.get_forecasts
    data:
      type: hourly
    target:
      entity_id: weather.pirateweather
    response_variable: pirate_valasz
  - service: input_number.set_value
    data_template:
      entity_id: input_number.weather_pirate_esso_esely
      value: "{{ pirate_valasz['weather.pirateweather']['forecast'][0]['precipitation_probability'] }}"
1 Like

I never find it out alone, thank you guys!

Once again, surprised on a cold wintery morning where the heating did not come on. This time, I could have known better, because I knew weather.get_forecast would be deprecated.

Change 1: Use correct service weather.get_forecasts

Since this post, weather.get_forecast has been deprecated, so if you need this now, you will have to change both service calls from service: weather.get_forecast to service: weather.get_forecasts.

Change 2: Adapt template for dictionary format returned by weather.get_forecasts

This will result in the following error messages in the log, since get_forecast returns a dictionary object, which is different to how get_forecast used to return the forecast information:

* Error rendering state template for sensor.weather_hourly: UndefinedError: 'dict object' has no attribute 'forecast'
* Error rendering state template for sensor.weather_daily: UndefinedError: 'dict object' has no attribute 'forecast'

One needs to select the target entity (e. g. weather.home) as the key when retrieving the forecast from either response_variable.

To do so, you need to change

          forecast: "{{ hourly_forecast.forecast[:5] }}"

into

          forecast: "{{ hourly_forecast['weather.home'].forecast[:5] }}"

and

          forecast: "{{ daily_forecast.forecast[:5] }}"

into

          forecast: "{{ daily_forecast['weather.home'].forecast[:5] }}"

The updated code, as taken from my configuration.yaml, now looks like this:

template:
  - trigger:
    - platform: time_pattern
      minutes: "*"
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
    action:
      - service: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: hourly
        response_variable: hourly_forecast
      - service: weather.get_forecasts
        target:
          entity_id: weather.home
        data:
          type: daily
        response_variable: daily_forecast
    sensor:
      - name: "Weather Hourly"
        unique_id: home_hourly
        state: "{{ states('weather.home') }}"
        attributes:
          temperature: "{{ state_attr('weather.home', 'temperature') }}"
          dew_point: "{{ state_attr('weather.home', 'dew_point') }}"
          temperature_unit: "{{ state_attr('weather.home', 'temperature_unit') }}"
          humidity: "{{ state_attr('weather.home', 'humidity') }}"
          cloud_coverage: "{{ state_attr('weather.home', 'cloud_coverage') }}"
          pressure: "{{ state_attr('weather.home', 'pressure') }}"
          pressure_unit: "{{ state_attr('weather.home', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('weather.home', 'wind_bearing') }}"
          wind_gust_speed: "{{ state_attr('weather.home', 'wind_gust_speed') }}"
          wind_speed: "{{ state_attr('weather.home', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('weather.home', 'wind_speed_unit') }}"
          visibility: "{{ state_attr('weather.home', 'visibility') }}"
          visibility_unit: "{{ state_attr('weather.home', 'visibility_unit') }}"
          precipitation: "{{ state_attr('weather.home', 'precipitation') }}"
          precipitation_unit: "{{ state_attr('weather.home', 'precipitation_unit') }}"
          forecast: "{{ hourly_forecast['weather.home'].forecast[:5] }}"
      - name: "Weather Daily"
        unique_id: home_daily
        state: "{{ states('weather.home') }}"
        attributes:
          temperature: "{{ state_attr('weather.home', 'temperature') }}"
          dew_point: "{{ state_attr('weather.home', 'dew_point') }}"
          temperature_unit: "{{ state_attr('weather.home', 'temperature_unit') }}"
          humidity: "{{ state_attr('weather.home', 'humidity') }}"
          cloud_coverage: "{{ state_attr('weather.home', 'cloud_coverage') }}"
          pressure: "{{ state_attr('weather.home', 'pressure') }}"
          pressure_unit: "{{ state_attr('weather.home', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('weather.home', 'wind_bearing') }}"
          wind_gust_speed: "{{ state_attr('weather.home', 'wind_gust_speed') }}"
          wind_speed: "{{ state_attr('weather.home', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('weather.home', 'wind_speed_unit') }}"
          visibility: "{{ state_attr('weather.home', 'visibility') }}"
          visibility_unit: "{{ state_attr('weather.home', 'visibility_unit') }}"
          precipitation: "{{ state_attr('weather.home', 'precipitation') }}"
          precipitation_unit: "{{ state_attr('weather.home', 'precipitation_unit') }}"
          forecast: "{{ daily_forecast['weather.home'].forecast[:5] }}"

Happy heating :slight_smile:

2 Likes

Nice work FearlessReach!
Thank you for this code