Accessing part of an array

I think im going crazy so i come here looking for sanity.

I have an entity that populates an attribute ‘Origin’ with an array of longitude and latitiude.
It looks like this
origin: 59.3,20.3

In the developer section i then wrote
{{ states.sensor.home_to_dest.attributes.origin }}
[
59.3,
20.3
]
so i know its an array, i wrote
{{ states.sensor.home_to_dest.attributes.origin[0] }}
which, in my mind, should return 59.3 but it just returns 5
if i change the 0 to a 1 then it returns 9, its just returning the number based on position.

So im at a loss really as to how to access index of an array.

can anyone offer some help on this?

As the docs say, try to avoid using the states object itself:

Avoid using states.sensor.temperature.state, instead use states('sensor.temperature'). It is strongly advised to use the states(), is_state(), state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).
source

Does

{{ state_attr('sensor.home_to_dest','origin')[0] }}

work?

I like what youre saying and i did try it, it produces the same.
if you use Waze travel entitiies, that is where these come from

When you enter

{{ state_attr('sensor.home_to_dest','origin') }}

In Developer Tools - Template,
what does it say on the right for the Result type? Please post the result in a code box as well.
(Or post a screenshot)

Hm, weird. Usually entity states are strings, so maybe try this:

{{ (state_attr('sensor.home_to_dest','origin') | from_json)[0] }}
{{ (state_attr('sensor.home_to_dest','origin') | from_json)[1] }}

JSONDecodeError: Extra data: line 1 column 10 (char 9)

i see the issue.
The waze entity is not actually creating an array, when i list the attributes it doesnt encase it as [ , ]
It shows it as a string ‘origin’: ‘59.3,20.3’
other entities list such elements as ‘Location’: [None, None] for example

ill parse it using the comma

bizare as you can see in one of screen shots that the developer display shows the result with square brackets… lead me down a false path :slight_smile:

Ah, that somewhat makes sense. Instead of using the comma you could also cheese it and just add the brackets, but there’s no real benefit I guess:

{{ ("["state_attr('sensor.home_to_dest','origin')"]" | from_json)[0] }}
{{ ("["state_attr('sensor.home_to_dest','origin')"]" | from_json)[1] }}
1 Like

i appreciate the help, thanks

1 Like

Unfortunately the template editor interprets results. So a string that looks like a list will be interpreted as a list. Actual templates don’t do this, so it remains a string.

This is a constant source of confusion yet issues raised about it are ignored. Apparently that is the way the template editor is supposed to work.

I looked at the waze sensor I have and it is not a list.
It’s quite obvious when you look at the attributes and know how HA present them.

The attribute is written as origin: xxx,xxx
That is a string. If it had been a list it would have looked like this:

Here DVD is an array/list with two items.

So if waze would have been a list it would have looked like this:

origin: 
  - xxx
  - xxx

So to use the string I suggest:

{% set gps = state_attr('sensor.home_to_dest','origin').split(",") %} 
{{ gps[0] }}
{{ gps[1] }}

many thanks for your input but stating things are obvious is not helpful.
A string of xxx,xxx can be shown the same as an array.
Ive created many entities that contain location of [ xxx,xxx ] which is an array and is accessed as an array ie. a[0] a[1] but this is shown as xxx,xxx

as you can see here if i print out the entity attributes i see that Location is an array
{‘Administrative Area’: ‘Akershus fylke’, ‘Areas Of Interest’: ‘N/A’, ‘Country’: ‘Norge’, ‘Inland Water’: ‘N/A’, ‘ISO Country Code’: ‘O’, ‘Locality’: ‘O’, ‘Location’: [39.38125, 20.18762205], ‘Name’: ‘place’, ‘Ocean’: ‘N/A’, ‘Postal Code’: ‘2414’, ‘Sub Administrative Area’: ‘N/A’, ‘Sub Locality’: ‘N/A’, ‘Sub Thoroughfare’: ‘5’, ‘Thoroughfare’: ‘len’, ‘Time Zone’: ‘Europe/Oslo’, ‘Zones’: [], ‘icon’: ‘mdi:map’, ‘friendly_name’: ‘Maria IPhone Geocoded Location’}

but is listed in the state list as Location: 39.38125, 20.18762205
just the same as waze in visuals

for anyone wanting to split it and know how i decided to do it…
{{ state_attr(‘sensor.home_to_dest’,‘origin’).split(’,’)[0] }}
{{ state_attr(‘sensor.home_to_dest’,‘origin’).split(’,’)[1] }}

I was then able to feed this into a Rest query to openweather to have dynamic weather for calendar destination so that home people could be notified of what to wear.

1 Like

It is not the same. Look at the location of your json and compare to what developer tools display:

Notice that the ' ' encloses the full string, where as yours is enclosed in [ ] and is float values.
Not the same.

If you can then the format I posted is better since it only does one split and keeps both halves, yours uses twice the resources and throw away half of the result each time.

If you did not find this obvious then it’s obvious you do not

So there is nothing offensive in that comment.

oh my … yes i clearly stated that this is seen in the template and IS the example of an array confirmation.
but when you query the state of the entity (in developer / states), both a string and array are shown the same.

im not going to argue tit for tat. if you dont like what i write and cannot be polite about it then feel free to navigate somewhere else.

1 Like