#json array - how to address individual fields?

1 messages ยท Page 1 of 1 (latest)

deft current
zinc terrace
#

Seems we have the same questions huh ;)

jade swift
#

Not really ๐Ÿ™‚ but you need to create wat tommmii has ๐Ÿ™‚

deft current
#
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

zinc terrace
#

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.

deft current
#

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

zinc terrace
#

Extracting max value and calculate that time is going to work I think.

deft current
#

pesudo code : person[0].name would result in John

jade swift
#

correct

deft current
#

how ?

zinc terrace
#

Imma head out for now. Good luck figuring it out Tommmii

jade swift
#

Sorry, this thread with 2 different topics going at once made it even more complicated

#

ah, nice

deft current
#

sensor.rest_get_last2_persons.name ?

jade swift
#

You are not beign clear

jade swift
zinc terrace
deft current
#

in devtools

{{
states.sensor.rest_get_last2_persons.state
}}

that shows the above json

#

Should I first of all change the sensor config ?

jade swift
#
{% 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

deft current
#

ok, but what if I just want to access the 2nd name? or the 2nd time ?

jade swift
#

{{ data[1].name }} for the second name (there are 2 items in this list, and counting starts at 0)

deft current
#

hmm...so map() is effectively creating (mapping) attributes ?
Would it not be more elegant to do the mapping as part of the sensor config ?

jade swift
#

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 ๐Ÿ™‚

deft current
#

ok, lots of stuff to experiment with... thanks !

deft current
#

Specifically the 4th example, the one with multiple bedrooms each with multiple datapoints.

jade swift
#

You never explained your goal.. That's what made it so difficult