Hi all, any ideas why my morning weather announcement routine stopped working with error “Error rendering data template: UndefinedError: None has no element 0”?
Yes. See “Weather” under this list:
Lots of topics on here about it.
In future, please post code as text, not screenshots. See here:
Thanks for your reply. Sorry for posting the screenshot and not the code but I’m sure volunteers here supporting the full community are tired of hearing “sorry” and thank you" ![]()
I’ve replaced “forecast” with “weather.get_forecasts” but I get the same error.
Here’s my code:
service: notify.alexa_media_kitchen
metadata: {}
data:
message: >-
{% set state = states('weather.openweathermap') %} {% set temperature =
state_attr('weather.openweathermap', 'temperature') %} {% set humidity =
state_attr('weather.openweathermap', 'humidity') %} {% set windspeed =
state_attr('weather.openweathermap', 'wind_speed') %} {% set rain =
state_attr('weather.openweathermap', 'weather.get_forecasts').0.precipitation_probability
%} {% if state == 'partlycloudy' %}
{% set state = 'Partly Cloudy' %}
{% endif %} Good Morning! It looks like today the weather will be {{state}},
with a temperature of {{temperature}} degrees, wind speed of {{windspeed}}
meters per second and a {{rain}}% chance of rain. Enjoy your day!
It’s not as simple as a rename. weather.get_forecasts is a service not an attribute. If you need to work with the forecasts beyond simply using the integration’s weather card display, you’ll need to pull them in to a template sensor as described in the first example here, swapping weather.home for weather.openweathermap:
If you add the following code to that, you should get the entire forecast:
attributes:
forecast: "{{ hourly['weather.openweathermap'].forecast }}"
Then you’ll need to update your announcement to use the rainfall probability attribute from that sensor instead.
Getting a weather forecast seems to be extremely difficult. I’ve tried on and off for months and my automation always fails on trying to read a forecast. I’ve attempted to use various services and settled for OpenMeteo. I’m trying to follow the instruction above, placing a template in configuration.yaml and adding attributes as above but get the error
Logger: homeassistant.config
Source: config.py:354
First occurred: 7:35:29 pm (3 occurrences)
Last logged: 7:35:35 pmInvalid config for ‘template’ at configuration.yaml, line 44: ‘attributes’ is an invalid option for ‘template’, check: attributes
Just the error does not tell us much. If you post your yaml, properly formatted, we’ll be able to tell you what to fix. Or tell us what you want to do with the results. It is not that difficult if you know how. Copying it to an attribute as described above is also not needed in almost all cases. You can ust use an action to get the data for the notification or whatever else you want to do.
It also helps to show the output of the weater action in the developer toos to see if the wether service you use provides the information you want.
Thanks for the clarification Edwin. Here is my (non-working) template:
template:
- trigger:
- trigger: time_pattern
hours: /1
action:
- action: weather.get_forecasts
data:
type: hourly
target:
entity_id: ['Weather Via OpenMeteo']
response_variable: hourly
sensor:
- name: Temperature forecast next hour
unique_id: temperature_forecast_next_hour
state: "{{ hourly['Weather Via OpenMeteo'].forecast[0].temperature }}"
unit_of_measurement: °C
attributes:
forecast: "{{ hourly['Weather Via OpenMeteo'].forecast }}"
I initially had the line beginning attributes: at its start but, on doing a syntax check, I was told that this could prevent the system from booting. Weather Via OpenMeteo is a label I’ve added for the Open_Meteo, OpenMeteo or open_meteo service (don’t know which is the correct syntax!). I’ve read various instructions but find I can’t even understand the basic requirements. Do I need to use an external weather service, or is openweathermap built in?
Incidentally, I looked in the Developer Tools but couldn’t see any reference to any Weather service output, even though OpenMeteo is definitely installed.
Thank you.
The first error I spot is this: ‘Weather Via OpenMeteo’ is not an entity id of a weather service. It should be something like ‘weather.openweathermap’ in all places. You need the actual entity id for your weather entity.
Here the [ and ] should be removed.
The indentation of forecast is slightly too much indented, though this probably isn’t a problem.
So it should look something like this (untested):
template:
- trigger:
- trigger: time_pattern
hours: /1
action:
- action: weather.get_forecasts
data:
type: hourly
target:
entity_id: 'weather.openweathermap'
response_variable: hourly
sensor:
- name: Temperature forecast next hour
unique_id: temperature_forecast_next_hour
state: "{{ hourly['weather.openweathermap'].forecast[0].temperature }}"
unit_of_measurement: °C
attributes:
forecast: "{{ hourly['weather.openweathermap'].forecast }}"
Note that copying the forecast attribute generates a lot of data for the recorder database. This is the reason it was removes as an attribute in the first place. If you do not need it you could consider removing it. If you do use it, you could consider calling the action in that place instead of using the attribute, or make attributes for only the values you actually use.
