Render content from rest_command in Markdown card

Hi, I can’t figure out how to accomplish the following.

If I have a REST command like this:

rest_command:
  food:
    url: "http://example.com/food"

And it returns a simple list like this:

[
    "Macaroni and Cheese",
    "BBQ Baby Back Ribs",
    "Chicken Cacciatore",
    "Cheese Tortellini Pesto",
    "BBQ Brisket Mac & Cheese"
]

How do I get the Markdown card’s content to display it? I’m looking for something like this (obviously this doesn’t work):

type: markdown
title: Food
content: >
  {% for food in rest_command.food %}
    - {{ food }}
  {% endfor %}

Any ideas?

You probably want a restful sensor rather than a restful command. The sensor can store the returned result. Which you can then display in your markdown card.

Ok, here’s my sensor:

- platform: rest
  name: food
  resource: http://example.com/food
  json_attributes_path: "$"

Causing this error in logs:

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.food. State max length is 255 characters.

You need to pick one of the attributes as the state and use a value_template to grab it. The whole lot of data is too big for the state (the 255 character limit does not exist for attributes only the state).

But there are no attributes… it is a list.

This does not work, it is only blank:

- platform: rest
  name: food
  resource: http://example.com/food
  value_template: >
    {% for food in value_json %}
      - {{ food }}
    {% endfor %}

However, if I change {{ food }} to {{ food[0] }} I do indeed get a list of the first letters of each item in the list to appear in the Markdown card, how I can I get it to treat {{ food }} as a string? This doesn’t work either: {{ food | string }}

Can you please post the full (formatted) data returned by your command?

Do you mean the endpoint/resource’s response? It’s exactly as it appears in the first post, it is a simple JSON array.

No, that is not json it’s a list.

It is valid JSON. Keyless lists/arrays are valid JSON. You can verify this yourself by simply pasting the contents of the code block in my first post into any JSON validator online.

I did do that but must have made a copy/paste mistake.

Try this:

- platform: rest
  name: food
  resource: http://example.com/food
  value_template: "{{ 'OK' if value[0] != none else 'unknown' }}"
  json_attributes_path: "$"

Now the Markdown card says “OK”.

You copied my list of food into a JSON validator and it didn’t validate? I tried all of these and it passes:

Those are just the top Google results, did you find one that says my array is invalid JSON? Can you link me?

Because you need to use the sensor attributes not the state. The state can only contain 255 characters, The attributes do not have this limit.

Yes. And then I tried again and it did work, hence I must have

Because you need to use the sensor attributes not the state. The state can only contain 255 characters, The attributes do not have this limit.

I don’t understand, can you please explain this a bit more? How would you go about doing what you’re describing with the data structure I have?

That would depend how the attributes are being imported. Can you show what the attributes are in Developer Tools / States for this rest sensor:

- platform: rest
  name: food
  resource: http://example.com/food
  value_template: "{{ 'OK' if value[0] != none else 'unknown' }}"
  json_attributes_path: "$"

Unfortunately it only shows friendly_name: food in the Attributes column.

Anybody? Is this just something that is outside the scope of built-in HASS behavior? I’ve been going over the documentation for a while now and trying all kinds of stuff but can’t seem to get it working…

1 Like

I’ve been searching for answers for over a week now.

I’m new to Home Assistant but I am a senior software developer.

Is it true that there is no reasonable way to store json arrays?

I would love to show local news, -gigs and so on on my dashboard.