How would one parse this attribute list with a template to pick a value out?

QUESTION

How do I format the template to pull the values from the items section?

See the explanation if you can help me figure this out. Thanks!

I’m using a RESTful sensor to GET data from Todoist which appears as a single sensor called sensor.household.

This is the sensor configuration:

- platform: rest
  name: Household
  method: GET
  resource: "https://api.todoist.com/sync/v9/projects/get_data"
  params:
    token: !secret todoist_api_token
    project_id: 2297623587
  value_template: "{{ value_json['project']['id'] }}"
  json_attributes:
    - project
    - items
  scan_interval: 30

In the attribute column for this entity (sensor.household), there is first a JSON object followed by a List. It looks exactly like this:

project: 
child_order: 3
collapsed: false
color: blue
id: '2297623587'
is_archived: false
is_deleted: false
is_favorite: false
name: Household
parent_id: null
shared: true
sync_id: '9907214'
view_style: board

items: 
- added_at: '2022-08-31T21:07:45.518643Z'
  added_by_uid: '8740868'
  assigned_by_uid: '8740868'
  checked: false
  child_order: 1
  collapsed: false
  completed_at: null
  content: This is a Test Task
  description: This is a test description.
  due:
    date: '2022-09-01'
    is_recurring: false
    lang: en
    string: Sep 1
    timezone: null
  id: '6133868890'
  is_deleted: false
  labels: []
  note_count: 1
  parent_id: null
  priority: 1
  project_id: '2297623587'
  responsible_uid: '8740868'
  section_id: '99230498'
  sync_id: '6134017289'
  user_id: '8740868'

friendly_name: Household

When running a template in the Dev tools, I can do the following:

{% set value_json = state_attr('sensor.household','project') %}
{{ value_json.name }}

The results = “Household”

If I change the template to this:

{% set value_json = state_attr('sensor.household','items') %}
{{ value_json.description }}

Despite there being a “description” of the item, I get no result. I suspect it is because the data under “items” is a list, and the data under projects is not.

QUESTION

How do I format the template to pull the values from the items section?

items is a list containing a single item. A list is indexed starting with zero so this should get the ‘zeroth’ item in the list.

{% set x = state_attr('sensor.household','items') %}
{{ x[0].description }}

YES!!! That’s the syntax I was looking for! Thanks!

{% set value_json = state_attr('sensor.household','items') %}
{{value_json[0].description}}

= “This is a test description”

1 Like