Need some help with formatting a list from an API

I am pulling some entries from a Google spreadsheet. This is my sensor.

  - platform: rest
    resource: https://... 
    method: GET
    value_template: '{{value_json["values"][0]}}'
    name: "Master ToDo List"
    scan_interval: 1800

And I used Developer Tools > Template to test the output.

{## Imitate available variables: ##}
{% set value_json = {
    "range": "Sheet1!A9:A16",
    "majorDimension": "COLUMNS",
    "values": [
        [
            "Design a light cover. ",
            "EEMAILS ",
            "PRINT THE LINEAR SERVO PARTS AGAIN",
            "DDESIGN PRINTER TOOLS HOLDER",
            "BLOG  ",
            "DESIGN KITCHEN SINK BOARDS",
            "GET FAMILY REUNION PICTURES ",
            "FAN NEXT STEPS"
        ]
    ]
} %}

{{value_json["values"][0]}}

Which gives me…

I dug around and found a similar question but I don’t have the code quite right. Display a list of items - #2 by juaigl
I tried a markdown card and an entity card.

This is the entity card
image

and the markdown is
image

and finally the markdown code…

type: markdown
content: >-
  Items: 
  {% for item in states("sensor.master_todo_list") %}
  * {{item}}
  {%- endfor %}

I would like the list to be somewhere between those two cards. Each element on its own line (or bullet point).

Any help appreciated.

What you are iterating in the markdown card is a string, so the “item” variable gets one char in each loop.

Try adding |from_json filter:

type: markdown
content: >-
  Items: 
  {% for item in (states("sensor.master_todo_list") | from_json) %}
  * {{item}}
  {%- endfor %}

Understood. When I add from_json I get an error. I am doing a little digging into my json formatting. So far I checked it on a json verifier and I get an A OK.

image

Maybe I can parse the string by hand? I will look around.

An entity’s state property can only store a string value of 255 characters. So if your JSON payload’s total length ever exceeds that limit, it will be truncated thereby corrupting its JSON structure.

In contrast, an entity’s attributes property can store up to 16Kb and the data’s type can be a string, list, dict, boolean, etc. That means your data’s list structure will be preserved and your Markdown card’s template will work correctly (although it will need to use the state_attr() function).

I recommend you modify your REST Sensor’s configuration to store your spreadsheet data in an attribute. Refer to the example in the documentation.

OK so looking at the docs… I see how the attributes are defined in json_attributes and how the path is defined. I just don’t understand what the $ syntax means.

It’s part of the JSONpath syntax.

The documentation suggests using the following tool to test and learn JSONpath.

That’s helpful. I did experiment with the tool and the $ didn’t make a difference if it was there or not. But I did find a reference for the JSONPath syntax. I will just drop it here in case it helps anyone else.