Custom Button Card -> tap_action -> markdown

EDIT: I’m still interested in the answer to this but is so often the case in HA dashboards, if there is a problem, the answer is usually custom:button-card

I just replaced my markdown card with a button-card and all is good.


I think I’ve found an issue with using the markdown card in a custom:button card.

I should add here that this is also using lovelace_gen which might complicate things…

The problem is very simple to describe.

I don’t seem to be able to pass a variable into a markdown card without getting the dreaded

This is the first time I have had any trouble doing this kind of thing and I do it a lot in my config in other cards so I am just wondering if this is indeed a limitation and I should stop banging my head against the wall or am I somehow doing something wrong?

As simply as I can (and removing anything irrelevant e.g. styling) I have this:

type: entities
entities:
  - type: custom:hui-element
    card_type: horizontal-stack
    cards:
      - !include
          - /config/lovelace/dashboard_home/sonos/sonos_button.yaml
          - room: kitchen


sonos_button.yaml

Firstly to demonstrate the principle - this works

type: custom:button-card
entity: media_player.sonos_{{ room }}
tap_action:
  action: fire-dom-event
  browser_mod:
    service: browser_mod.popup
    data:
      title: SONOS - {{ room }}
      content:
        type: vertical-stack
        cards:
          - type: entities
            entities:
              - type: custom:hui-element
                card_type: custom:mini-media-player
                entity: media_player.sonos_{{ room }}

              - type: custom:hui-element
                card_type: markdown
                content: >
                  TEST

              - entity: sensor.sonos_{{ room }}_lyrics
                type: attribute
                attribute: plainLyrics

All of these produce the same “Unknown error” error:
(I have also tried moving the markdown into a separate file and including it but that also didn’t work.)

        type: vertical-stack
        cards:
          - type: entities
            entities:
              - type: custom:hui-element
                card_type: custom:mini-media-player
                entity: media_player.sonos_{{ room }}

              - type: custom:hui-element
                card_type: markdown
                content: >
                 {% if is_number(states('sensor.sonos_{{ room }}_lyrics')) %}
                   {{ state_attr('sensor.sonos_{{ room }}_lyrics', 'plainLyrics') }}
                 {% else %}
                   {{ states('sensor.sonos_{{ room }}_lyrics') }}
                 {% endif %}



                content: >
                  {% raw %}
                  {% set room = %} {% endraw %} {{ room }} {% raw %} %}
                  {% endraw %}

                  {% if is_number(states('sensor.sonos_' ~ room ~ '_lyrics')) %}
                    {{ state_attr('sensor.sonos_' ~ room ~ '_lyrics', 'plainLyrics') }}
                  {% else %}
                    {{ states('sensor.sonos_' ~ room ~ '_lyrics') }}
                  {% endif %}

You did remember the # lovelace_gen line in the YAML files you want to use lovelace_gen syntax in?

The {% set room = %} {% endraw %} {{ room }} {% raw %} %} line also seems to have a %} too much after the = sign.

Also be aware that sometimes the Jinja will break the formatting.

{% set room = {% endraw %} {{ room }} {% raw %} %} might be parsed to

{% set room = 
   bedroom
   %}

Normally that does not matter, but in some cases it will.

The {%, %}, {{ and }} have another form also in {%-, -%}, {{- and -}}.
The minus will remove any whitespace character before {%- and {{- and after -%} and -}}, which means
{% set room = {%- endraw -%} {{- room -}} {%- raw -%} %} will be parsed as
{% set room =bedroom %}
This is not a special thing for lovelace_gen, but a standard thing for jinja in HA.

Thnaks.

Yes I did remember this and…

I’m not sure about that, I copied it from another place I have used that in but to be fair I did mess around with it quite a bit while trying to get it to work.

I tried a load of other different things and I have come to the conclusion that it is something obscure to do with the nesting I have of !incudes that end up at a popup. My suspicion is that the markdown card cannot deal with anything that looks like jinja at that level of nesting the way I had it setup.

In any case I have moved on now (life is too short!) and as I said I’m using the supremely over powered button-card.

Thanks again for replying. I’m still interested in a definitive answer if anyone has one.

This line will after parsing be

{% set room = %} bedroom %}