Lovelace markdown card with template variables

Good evening to the community.

I have been trying to get the markdown card working most of the day but after trying adjusting everything I can think of and searching all through the forum posts it seems nothing will give me the clues I need to get this working.

Initially I tried to setup my own arrangement but when that kept failing I just tried to setup exactly as per the example (except for substituting my entities):

type: entity-filter
entities:
  - light.tank
  - light.patio
  - light.side1
card:
  type: markdown
  content: |
    The lights that are on are:
    {% for l in config.entities %}
      - {{ l.entity }}
    {%- endfor %}

    And the door is {% if is_state('binary_sensor.door', 'on' %} open {% else %} closed {% endif %}.

This returns errors or simply the content as a string IE:

The lights that are on are:
{% for l in config.entities %}
  - {{ l.entity }}
{%- endfor %}

And the door is {% if is_state('binary_sensor.door', 'on' %} open {% else %} closed {% endif %}. 

depending on how I adjust the config file.

Initially I was trying to add this as a new card in an existing view but that was so problematic I tried setting up a new view but that was equally problematic.

It seems to me it’s possibly a syntax problem so if I was adding it to this view as a new card:

  - title: Solids
    cards:
      - type: glance
        title: Backwash Tanks
        entities:
          - entity: sensor.backwash_settling_level_storage
            name: Settling          
          - entity: sensor.backwash_settled_level_storage
            name: Settled                 
          - entity: binary_sensor.backwash_tank_hi_lvl
            name: Settled HI LVL             
          - entity: binary_sensor.backwash_tank_lo_lvl
            name: Settled LO LVL

How would it need to be added with the correct syntax?

I would like to add this as a first test. (Is the entity filter required though?):

      - type: markdown
        title: Batch System
        content: |
          Batch Auto/Pause Status is {% if is_state('binary_sensor.batch_auto_pause_status', 'on' %} ON {% else %} OFF {% endif %}.

That returns a new Card with no errors but…

LovelaceMarkdownCard%20(2)

The template variable is printed as a string it seems?

Cheers in advance for any help!

1 Like

I’ve just spent an hour on this too when I noticed there is a typo in the documentation! A missing ) for is_state function.
Try this:

Batch Auto/Pause Status is {% if is_state('binary_sensor.batch_auto_pause_status', 'on') %} ON {% else %} OFF {% endif %}.

If there is a single error in your code, whole rendering gets broken, so take it one step at a time starting from a simplest thing and go incrementally.
I started with {{now().hour}} and went from there.

Also, a much better place to experiment with templating is the template tab under dev tools.
I have submitted a PR, to fix the typo, so no one else trips up on this.

good luck.

1 Like

Thanks for the reply @SergejPopov .
Aaaargghhh a typo eh! I have tested all along in the template editor and it is obviously much more forgiving it seems?

Just now tested this:

      - type: markdown
        title: Batch System
        content: |
          The Batch Auto/Pause Status is
          {% if is_state('binary_sensor.batch_auto_pause_status', 'on') %} ON
          {% else %} OFF
          {% endif %}

and as expected it returns:

The Batch Auto/Pause Status is
           ON

However when I test again in my config for a new card (starting from the previously configured card on the view) like this:

      - type: entities
        title: Last Cleaned
        show_header_toggle: false
        entities:
          - entity: sensor.settled_last_cleaned
            name: String
          - entity: input_datetime.only_date01 
            name: SETTLING
          - entity: input_datetime.only_date02 
            name: Flooded Pipe 

      - type: markdown
        title: Batch System
        content: |
          The Batch Auto/Pause Status is
          {% if is_state('binary_sensor.batch_auto_pause_status', 'on') %} ON
          {% else %} OFF
          {% endif %}

I get the Markdown card with the markup all displayed as a string?
Any ideas? I’m nearly done with this…is it working for you reliably?

On its own in a separate view maybe?

I just ran your content through the Templating Developer tool and it seems to execute ok. I get.

The Batch Auto/Pause Status is
OFF

Perhaps try using a ‘>’ as opposed to ‘|’ after the ‘content:’ ? This is what I typically use for templates.

1 Like

Cheers for checking @micque …Yes I wondered about the pipe versus the bracket? There seems to be no documentation about using either for this example in the markdown card?

EDIT…what version of HA are you using? I’m using 0.94.0 in a docker on a QNAP NAS.

This is a YAML syntax, but I do think this should be addressed in the HA documentation somewhere.

https://yaml-multiline.info/

1 Like

Tried this…produced this:

Using this:

  - title: Markdown
    cards:
      - type: markdown
        title: Batch System
        content: |
          {{now().hour}}

Could someone post a lovelace config snippet they have which is working possibly?

Cheers!

@MatthewFlamm ok so now I know a little more about YAML syntax…cheers for the link!

When I said “Just now tested this:”

I posted an incorrect example of what I actually tested in the template editor…I actually tested:

          The Batch Auto/Pause Status is
          {% if is_state('binary_sensor.batch_auto_pause_status', 'on') %} ON
          {% else %} OFF
          {% endif %}

That executes in the template editor.
However the config for the Markdown Card does not execute at all. It merely prints a string.

Can I ask has anyone in the community actually executed template variables in a markdown card?
Could you post an example of your successful config snippet please?

Cheers?

1 Like

So the big question is:

can variables / template syntax, be used in a Lovelace card??

1 Like

Sure for cards that support it. e.g. the markdown card discussed above does:

content
(string)(Required)
Content to render as markdown. May contain templates.

For cards that don’t support templating there’s this:

1 Like

@tom_l Thanks for posting. Is it possible for you to post a snippet that you know works for my reference please?

using the markdown card?

There are many examples in the link I posted.

EDIT: also a recent solution using it:

Yes I have tried quite a few of those examples but just to have another test I used.

      - type: markdown
        title: Template Example
        content: >
          Sensor States          
          

          Test template value example which does not execute however?


          {% for state in states.sensor %}
            {{ state.entity_id }}={{ state.state }},
          {% endfor %}

As I expected (as I have tried this particular example and many others before) that produces this (using the markdown card):

I believe the Markdown Card may not actually support templates on the current configuration I am using?

Home Assistant
0.94.0

Installed on QNAP NAS using container station.

Working for me on Hassio NUC image v0.99.3:

It does weird things depending on the number of spaces in the padding.

Try this:

type: markdown
content: >
 {% for state in states.sensor %}
   {{ state.entity_id }}={{ state.state }},
 {% endfor %}
title: Test

Hmmm… @tom_l I suspected this has to do with the configuration I am using.
Cheers for confirmation!

It seems I might need to make the move to a later version.
I will do that and post again what I find. May be a day or so as I have a bit on atm.

See my edit above. Depending on the padding strange things happen (different font, or not working at all).

Are you using Lovelace UI or yaml mode?

@tom_l I had someone else say that sort of thing above so I tested your example.

      - type: markdown
        content: >
         {% for state in states.sensor %}
           {{ state.entity_id }}={{ state.state }},
         {% endfor %}
        title: Test

which unfortunately produced this:

I use yaml mode…

Try incrementing the number of spaces. Also the middle line of your template should be 2 spaces more than the other two.
Also try this:

type: markdown
content: |2-
   {% for state in states.sensor %}
     {{ state.entity_id }}={{ state.state }},
   {% endfor %}
title: Test

The middle part is 2 spaces but when it prints the string it does not display that way. How many spaces should I try do you think?

Haha…do these messages ever annoy you as much as they annoy me? (Pops up as I type this to reply to you)

Encourage everyone to get involved in the conversation

You’ve replied 3 times to @tom_l in this particular topic!

A great discussion involves many voices and perspectives. Can you get anybody else involved?

And don’t forget, if you’d like to continue your conversation with this particular user at length outside of public view, send them a personal message.