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

Also having this issue - ‘daily’ is undefined. Has anyone been able to get it working?

Okay I’ve managed to fix it!

I put this in my configuration.yaml where ‘home_2’ is my weather entity.

template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        target:
          entity_id: weather.home_2
        data:
          type: daily
        response_variable: daily
    sensor:
      - name: Daily Weather
        unique_id: weather_forecast_daily
        state: "{{ now().isoformat() }}"
        attributes:
          forecast: "{{ daily['weather.home_2'].forecast }}"

Then I’m able to access the forecast data in a similiar way to before. I’m using mine in an automation:

🌞 Weather forecast today: {{ states.sensor.home_condition_today.state }}.

    Temp: {{state_attr('sensor.daily_weather','forecast')[0].temperature}} °C // {{state_attr('sensor.daily_weather','forecast')[0].templow}} °C
     UV Index: {{ state_attr('sensor.home_uv_index','level').capitalize() }}
     Pollen: {{ state_attr('sensor.home_grass_pollen_today','level').capitalize() }}
     Mould: {{ state_attr('sensor.home_mould_pollen_today','level').capitalize() }}
     Tree Pollen: {{ state_attr('sensor.home_tree_pollen_today','level').capitalize() }}
     Ragweed Pollen: {{ state_attr('sensor.home_ragweed_pollen_today','level').capitalize() }}

    Next 4 days: {{state_attr('sensor.daily_weather','forecast')[1].condition}} | {{state_attr('sensor.daily_weather','forecast')[2].condition}} | {{state_attr('sensor.daily_weather','forecast')[3].condition}} | {{state_attr('sensor.daily_weather','forecast')[4].condition}} 

    Temperature: {{state_attr('sensor.daily_weather','forecast')[1].temperature}} °C | {{state_attr('sensor.daily_weather','forecast')[2].temperature}} °C | {{state_attr('sensor.daily_weather','forecast')[3].temperature}} °C | {{state_attr('sensor.daily_weather','forecast')[4].temperature}} °C

    Precipitation: {{state_attr('sensor.daily_weather','forecast')[1].precipitation}} mm | {{state_attr('sensor.daily_weather','forecast')[2].precipitation}} mm | {{state_attr('sensor.daily_weather','forecast')[3].precipitation}} | {{state_attr('sensor.daily_weather','forecast')[4].precipitation}} mm

The difference is before I was using:
{{ state_attr(‘weather.home_2’, ‘forecast’)[0].temperature }}

and now it is (where ‘daily weather’ is the name I have given my template sensor):
{{state_attr(‘sensor.daily_weather’,‘forecast’)[0].temperature}}

A small change that has been fiddly to figure out. Other templates I have seen have created a template attribute for each forecast attribute but doing it this way means it’s a bit easier to query each one.

2 Likes

yeah, I did the same as you and it worked!

did this works for you?
{{ daily }} returns as empty string “” so when im trying to set the attributes.forecast to:{{ daily[‘weather.my_weather’].forecast }} i get null on my sensor
am i missing something?

btw: invoking the same service with same entity_id from the hassio services returns a good result

Which weather integration are you using?

I’m late to this party and trying to fix an existing automation that gives me a summary forecast for the day, by parsing the hourly’s to give me highs and lows, then sending it to me as a push notification.

Since the change, which I knew was coming, its now broken the original implementation, but I’ve attempted to fix it but its still showing ‘none’ and ‘unknown’ for temperatures and conditions. This is my attempt to fix it:-

alias: Todays Temperature
description: ""
trigger:
  - platform: time
    at: "07:30:00"
  - platform: homeassistant
    event: start
