REST sensor - how to reference top level array in response?

I am trying to fetch data from an API in to a sensor:

  - name: Todoist Labels
    platform: rest
    resource: https://api.todoist.com/rest/v2/labels
    method: GET
    headers:
      Authorization: !secret todoist_api_token
    value_template: "{{ value_json }}"
    #json_attributes_path: "$[*]"
    json_attributes:
      - name
      - color
    scan_interval: 20

This seem to get the correct data, which you can see in the developer tools:

The data is there in the State column, but I can’t get all of it to show in the Attributes, and then I can’t use the full list. My problem seems to be that I would need a way to reference the top level of the JSON, but it doesn’t have a name! And I would need that name later in order to use these values elsewhere.

I tried a few variations with $[*] but nothing seems to work. Anybody can help?

json_attributes_path wants a single object. Did you try $[0] ?

1 Like

With

json_attributes_path: "$[0]"

the attributes stay the same as shown above - only the first label (blue) is included…

You could load the whole output into the attribute
An example (i just did a copy/paste of one of mine):

  - platform: command_line
    name: testevents
    command: >
         echo "{\"events\":" $(
         curl 
         -s 
         'https://api.fingrid.fi/v1/variable/336/events/json?start_time={{ (now()-timedelta(hours=4)).strftime('%Y-%m-%dT%H:%M:%SZ') }}&end_time={{ (now()+timedelta(hours=12)).strftime('%Y-%m-%dT%H:%M:%SZ') }}'
         ) "}" 
    value_template: > 
        {{ value_json.events | length }}
    json_attributes:
        - events 
1 Like

Ah, there multiple objects in the returned array, and you want all attributes of all objects extracted?
Not possible like that. As I said json_attributes_path: expects an object, not an array.

Not really sure what is your expected result, either. name.1: Blue, color.1: blue, name.2: ...?
If you want, e.g., a list of labels separated by comma in the state, that could likely be done through templates (unless that would result in > 255 chars, ofc)

1 Like

@koying thanks fir your reply. What I want to get, instead of


[
        {
                "id": "2166121670",
                "name": "Blue",
                "order": 1,
                "color": "blue",
                "is_favorite": false
        },
        {
                "id": "2166121659",
                "name": "Green",
                "order": 2,
                "color": "green",
                "is_favorite": false
        }
]

is simply a name for that same list, so I can use it from a foreach loop in Javascript (I am coding a custom card). Something like this:

{
	"labels": [{
			"id": "2166121670",
			"name": "Blue",
			"order": 1,
			"color": "blue",
			"is_favorite": false
		},
		{
			"id": "2166121659",
			"name": "Green",
			"order": 2,
			"color": "green",
			"is_favorite": false
		}
	]
}

I was able to use @vingerha 's answer to good effect. This is how it looks:

  - platform: command_line
    name: test_label_colors
    command: >
         echo "{\"label_colors\":" $(
           curl -s https://api.todoist.com/rest/v2/labels -H "Accept: application/json" -H "Authorization: Bearer 435aeeeaaeae6caeeaaeaeeaeaaeaeaeaee0"
         ) "}" 
    value_template: > 
        {{ value_json.label_colors | length }}
    json_attributes:
        - label_colors 
    scan_interval: 200

(That’s not my real token, of course).

With this I get success :tada: !

Extra security tip: in order to move that token into secrets.yaml, you can’t just move the token itself, you have to move the entire command, so that in the sensor definition you change the command to this:

    command: !secret todoist_cmd_with_api_token

And in secrets.yaml

todoist_cmd_with_api_token: 'echo "{\"label_colors\":" $(curl -s https://api.todoist.com/rest/v2/labels -H "Accept: application/json" -H "Authorization: Bearer 435aeeeaaeae6caeeaaeaeeaeaaeaeaeaee0") "}" '

I had to put it all in one line to get it to work, but that’s probably just me being clumsy with YAML (as usual).

Thanks for all the help, guys, I really appreciate it. :+1: