Templating Help With State Attributes

Hello, I’m a bit new to templates. I’m trying to achieve Alexa Announce using notify.alexa_media service, but with some state attributes.

For example, I can get this working easily:
message: It is currently {{states.weather.thermostat.state}} and {{state_attr(‘weather.thermostat’, ‘temperature’)}}

But I would also like to include the high/low forecast for today, which appears in state attributes, but it appears with the rest of the week’s forecast like below. How do I call those state attributes that have many with the same title? In the below example, I’d like to call the first set of temperature: and templow:

temperature: -4
humidity: 56
pressure: 1030
wind_bearing: 335
wind_speed: 7
visibility: 16
attribution: ‘Ecobee weather provided by FI:CYND at 2020-11-24 16:02:40 UTC’
forecast:

  • condition: sunny
    temperature: -2
    templow: -8
    wind_bearing: 335
    wind_speed: 7
    datetime: ‘2020-11-24T16:19:45.547530+00:00’
  • condition: snowy
    temperature: 3
    templow: -5
    wind_bearing: 100
    wind_speed: 6
    datetime: ‘2020-11-25T16:19:45.547530+00:00’
  • condition: rainy
    temperature: 5
    templow: 1
    wind_bearing: 94
    wind_speed: 6
    datetime: ‘2020-11-26T16:19:45.547530+00:00’
  • condition: rainy
    temperature: 5
    templow: 2
    wind_bearing: 217
    wind_speed: 3
    datetime: ‘2020-11-27T16:19:45.547530+00:00’
  • condition: rainy
    temperature: 5
    templow: 1
    wind_bearing: 259
    wind_speed: 7
    datetime: ‘2020-11-28T16:19:45.547530+00:00’
    friendly_name: Thermostat

Thanks for bearing with me. :slight_smile:

1 Like

Your formatting is kinda busted, but you can try this.

state_attr('weather.thermostat', 'forecast')[0]['templow']

Basically, we get the forecast attribute. Then we need to parse that. It looks like a list of things, so I’ll parse it like a list. Element 0 should be the first one (most current). Then from there, I get the ‘templow’ value.

If you wanted the next day, you’d use [1][‘templow’]. etc, etc.

Open up your template editor in developer tools and you can try all of this realtime to figure it out.

"{% set forecast = state_attr('weather.thermostat', 'forecast') %}"
"{{ forecast[0]['templow'] }}"

That ‘should’ show -8 on the right side of the window if it all worked like I think. If not, tweak things and give it a shot.

2 Likes

Thank you for this! I didn’t understand how to use the [0] piece to parse it out. This now works as follows, though may be ugly in terms of formatting. Very much appreciate your help.

Good morning! It is currently {{states.weather.thermostat.state}} and
{{state_attr(‘weather.thermostat’, ‘temperature’)}}. Todays high is {{state_attr(‘weather.thermostat’, ‘forecast’)[0][‘temperature’]}} with a low of {{state_attr(‘weather.thermostat’, ‘forecast’)[0][‘templow’]}}. Have a great day!