@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 !
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.