How to migrate from weather integration approach to new weather.get_forecast service?

Hi, I use this template too and I don’t have this error. But with the new version of the weather.get_forecasts service (with an s) it no longer works because there is a header that is added to the result. Does anyone know how to solve this problem?

Before with weather.get_forecast

After with weather.get_forecasts

Just add the name of the forecast provider in the name like so:

daily['weather.forecast_shawinigan_sud'].forecast
2 Likes

I think I have different issue. I just would like to use a very basic forecast card.

It works, thank you :grinning:

hi. can you provide your working sample.

This is my current working sample, but come march 2024 it will break.

template:
# My Weather Entities Daily
  - trigger:
      - platform: state
        entity_id: weather.my_weather
    action:
      - service: weather.get_forecast
        data:
          type: daily
        target:
          entity_id: weather.my_weather
        response_variable: daily
    sensor:
      - name: My Weather Forecast Daily
        icon: mdi:weather-cloudy
        state: "{{ daily.forecast[0].condition }}"
        attributes:
          forecast: "{{ daily.forecast }}"

The service call’s name is now plural:

weather.get_forecasts

The new service call lets you get the forecast from multiple weather entities. The response_variable’s structure is slightly different so you have to specify which entity’s forecast you want (even if you only asked for the forecast from one weather entity).

daily['weather.my_weather'].forecast

Example

template:
# My Weather Entities Daily
  - trigger:
      - platform: state
        entity_id: weather.my_weather
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.my_weather
        response_variable: daily
    sensor:
      - name: My Weather Forecast Daily
        icon: mdi:weather-cloudy
        state: "{{ daily['weather.my_weather'].forecast[0].condition }}"
        attributes:
          forecast: "{{ daily['weather.my_weather'].forecast }}"
5 Likes

Thank you for your help here.
Let me step back. :slight_smile:
As I have the integration here, is the weather service related config still needed?

The only customization what I need, to round the Celsius values on my cards. This was my original config, which became deprecated.

weather:
  - platform: template
    name: "otthon_rounded"
    condition_template: "{{ states('weather.otthon') }}"
    temperature_template: "{{ state_attr('weather.otthon', 'temperature') | round(0, default=-99) }}"
    humidity_template: "{{ state_attr('weather.otthon', 'humidity')}}"
    forecast_template: >
      {% set ns = namespace(z = []) %}
      {% for x in state_attr('weather.otthon', 'forecast') %}
        {% set ns.z = ns.z +
          [{
          'condition': x.condition,
          'precipitation': x.precipitation,
          'temperature': x.temperature | round(0, default=-99),
          'templow': x.templow | round(0, default=-99),
          'datetime': x.datetime
          }] %}
       {% endfor %}
       {{ ns.z }}

The deprecation caused me a bit of grief too. I was using it, among other things, to identify “bad weather” in 24h forecast and store that in template “safety” binary sensors, which I would then use to notify me. But since you can’t use forecast attribute in templates anymore (post-deprecation)…

I devised a new method. State/attribute changes on weather entity will run get_forecasts, identify the same as before, and notify me if needed; no more template sensors, everything’s in one automation.
Be sure to change the timedelta in the last_triggered condition to whatever you want, if you reuse this automation.

It’s still available. It will be removed in March.

With this change, the forecast attribute of the weather entity is deprecated, and will be removed in Home Assistant Core 2024.3.

Here is the definition of my new template. I don’t use the same variables, but basically it’s the same code.


template:

# Obtenir les pévisions journalières
          
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_shawinigan_sud
        response_variable: previsionsjournalieres
    sensor:
      - name: Prévisions météorologiques journalières
        unique_id: previsions_meteorologiques_journalieres
        state: "{{ now().isoformat() }}"
        icon: mdi:hours-24
        attributes:
          forecast: "{{ previsionsjournalieres['weather.forecast_shawinigan_sud'].forecast }}"

I know… but this thread is about migrating, so assuming post-removal support…

After 2024.3, you can’t use it anymore. Until then, it’s still usable.

Does anyone know what the best practice is here to round the temperature values?
As we can’t use forecast attribute in templates anymore from March.
Thank you!

I recently made a change from using “forecast” to “forecasts” in my configuration, following the suggestions from Taras and ValMarDav also adding ‘weather.forecast_home’ to the attribute definition. However, it seems like the sensor broke again, and I’m encountering an error stating that “hourly” is not defined.

- trigger:
    - platform: time_pattern
      hours: /1
  action:
    - service: weather.get_forecasts
      target:
        entity_id: weather.forecast_home
      data:
        type: hourly
      response_variable: hourly
  sensor:
    - name: Wetter Vorhersage Stündlich
      unique_id: weather_forecast_hourly
      state: "{{ now().isoformat() }}"
      attributes:
        condition: "{{ hourly['weather.forecast_home'].forecast[0].condition }}"
        temperature: "{{ hourly['weather.forecast_home'].forecast[0].temperature }}"
        wind_bearing: "{{ hourly['weather.forecast_home'].forecast[0].wind_bearing }}"
        cloud_coverage: "{{ hourly['weather.forecast_home'].forecast[0].cloud_coverage }}"
        precipitation: "{{ hourly['weather.forecast_home'].forecast[0].precipitation }}"
        humidity: "{{ hourly['weather.forecast_home'].forecast[0].humidity }}"

If I call the service in the developer tools, it pulls the data, so it should be the sensor definition where I made a mistake.

Hi, I tried your code and it works. The only thing I changed is the weather entity.

Hi, thank you for tying it out. I restarted Homeassistant a couple times and now it somehow works. I did not change anything, but now the response variable is recognized. No idea, why it works now, but it works :-). Thank you for your support.

Hello,
I have the same problem. I also restarted teh Homeassistant a couple times, but not solved.

One possible thing to look at, is whether your weather integration has been updated to support the new methods for getting forecast data.
One quick way to check is to goto UI->Developers tools->Services tab and enter something like:

service: weather.get_forecasts
data:
  type: daily
target:
  entity_id: weather.your_weather_entity

If it returns forecast data, then that part is good news.

Can anyone help me how to use forecast with a weather card?

I had a forecast shown on a dashboard for the next 8 hours.
When I use dev tools to check my weather entity, I can see lots of data.

  forecast:
    - condition: cloudy
      precipitation_probability: 0
      datetime: "2024-04-05T18:00:00+00:00"
      wind_bearing: 212
      cloud_coverage: 98
      temperature: 17.3
      pressure: 1016
      wind_speed: 8.75
      precipitation: 0
      humidity: 75

However, when I use a weather card, it does not matter which one, I only see the upcoming temperature but never precipation and preciptation probability.


forecast2

Why does non of my weather cards use that data to show it?

I post this here because the error only come up after updating to 2024.4.

First try the standard Lovelace weather card just to rule out the Lovelace card.