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.
FearlessReach:
Here is the precise code I inserted into configuration.yaml
:
template:
- trigger:
- platform: time_pattern
minutes: "*"
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
action:
- service: weather.get_forecast
target:
entity_id: weather.home
data:
type: hourly
response_variable: hourly_forecast
- service: weather.get_forecast
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.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.forecast[:5] }}"
(This code is based on the mentioned GitHub post and will work without any changes needed as long as you are using the weather.home
entity.)
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