#json array - how to address individual fields?
1 messages ยท Page 1 of 1 (latest)
Seems we have the same questions huh ;)
Not really ๐ but you need to create wat tommmii has ๐
rest:
- resource: http://resource.ip:port/api/GetPersons?count=2
headers:
the-key: thvre5642vhre6254vre
scan_interval: 60
sensor:
- name: "rest_get_last2_persons"
This is my sensor
Thx for the help. I'm just toying with an integration thats giving me a sensor with a lot of attributes. Each value is mm rain. Incrementally for each 5 min.
this populates with :
[
{
"number": "0113",
"name": "John",
"time": "2022-02-25T10:31:31.023"
},
{
"number": "0114",
"name": "Jane",
"time": "2022-02-25T10:31:26.103"
}
]```
Now I would need to read out the person's names
Extracting max value and calculate that time is going to work I think.
pesudo code : person[0].name would result in John
correct
how ?
Imma head out for now. Good luck figuring it out Tommmii
Sorry, this thread with 2 different topics going at once made it even more complicated
ah, nice
sensor.rest_get_last2_persons.name ?
You are not beign clear
where do you see this data? Is it the state of the sensor, an attribute?
Yes that's why I'm not gonna bother atm
in 
{{
states.sensor.rest_get_last2_persons.state
}}
that shows the above json
Should I first of all change the sensor config ?
{% set data = states('sensor.rest_get_last2_persons`) | from_json %}
{{ data | map(attribute='name') | list }}
that should give you a list of names
[ 'Jane', 'John' ]
Well, other way around though
ok, but what if I just want to access the 2nd name? or the 2nd time ?
{{ data[1].name }} for the second name (there are 2 items in this list, and counting starts at 0)
hmm...so map() is effectively creating (mapping) attributes ?
Would it not be more elegant to do the mapping as part of the sensor config ?
Well, normally map() maps values to something. so {{ ["1", "5", "a"] | map("int", default=0) | list }} will result in [ 1, 5, 0]
it has multiple purposes ๐
ok, lots of stuff to experiment with... thanks !
I'll drop this here, it looks to be the most elegant solution to my goal :
https://www.home-assistant.io/integrations/sensor.rest/#fetch-multiple-json-attributes-and-present-them-as-values
Specifically the 4th example, the one with multiple bedrooms each with multiple datapoints.
You never explained your goal.. That's what made it so difficult