Extract value from list (convert from old state_attr to new)

Hi all,

Been trying all day to figure this out, reading the documentation and searching on and on.

I am using a REST sensor to gather info about the gas price at my local gas stations.
The REST works perfect, and stores value in a list.
But I cant figure out how to extract only one attribute, inside another attribute.
I got it working using the old way of writing the template, but I cant figure out how to write the correct way.

This line works, and returns the value i want:
{{ states.sensor.gas_station.attributes["stationDetails"][0]["price"] }}

But however I try to write this the new way, i get error messages.

This template gives me the list over StationDetails - but i cant figure out how to only extract i.e price from list 1.

"{{ state_attr('sensor.gas_station', 'stationDetails') }}"

This line, which I thought was the correct one, just gives me an error message:
"{{ state_attr('sensor.gas_station', 'stationDetails', 'price') [0] }}"

This is the entity with the attributes:

Can anyone point me in the correct direction to where the heck I’m going wrong?

The attribute ‘stationDetails’ is a list of dictionaries… ‘price’ is a key within those dictionaries. Since you want the one from the first item in the list, you need put the list index first then extract the value from its price key:

"{{ state_attr('sensor.gas_station', 'stationDetails')[0].price }}"
1 Like

Thank you, for both explanation and solution!