How to insert some code inside a table on a markdown card

Some Markdown Resources:

Markdown in general look here
Markdown Tables look here
Markdown Table Generator look here

I had been trying without success to insert the output of the below code into a table so it is a little better presented.

Here is the code as it was being used on a markdown card:

      - type: markdown
        title: Last Cleaned
        content: |        
       
          {% for state in states.input_datetime -%}        
          {%- if loop.first %}
          {% elif loop.last %}
          
                   
          {% else %}

          
          {% endif -%}
         
          {{ state.name | capitalize }} - - - - **{{ state.attributes.day }}/{{ state.attributes.month }}/{{ state.attributes.year }}**
          {%- endfor %}

          ### Daily Action:

          Empty Flooded Pipe Settling Tank
          Empty Pond Skimmer Basket

As an example…below is what I am looking for in a table:

| System Name | Last Cleaned |
|:-----------:|:------------:|
|      xx     |      xxx     |
|      xx     |      xxx     |

So I tried modifying my code to include some markdown:

      - type: markdown
        title: Last Cleaned
        content: |        
          | System Name | Last Cleaned |
          |:-----------:|:------------:|       
          {% for state in states.input_datetime -%}        
          {%- if loop.first %}
          {% elif loop.last %}
          
                   
          {% else %}

          
          {% endif -%}
         
         | {{ state.name | capitalize }} | **{{ state.attributes.day }}/{{ state.attributes.month }}/{{ state.attributes.year }}** |
          {%- endfor %}

          ### Daily Action:

          Empty Flooded Pipe Settling Tank
          Empty Pond Skimmer Basket

However that did not work as intended. It did seem to work to produce the headings for the table but each row just printed a pipe character beside the output of the loop:

So I played around with the code in the template editor and realised I can get a pretty good representation of a markdown table by stripping out the formatting I had appplied to get some whitespace into my initial markdown card:

CODE:

{% for state in states.input_datetime -%}
{%- if loop.first %}
{% elif loop.last %}
{% else %}          
{% endif -%}
| {{ state.name | capitalize }} | {{ state.attributes.day }}/{{ state.attributes.month }}/{{ state.attributes.year }} |
{%- endfor %}

OUTPUT:

| Solids settling | 3/12/2019 |          
| Flooded pipe | 2/12/2019 |          
| Shadehouse final bio | 27/9/2019 |          
| Shadehouse first bio | 24/10/2019 |          
| Pump area final bio | 1/1/1970 |          
| Pump area first bio | 12/11/2019 |          
| Sludge clarifier | 27/9/2019 |          
| Sludge screen | 22/11/2019 |          
| Pump area swirl | 2/6/2019 |          
| Shadehouse swirl | 31/10/2019 |
| Solids settled | 27/11/2019 |

Next step was a bit of a ‘Eureka Moment’ which works not too bad after I added a couple of pipe characters in the middle section of the code as I noticed that the code produced a blank row in the table just under the headings:

      - type: markdown
        title: Last Cleaned
        content: |        
          | System Name | Last Cleaned |
          |:------------|-------------:|      
          |{% for state in states.input_datetime -%}     
          {%- if loop.first %}
          {% elif loop.last %}
          {% else %} 
          {% endif -%}| |     
          |  {{ state.name | capitalize }}  |  {{ state.attributes.day }}/{{ state.attributes.month }}/{{ state.attributes.year }}  |
          {%- endfor %}

          ### Daily Action:

          Empty the Flooded Pipe Settling Tank
          Empty the Pond Skimmer Basket

After that I also left-aligned the first column and right-aligned the second column and ended up looking like the below.

Cheers!

1 Like

if you want an actual table. you need this format

x | y |
---|---|
value| value|
value| value|

which will look like this

x y
value value
value value

EDIT: And for your solution, this could be used to get around all the ‘issues’ you had.

          | System Name | Last Cleaned |
          |:------------|-------------:|      
          {%- for state in states.input_datetime %}
          {{ '|{}|{}/{}/{}|'.format(state.name | capitalize, state.attributes.day, state.attributes.month, state.attributes.year) }}
          {%- endfor %}
1 Like

had the same challenge yesterday… Hard time creating a table in Markdown

still not all too obvious and seems rather backwards having to fiddle with these characters, spaces and fake tables… and thats for the table alone. The true challenge arrives when you want to style the content of those tables. duh.

maybe we should move on and find a more modern way of representing, well, tables…

Im in the process of experimenting with a few nested custom cards, but by no means there yet…

1 Like

That’s markdown though… this has nothing to do with home assistant. If we get something different, then you’d have to update all your markdown stuff to the new system.

1 Like

Thanks very much @petro I’ll be trying that and report back my findings!

EDIT: Of course your solution works perfectly and looks so much better than what I had managed to arrive at. Cheers!

I’m a little confused though with this:

Why are the pipe characters missing on the left side?
I see the difference in the result but I have not seen that documented anywhere?

dont get me wrong, I like the markdown card very much, especially the way it allows us to use native templates in HA, for which it still is a unique card!

Tables otoh are a different beast altogether in markdown, and for that Ive searched another solution for the moment. Until I get better in Markdown tables…

1 Like

I agree with your sentiments but would like to add that I am just ecstatic that a ‘built in’ part of HA works so well ‘out of the box’. This functionality is very useful to me and I will now be making quite a few other similar markdown cards to better monitor whats going on.

I’m not a fan of ‘Custom’ things in my HA system as it controls/monitors what to me are ‘mission critical’ systems that MUST continue to work reliably.

Avoiding ‘Custom’ stuff seems to have helped me avoid major problems.

They aren’t required but you can add them if you want.

I’m not sure, you’d probably have to look outside home assistant to get the documented functionality as markdown is a world-wide format.

1 Like