home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
70.95k stars 29.6k forks source link

Weather template - KeyError: 'condition_template' #103081

Open DirkMu opened 10 months ago

DirkMu commented 10 months ago

The problem

Currently, when checking the documentation I see two ways to configure a weather template. Maybe I misinterpret it.

template:
  - weather:

and

weather:
  - platform: template

When using the first approach with all values defined HA trows an error:

ERROR (MainThread) [homeassistant.components.weather] Error while setting up template platform for weather
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/template/weather.py", line 161, in async_setup_platform
    WeatherTemplate(
  File "/usr/src/homeassistant/homeassistant/components/weather/__init__.py", line 251, in __call__
    instance: PostInit = super().__call__(*args, **kwargs)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/template/weather.py", line 185, in __init__
    self._condition_template = config[CONF_CONDITION_TEMPLATE]
                               ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'condition_template'

What version of Home Assistant Core has the issue?

core-2023.10.5

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

template

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/

Diagnostics information

No response

Example YAML snippet

template:
  - weather:
    - name: "name"
      unique_id: my_unique_id
      condition_template: "{{ states('weather.my_region') }}"
      temperature_template: "{{ states('sensor.outside_temperature') }}"
      dew_point_template: "{{ states('sensor.my_region_dew_point') }}"
      apparent_temperature_template: "{{ states('sensor.outside_apparent_temperature') }}"
      humidity_template: "{{ states('sensor.humidity') }}"
      pressure_template: "{{ states('sensor.pressure') }}"
      wind_speed_template: "{{ states('sensor.wind_speed') }}"
      wind_gust_speed_template: "{{ states('sensor.wind_gust_speed') }}"
      wind_bearing_template: "{{ states('sensor.wind_bearing') }}"
      ozone_template: "{{ states('sensor.ozone') }}"
      cloud_coverage_template: "{{ states('sensor.cloud_coverage') }}"
      visibility_template: "{{ states('sensor.visibility') }}"
      forecast_daily_template: "{{ state_attr('sensor.weather_forecast_daily', 'forecast') }}"
      forecast_hourly_template: "{{ state_attr('weather.weather_forecast_hourly', 'forecast') }}"

Anything in the logs that might be useful for us?

ERROR (MainThread) [homeassistant.components.weather] Error while setting up template platform for weather
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 359, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/template/weather.py", line 161, in async_setup_platform
    WeatherTemplate(
  File "/usr/src/homeassistant/homeassistant/components/weather/__init__.py", line 251, in __call__
    instance: PostInit = super().__call__(*args, **kwargs)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/template/weather.py", line 185, in __init__
    self._condition_template = config[CONF_CONDITION_TEMPLATE]
                               ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'condition_template'

Additional information

No response

home-assistant[bot] commented 10 months ago

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `template` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign template` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

codyc1515 commented 7 months ago

The documentation for how to use this is rather poor, so I'm not surprised that you are facing issues. It would be good to see an actual demo given in the documentation, particularly around the forecast templates.

codyc1515 commented 7 months ago

Fixed here https://community.home-assistant.io/t/template-weather-provider-throws-keyerror-condition-template/673503/2

DirkMu commented 7 months ago

Hi. Thanks for contributing. From my perspective this is not a fix. It just confirms my experience. Today, all templates are supposed to be defined below template. This is what the documentation says. https://www.home-assistant.io/integrations/template/

template:
  - trigger:
  - binary_sensor:
  - sensor:
  - weather:

This is what I understand is the recommended new way. Following that documentation the weather tamplate is not created and HA throws errors. The approach mentioned in the community post, from my perspective, is the old way. That is working form me, too.

rdbahm commented 4 months ago

I'd love to add to this, as I ran into the same issue. I believe DirkMu is correct - the "modern" template format is like so:

template:
  - weather:

Legacy is:

weather:
  - platform: template

We can be really sure of this because all the development for services which return a value (like, say, get forecasts) has been on this "modern" style. That's also why I've got a renewed interest in this issue. With the elimination of the previously-deprecated "forecast" attribute on Weather entities, it's no longer possible to use the legacy template with forecasts (without creating a template sensor to periodically pull the attribute into a sensor). Since it doesn't currently seem to be possible to use the "modern" format, users of Template Weather Providers are dead in the water in 2024.4.

It might be as simple as the documentation needing a little polish. But I haven't yet gotten to the bottom of it. I'll see if I can learn more.

issue-triage-workflows[bot] commented 1 month ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

DirkMu commented 1 month ago

Not stale

pedolsky commented 1 week ago

Issue still exists (HA v2024.8.1) - developers' response would be really appreciated.