I am trying to get the attribute forecast → wind_speed (blue arrow). I can get the wind_speed (red underline) by:
{{state_attr('weather.ksgr_daynight', 'wind_speed')}}
But not sure how to get the forecast wind speed. Thanks in advanced.
I am trying to get the attribute forecast → wind_speed (blue arrow). I can get the wind_speed (red underline) by:
{{state_attr('weather.ksgr_daynight', 'wind_speed')}}
But not sure how to get the forecast wind speed. Thanks in advanced.
The forecast
attribute contains a list. Lists are zero-based which means the index of the first item in the list is 0
(not 1
). Each item in the forecast
list is a dictionary containing 8 keys and you want the key named wind_speed
.
{{ state_attr('weather.ksgr_daynight', 'forecast')[0].wind_speed }}
Correction. Removed trailing single-quote and parentheses.
Thanks Taras. I hadn’t figured out the [0] part of the construction yet. (Obvious, in retrospect.)
However, I think there’s an extra ’ at the end of wind_speed.
Yes, in my haste, I overlooked to delete it. Thanks for bringing it to my attention; fixed now.
Thanks. Also, it wasn’t working for me until I deleted the last “)”
{{ state_attr('weather.ksgr_daynight', 'forecast')[0].wind_speed }}
That’s what happens when I supply answers late at night or before breakfast.
This may be obvious, now that Taras has enlightened us, but you can also use a similar construction to get the 2nd, 3rd, 4th, etc. day’s forecasts as follows:
{{ state_attr('weather.ksgr_daynight', 'forecast')[1].wind_speed }}
{{ state_attr('weather.ksgr_daynight', 'forecast')[2].wind_speed }}
{{ state_attr('weather.ksgr_daynight', 'forecast')[3].wind_speed }}
FWIW, to make it more robust, I would advise to first check if the forecast
attribute contains a list and determine how many items it contains, especially if the goal is to access more than just the zeroth item.
Most of the time, what you and I have suggested will work because forecast
contains a list with several items (and probably with a consistent quantity of items). However, that one time when something goes wrong and forecast
is empty will cause the template to fail with an error message (because the template will be attempting to access items in a non-existent list).
Excellent point. It’s always good to check for the odd exception.
so how do i get that into a card…
showing me the last few items
You can’t, cards don’t have the ability to get sub-attributes. You have to create template entities to extract the information.