Need RESTful JSON Programming Help: How to iterate through objects to build a list in a single entity attribute

So I figured out how to access the JSON endpoint using a rest sensor, and I figured out how to display a value from the first object in the JSON.

Now, I’m trying to figure out how to iterate through the objects to create a list that displays as a single attribute in the sensor.

Here’s the deal. It’s a list of tasks, and the JSON has this format:

{
  "STATUS":  "OK",
  "todo-items": [
    {
       "id": 3497834,
       "content": "This is a task."
    },
    {
       "id": 3497835,
       "content": "This is another task."
    }
  ]
}

So to access “This is a task”, I simply used the following:

- platform: template
  sensors:
    todo_items:
      value_template: '{{ states.sensor.tasks.attributes["todo-items"][0]["content"] }}'

where [0] pulls the first entry.

Now I need to figure out how to iterate through the length of the entire array, increasing each [0] as it loops, and use the values in the “content” item as a list under a single attribute for the sensor and not the state of the sensor.

Ultimate goal? Display of list of tasks that are on the table to be completed.

The resulting sensor would be something like sensor.tasks with a value_template of “OK” and an attribute of tasks: showing a list of those tasks.

Or…if there’s a better way to do this…any ideas or help would be appreciated. Thanks!

Do you have access to the REST server side?

You could create a new GET end point called summary which provides the formatted list as a single entry.

I suppose it depends on what you want to do with the list and if you need access to individual items or if you just want to display the list.

I am limited to a few pre configured endpoints. I was able to template my way to a list of tasks, but haven’t been able to figure out how to filter or sort based upon other key pairs. I’ll post my template when I’m awake.

So I got this far:

{% for i in states.sensor.tasks.attributes["todo-items"] %}
{{i["project-name"]}}
{{ i.content }}
{% endfor %}

Which gives me a list containing the project name then a task. The project name and task are both found at the same level, so I’m wondering how I can group the tasks based upon the project name.

There is a tasks.json endpoint, and there’s also a projects.json endpoint, so I’m wondering how I can use one to group the other by a common field.

1 Like

Not familiar with the script syntax in HA, but if you can create a map and have “append” ability you can group things easily.

By “Map” I mean a set of key->value associations, which you already have with “i” there. So if you could do:

output[ i["project-name"] ].append( i.content )

This would group the items by project name.

You would need to iterate over the keys in “output” were each key would be a project name and the value would be a list of contents. (keys might be referred to as attributes)

Again I don’t know how far you can go in HA script syntax (and I’m out of time to look it up tonight). I can give you pseudo code, but to be honest I think you are heading in the right direction anyway.

The details you provide are a little vague, and seem to change with each post. But if I understand, then I think you could use a python_script to take the data from the one or two REST sensors you currently have and create a single sensor with the information combined, sorted, etc. into the form you want. You’d use an automation to call the script whenever either of the REST sensors changes.

If you’re interested in this possibility, and would like some help, let me know. I’d need more specifics, though.

Phil, I managed to solve the problem. I don’t have the solution on me right now, but I’ll post it later.

It involves using one of the points of data as a group, then iterating through the data to find all instances that match that group.

Hi @arretx I realise this is a very long time after the post, but I wonder if you have your solution available? It sounds very similar to what I’m trying to do!
Thanks!

Unfortunately I’ve lost access to that particular system so I don’t have that code anymore, and I quit using Teamwork.com, although the code would apply to any JSON objects when adapted correctly. I’ve been trying to get it to work with Clickup, but I can’t figure out the Authentication headers to make the connection from HA.

Damn no worries, thanks for replying!
For context, I was just trying to get a nice looking array into a sensor. I’m using grocy and want to get a list of products. I have the products in a string list, but can’t get it to format nicely and so was hoping there’s a better alternative.