How to extract elements from a 'list'?

An integration provided entity has an attribute that Developer Tools > Template reports as being of type 'list:

How can I access the individual list items? I read the Jinja documentation and it suggests that I can just use normal array indexing syntax:

{% set mylist = state_attr('theentity','the attribute') %}
{% set item = mylist[0] %}
{{ item }}

But then I get:

which is the first character of the list rendered as a string. I’ve tried using the subscript syntax in different ways but nothing I have tried seems to work. I’ve searched for info on this but have found nothing. This stuff must be documented somewhere but I haven’t been able to find it.

Can someone explain how to do what I want to do, or better still point me to relevant documentation?

No… the first character is a square bracket, your output is a opening parenthesis.
From what you have posted so far it is impossible to help, it does not seem to be the same entity/attribute or something else that is not right.

What does this give you:

{% set mylist = state_attr('theentity','the attribute') %}
{% set item = mylist[0:5] %}
{{ item }}

well no. When HA renders this list as a ‘string’ (for example in an entity card) the string representation is ‘(51.387683997237, -0.487248867841726)’, hence the opening parentheses.

In order to run what you asked I had to amend things as follows (add an explicit cast to ‘string’) as otherwise I get the error that ‘float’ is not subscriptable:

{% set mylist = state_attr('theentity','theattribute') | string %}
{% set item = mylist[0:5] %}
{{ item }

and then the output is:

(51.3

What is the output from this, is it a string ?:

{% set mylist = state_attr('theentity','the attribute') %}
{{ mylist }}

What if you force it to a list then?

{% set mylist = state_attr('theentity','the attribute') | list %}
{% set item = mylist[0] %}
{{ item }}

To clarify what is happening, first some facts: the state of any entity is always a string. An attribute of an entity can be any type. A jinja template always returns a string.

Now where it gets complicated: if you put a template in the template tester, or if you use a template to specify an attribute, then HA will try to convert the string to a number or a compound type if it can.

So from what is shown above, the template editor is saying something is a list, but from list[0] being the character ‘(’ you can tell that the template editor saw samething in a string that it could convert to a list. To illustrate, put this in the template tester. It is clearly a template returning a string (that looks like a data type known as a tuple, which is very similar to list with 2 elements):

{{ "(12, 34)" }}

The output in the right panel states this is a list, which is what HA converted the string to:

[
  12,
  34
]

Now this, which is the first element of the same string inside the template:

{{ "(12, 34)"[0] }}

Shows:

(

While what you all were expecting to see:

{{ [12, 34][0] }}

Shows:

12

Which is the same result that you would get from:

{{ (12, 34)[0] }}

Unfortunately, it isn’t so straightforward that you can convert the string to the tuple easily yourself:

{{ "(12, 34)" | list }}

Will get you a list of characters:


[
  "(",
  "1",
  "2",
  ",",
  " ",
  "3",
  "4",
  ")"
]

Your best option (as far as I can tell) is to remove the braces, split the result using a comma and convert the parts to a number or use a regular expression.

PS. Put only one of those templates in the tester at the time. Otherwise it is a string with multiple lines that it may not be able to convert to anything else.

1 Like
{% set mylist = state_attr('theentity','the attribute') | from_json %}
{{ mylist[0] }}

from_json takes objects that are strings and represent json objects, and turn them into usable objects in code. There’s a chance this does not work, you may need to replace ( with [ and ) with ].

What’s odd is that you’re claiming this is coming from an attribute, however attributes are typically typed. Meaning it should already be a list and not a string.

That’s not going to work, that’s going to create a list of characters, 1 item per letter.

@Edwin_D @petro Thanks for clarifying the (confusing) behaviour.

So, rephrasing my original question; if the entity returns a string of the format

(51.387683997237, -0.487248867841726)

How can I easily parse that to extract the two numeric values (as strings or floats, either is fine)? I believe that HA templates / Jinja has various string manipulation capabilities but I’m having a hard time finding any kind of comprehensive documentation on them. If this was C, or Java, or a shell script I could do it easily.

Did you see what I posted above? That explains what you have to do.

A rather cluncky way, but it works:

{% set coordstr = "(12, 34)" %}
{% set list = (coordstr | replace('(','') | replace(')','')).split(',') %}
{% set lat = list[0] | float(0) %}
{% set lon = list[1] | float(0) %}
{{ lat }}
{{ lon }}

or what petro suggested:

{% set coordstr = "(12, 34)" %}
{% set list = coordstr | replace('(','[') | replace(')',']') | from_json %}
{% set lat = list[0] %}
{% set lon = list[1] %}
{{ lat }}
{{ lon }}

@Edwin_D @petro Thanks. I will use the JSON route.

@petro said

What’s odd is that you’re claiming this is coming from an attribute, however attributes are typically typed. Meaning it should already be a list and not a string.

I’m only going by what I can ‘see’ when I use it. How can I check the actual type of the attribute? Developer Tools > States does not show the types for attributes.

{{ state_attr('sun.sun','elevation') | typeof }}
1 Like

@petro for the attribute in question the returned type is ‘str’, which I presume means ‘string’.

What is creating that entity?

The iCloud3 integration (it is an attribute of a device_tracker entity).

ah ok, that makes sense. It’s a custom integration and likely not following HA standards.

I figured it was lat/long when you posted this Topic, but im don’t feel “comfortable” in either list strings or templates, so i wouldn’t commend
But Have you tried " http://homeassistant.local:8123/hacs/repository/131915802 " ? This HACS integration, it uses OpenStreetMap database, as HA, but generates map-links to Google, Apple and OpenStreetMap

Thanks, that looks interesting. I will have a play with it!

BTW, if it is iCloud3 integration, seems you are using gps attribute. Why not using directly latitude and longitute attributes that are readily available?