Json_attributes with list?

Hi
I need help with a rest sensor with json_attributes.
I have a page that’s answer something like this:

[{
    "firstName": "John",
    "lastName": "G",
    "age": 26
},
{
    "firstName": "Daniel",
    "lastName": "H",
    "age": 25
},
{
    "firstName": "Eric",
    "lastName": "F",
    "age": 23
}]

I want the information in a sensor in Hass. So I tried the rest sensor like this:

- platform: rest
  resource: http://192.168.1.5:8081/alerts/all.json
  name: people
  scan_interval: 600
  timeout: 10
  value_template: '{{ value_json | length }}'
  json_attributes:
  - [0]
  - [1]
  - [2]

But the values from json_attributes are not working. Anyone has any idea?

This is a limitation of the platform. Json_attributes has to point to a dictionary. If you have control of the source, change it to output like this:

{
    "people": [{
        "firstName": "John",
        "lastName": "G",
        "age": 26
    },
    {
        "firstName": "Daniel",
        "lastName": "H",
        "age": 25
    },
    {
        "firstName": "Eric",
        "lastName": "F",
        "age": 23
    }]
}

Then…

- platform: rest
  resource: http://192.168.1.5:8081/alerts/all.json
  name: Kismet alerts
  scan_interval: 600
  timeout: 10
  value_template: '{{ value_json.people | length }}'
  json_attributes:
  - people

If you can’t control the source, you might have to use a different platform… use the command_line sensor instead

- platform: command_line
  name: Kismet alerts
  command: >-
    curl http://192.168.1.5:8081/alerts/all.json> /dev/null
    | jq '{"people":[.[] | {"lastName": .lastName, "firstName": .firstName, "age": .age}]}'
  value_template: >-
    {{ value_json.people| length }}
  json_attributes:
    - people

Thx for the fast answer.

I used command sensor instead:

- platform: command_line
  name: People
  scan_interval: 300
  command: >-
    echo "{\"people\": $(curl -X GET --no-buffer --silent --max-time 10 -H 'User-Agent: Home Assistant' -H 'Content-Type: application/json' -w ', "response": %{response_code}' http://192.168.1.5:8081/alerts/all.json)}"
  value_template: "{{ value_json.people | length | int(0) }}"
  json_attributes:
      - people
      - response
  command_timeout: 30
1 Like

that’s doing nothing.

1 Like