condition: []
action:
  - service: weather.get_forecasts
    target:
      entity_id: weather.forecast_home
    data:
      type: hourly
    response_variable: forecast_data
  - service: notify.mobile_app_xx_phone
    data:
      title: Daily Forecast
      message: >-
        Good Morning! The temperature outside is currently
        {{state_attr('forecast_data.forecast[0]','temperature')}} degrees, 
        {%set highof = state_attr('forecast_data','forecast') |      
        selectattr('datetime', 'search', now().timestamp() |
        timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | max %} {%
        if highof is defined and highof %} with a high of {{highof}} degrees
        {%endif%} {%set lowof = state_attr('forecast_data','forecast') |   
        selectattr('datetime', 'search', now().timestamp() |
        timestamp_custom('%Y-%m-%d')) | map(attribute='temperature') | min %} {%
        if lowof is defined and lowof %} and a low of {{lowof}}{%endif%}. The
        weather is forecast to be {{states('weather.forecast_home_hourly')}}{%
        set currently_raining = states('weather.forecast_home_hourly') ==
        'rainy' %}{%set check_for_rain =
        state_attr('weather.forecast_home_hourly','forecast') |
        selectattr('datetime', 'search', now().timestamp() |
        timestamp_custom('%Y-%m-%d')) |
        selectattr('condition','equalto','rainy') | list%}{%if not
        currently_raining%}{% if check_for_rain is defined and check_for_rain %}
        and rainy. {% else %}, with no rain. {%endif%} {%else%}. {%endif%} The
        house is currently
        {{state_attr('climate.main_house','current_temperature')}} degrees.
mode: single

I’ve not attempted to fix the whole script, just the first three state_attr calls (current temperature, high for the day, and low for the day). The remainder I’ve left using the older mechanism until i figure out what I’m missing to fix the first ones.

Any help would be appreciated.

UPDATE:-

Looking at the thread above, I changed the first call to get the current temperature to:-

{{state_attr('forecast_data','forecast')[0].temperature}}

and it still doesnt work… I get an error in the logs:-

None has no element 0

You are calling the service directly into a response variable of ‘forecast_data’ so I think using this instead should work:

"{{ forecast_data['weather.forecast_home'].forecast[0].temperature }}"

Perfect… I’ve not done that before. I’ve also corrected the rest of the script based on this and its working… ty!

I’m having the same issue with unavailable and have tried multiple ways to resolve. For me, the Met.no integration is referenced as weather.forecast_home. I used the template sample and modified it to below. I’ve created this in the configuration.yaml file, tried it in the templates.yaml file and in the template sensor helper and they all show unavailable, even after rebooting HA and the server. If I run the get.forecasts service, it get the forecast and if I check weather.forecast_home from the entity states, I get the forecast. The code is:

template:
  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.forecast_home
        response_variable: hourly
    sensor:
      - name: Temperature forecast hourly
        unique_id: temperature_forecast_hourly
        state: "{{ hourly['weather.forecast_home'].forecast[0].temperature }}"
        unit_of_measurement: °F

i guess i had a typo somewhere, all work good now thanks!
btw: im using accuweather

I checked today the integration and even if it should update every hour or at HA startup, I have inconsistent data.
Last update time is this morning 6.40am when I have a weekly reboot of HA instance. No update in the following hours. Moreover, the forecast days are still the one of the first run of 1 week ago. They go from 7th to 12th April. Are anyone having this problem?

No errors shown in code:


  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: sensor.weather_daily
        response_variable: daily
    sensor:
      - name: Weather Daily
        state: "{{ states('sensor.weather_daily') }}"
        attributes:
          temperature: "{{ state_attr('sensor.weather_daily', 'temperature') }}"
          dew_point: "{{ state_attr('sensor.weather_daily', 'dew_point') }}"
          temperature_unit: "{{ state_attr('sensor.weather_daily', 'temperature_unit') }}"
          humidity: "{{ state_attr('sensor.weather_daily', 'humidity') }}"
          cloud_coverage: "{{ state_attr('sensor.weather_daily', 'cloud_coverage') }}"
          pressure: "{{ state_attr('sensor.weather_dailye', 'pressure') }}"
          pressure_unit: "{{ state_attr('sensor.weather_daily', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('sensor.weather_daily', 'wind_bearing') }}"
          wind_speed: "{{ state_attr('sensor.weather_daily', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('sensor.weather_daily', 'wind_speed_unit') }}"
          visibility_unit: "{{ state_attr('sensor.weather_daily', 'visibility_unit') }}"
          precipitation_unit: "{{ state_attr('sensor.weather_daily', 'precipitation_unit') }}"
          forecast: "{{ daily['sensor.weather_daily'].forecast }}"

Your target entity ID should be your weather integration which will start with ‘weather.’, as this is what you are calling the service for, try changing this and see if it helps.

Also, are you using the met.no integration? I’ve noticed in my logs that it often seems to fail on an automation with an API error.

if I do against

        target:
          entity_id: weather.forecast_home

I get error in the log. I tried to revert to the previous entity but I get error again, like if it worked only the first time last week.
Getting these now:

Logger: homeassistant.helpers.event
Source: helpers/template.py:588
First occurred: 12:49:33 PM (22 occurrences)
Last logged: 12:49:33 PM

Error while processing template: Template<template=({{ state_attr('sensor.weather_daily', 'forecast')[3].temperature | round }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.weather_daily', 'forecast')[3].templow | round }}) renders=2>
Error while processing template: Template<template=({{ as_timestamp(state_attr('sensor.weather_daily', 'forecast')[3].datetime) | timestamp_custom('%d') | int }} {{ as_timestamp(state_attr('sensor.weather_daily', 'forecast')[3].datetime) | timestamp_custom('%b') }}) renders=2>
Error while processing template: Template<template=({{ state_attr('sensor.weather_daily', 'forecast')[3].temperature | round }} / {{ state_attr('sensor.weather_daily', 'forecast')[3].templow | round }} °C) renders=2>
Error while processing template: Template<template=({% from 'tools.jinja' import umidita_assoluta %} {{ umidita_assoluta('sensor.gw1100a_temperature_2', 'sensor.gw1100a_humidity_2') }}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2218, in forgiving_float_filter
    return float(value)
           ^^^^^^^^^^^^
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 586, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2545, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.12/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2221, in forgiving_float_filter
    raise_no_default("float", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1849, in raise_no_default
    raise ValueError(
ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ states("sensor.google_travel_time") |float - state_attr("sensor.google_travel_time", "duration")| replace(" mins", "") | float }}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 712, in async_render_to_info
    render_info._result = self.async_render(
                          ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 588, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ states("sensor.google_travel_time") |float - state_attr("sensor.google_travel_time", "duration")| replace(" mins", "") | float }}' but no default was specified

Integration is with met.no, but I don’t see error in calling their api

That seems to be saying that it is getting unknown from calling the service. I think it might be an issue with the integration.

I had to click ‘load full logs’ for it to show me the error. Also if I went to the actual weather integration it was showing as unavailable.

Try reloading the integration?

integration works fine, I can read all data with daily/hourly forecast.
If I call the service alone it works:

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

if I call it in the template it seems there is no return (I tried to create a new sensor too):

and of course when it tries to get the attributes it fails.

  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home
        response_variable: daily
    sensor:
      - name: Weather Daily
        state: "{{ states('sensor.weather_daily') }}"
        attributes:
          temperature: "{{ state_attr('sensor.weather_daily', 'temperature') }}"
          dew_point: "{{ state_attr('sensor.weather_daily', 'dew_point') }}"
          temperature_unit: "{{ state_attr('sensor.weather_daily', 'temperature_unit') }}"
          humidity: "{{ state_attr('sensor.weather_daily', 'humidity') }}"
          cloud_coverage: "{{ state_attr('sensor.weather_daily', 'cloud_coverage') }}"
          pressure: "{{ state_attr('sensor.weather_daily', 'pressure') }}"
          pressure_unit: "{{ state_attr('sensor.weather_daily', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('sensor.weather_daily', 'wind_bearing') }}"
          wind_speed: "{{ state_attr('sensor.weather_daily', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('sensor.weather_daily', 'wind_speed_unit') }}"
          visibility_unit: "{{ state_attr('sensor.weather_daily', 'visibility_unit') }}"
          precipitation_unit: "{{ state_attr('sensor.weather_daily', 'precipitation_unit') }}"
          forecast: "{{ daily['sensor.weather_daily'].forecast }}"

I’m kinda confused where is the problem.

I changed the variable and now it populates the sensor, but so far the /1 trigger doesn’t seem to work. Just the startup trigger works

  - trigger:
      - platform: time_pattern
        hours: /1
      - platform: homeassistant
        event: start
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home
        response_variable: daily
    sensor:
      - name: Weather Daily
        state: "{{ states('weather.forecast_home') }}"
        attributes:
          temperature: "{{ state_attr('weather.forecast_home', 'temperature') }}"
          dew_point: "{{ state_attr('weather.forecast_home', 'dew_point') }}"
          temperature_unit: "{{ state_attr('weather.forecast_home', 'temperature_unit') }}"
          humidity: "{{ state_attr('weather.forecast_home', 'humidity') }}"
          cloud_coverage: "{{ state_attr('weather.forecast_home', 'cloud_coverage') }}"
          pressure: "{{ state_attr('weather.forecast_home', 'pressure') }}"
          pressure_unit: "{{ state_attr('weather.forecast_home', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('weather.forecast_home', 'wind_bearing') }}"
          wind_speed: "{{ state_attr('weather.forecast_home', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('weather.forecast_home', 'wind_speed_unit') }}"
          visibility_unit: "{{ state_attr('weather.forecast_home', 'visibility_unit') }}"
          precipitation_unit: "{{ state_attr('weather.forecast_home', 'precipitation_unit') }}"
          forecast: "{{ daily['weather.forecast_home'].forecast }}"

So I was using an if statement that depends on the weather entity.

{{ states.weather.forecast_ouachita_hills.attributes.forecast[0].templow | float >= 30 }}

if the weather was under 30 degrees it would turn the heat on if not it would turn it off.

How do I make a template for the new system?

Is this a sensor template? It doesn’t seem to work. I load my sensors thru yaml includes via sensor: !include_dir_merge_list includes/sensors.

Using this block:

- platform: template
# Old weather style
  trigger:
    - platform: time_pattern
      hours: /1
    - platform: homeassistant
      event: start
  action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.openweathermap
        response_variable: daily
  sensor:
      - name: Old forecast style weather
        state: "{{ states('weather.openweathermap') }}"
        attributes:
          temperature: "{{ state_attr('weather.openweathermap', 'temperature') }}"
          dew_point: "{{ state_attr('weather.openweathermap', 'dew_point') }}"
          temperature_unit: "{{ state_attr('weather.openweathermap', 'temperature_unit') }}"
          humidity: "{{ state_attr('weather.openweathermap', 'humidity') }}"
          cloud_coverage: "{{ state_attr('weather.openweathermap', 'cloud_coverage') }}"
          pressure: "{{ state_attr('weather.openweathermap', 'pressure') }}"
          pressure_unit: "{{ state_attr('weather.openweathermap', 'pressure_unit') }}"
          wind_bearing: "{{ state_attr('weather.openweathermap', 'wind_bearing') }}"
          wind_speed: "{{ state_attr('weather.openweathermap', 'wind_speed') }}"
          wind_speed_unit: "{{ state_attr('weather.openweathermap', 'wind_speed_unit') }}"
          visibility_unit: "{{ state_attr('weather.openweathermap', 'visibility_unit') }}"
          precipitation_unit: "{{ state_attr('weather.openweathermap', 'precipitation_unit') }}"
          forecast: "{{ daily['weather.openweathermap'].forecast }}"

The YAML check complains:

Invalid config for 'template' from integration 'sensor' at includes/sensors/weather.yaml, line 257: required key 'sensors' not provided
Invalid config for 'template' from integration 'sensor' at includes/sensors/weather.yaml, line 264: 'action' is an invalid option for 'sensor.template', check: action
Invalid config for 'template' from integration 'sensor' at includes/sensors/weather.yaml, line 271: 'sensor' is an invalid option for 'sensor.template', check: sensor

Changing sensor to sensors gives different validation errors:

Invalid config for 'template' from integration 'sensor' at includes/sensors/weather.yaml, line 264: 'action' is an invalid option for 'sensor.template', check: action
Invalid config for 'template' from integration 'sensor' at includes/sensors/weather.yaml, line 271: expected dictionary for dictionary value 'sensors', got [{'name': 'Old forecast style weather', 'state': "{{ states('weather.openweathermap') }}", 'attributes': {'temperature': "{{ state_attr('weather.openweathermap', 'temperature') }}", 'dew_point': "{{ state_attr('weather.openweathermap', 'dew_point') }}", 'temperature_unit': "{{ state_attr('weather.openweathermap', 'temperature_unit') }}", 'humidity': "{{ state_attr('weather.openweathermap', 'humidity') }}", 'cloud_coverage': "{{ state_attr('weather.openweathermap', 'cloud_coverage') }}", 'pres
...

Indeed sensor template(s) should have correct syntax to parse the output of the integration.
This is my working example (I am using Open-Meteo as source).
VS Code validation for some reason showing error, but the code is valid.

- trigger:
    - platform: time_pattern
      hours: /1
  action: 
    - service: weather.get_forecasts
      target:
        entity_id: weather.ozollapas
      data:
        type: daily
      response_variable: my_forecast
  sensor:
    - name: Total Expected Precipitation Today
      unique_id: 3d7abfa1-4e24-481c-a956-dfe71a8c0d9b
      icon: mdi:weather-pouring
      unit_of_measurement: "mm"
      state: >
        {% set precipitation_probability = my_forecast['weather.ozollapas'].forecast[0].precipitation %}
        {{ precipitation_probability | float(0) | round(1) }}
    - name: Temperature Low Expected Today
      unique_id: 693de803-37b0-4ba7-88ff-e753cda83b1d
      icon: mdi:thermometer-low
      unit_of_measurement: "°C"
      state: >
        {% set temperature_low = my_forecast['weather.ozollapas'].forecast[0].templow %}
        {{ temperature_low | float(0) | round(1) }}
    - name: Temperature Expected Today
      unique_id: 61282551-5337-4061-bf04-c9287fd90e54
      icon: mdi:thermometer
      unit_of_measurement: "°C"
      state: >
        {% set temperature = my_forecast['weather.ozollapas'].forecast[0].temperature %}
        {{ temperature | float(0) | round(1) }}
    - name: Wind Speed Expected Today
      unique_id: 37aa9b62-4b15-47fd-8ce7-da66ec59771e
      icon: mdi:wind-turbine
      unit_of_measurement: "m/s"
      state: >
        {% set wind = my_forecast['weather.ozollapas'].forecast[0].wind_speed %}
        {{ wind | float(0) | round(1) }}
1 Like

can someone please help me, I tried to use the template from here but it does not work, my virtual sensors show unavailable.

It is very basic, I only want to know tomorrow’s min and max forecast temperature in two sensors max and min.

Can someone please tell me what the issue is here:

template:
  - trigger:
      - platform: state
        entity_id: weather.forecast_home
    action:
      - service: weather.get_forecasts
        data:
          type: daily
        target:
          entity_id: weather.forecast_home
        response_variable: daily
    sensor:
      - name: Tomorrow Max Temp
        state: "{{ daily['weather.forecast_home'].forecast[1].temperature }}"
        attributes:
          forecast: "{{ daily['weather.forecast_home'].forecast }}"

      - name: Tomorrow Min Temp
        state: "{{ daily['weather.forecast_home'].forecast[1].templow }}"
        attributes:
          forecast: "{{ daily['weather.forecast_home'].forecast }}"

Now it works somehow, I just edited the name to forecast_sickenhausen … weird. I leave it here as a simple example.


Update 2: It is weird, I tried with more sensors. For this to work I had to disable the weather entity and enable it again. After this, the sensors had values immieadiatle.