Lovelace card-templater card (Jinja2 card templating in Lovelace!)

Excellent!! Thanks :slight_smile:

Is this the right YAML for weblink?

type: 'custom:card-templater'
card:
  entities:
    - icon: 'mdi:light-switch'
      name: Sonoff Basic - Hometheater Switch
      type: weblink
      url_template: 'http://{{ states.device_tracker.80_7d_3a_59_db_40.attributes.ip }}/'
  show_header_toggle: false
  title: WiFi Switches / Plugs
  type: entities
entities:
  - entity: device_tracker.80_7d_3a_59_db_40

That looks correct. Providing that the device tracker has an ip attribute it should work fine.

I have a strange error. When I use device_tracker.00_00_00_00_00 the link suffix as ‘Error!’ whereas if I use device_tracker.myrouter (renamed/configured in known_devices), I can able to see the templated weblink. Any clue?

Can you try the same template in the Developer > Templates page?
If you also get an error there, the detailed error message should show in your logs (home-assistant.log).

Also, if you renamed it in known_devices.yaml, this would need a restart of Home Assistant for this to get picked up. Have you done this?

When I try
Anne Therese is at {{ states.device_tracker.c4_4f_33_8e_bc_5c.attributes.ip }}
I got Anne Therese is at 192.168.1.132
But when I try
Anne Therese is at {{ states.device_tracker.80_7d_3a_59_db_40.attributes.ip }}
I got an error
Error rendering template: TemplateSyntaxError: expected token 'end of print statement', got '_7d_3a_59_db_40'

This is a known issue with templates and entity ids starting with a number. The workaround is to use states.domain["entity"], for example:

{{ states.device_tracker["80_7d_3a_59_db_40"].attributes.ip }}

Thank you. Working good.

Version 0.0.6 has now been released. This includes the following features and fixes:

  • Templating of state attributes (released in 0.06b1 with enhancements in 0.0.6b2)
  • Preventing entities for cards like auto-entities from being overridden (this was happening as the result of code which passed the entities from the card-templater to any nested entities or galnce cards without entities defined)
  • Added variables, which are passed to the templating engine. These are
    • user.name - the name of the current user
    • user.is_admin - whether the current user is an admin
    • user.is_owner - whether the current user is the owner
    • page.path - the path name of the current page (e.g. /lovelace/home)

Here is an example of using the variables:

type: 'custom:card-templater'
card:
  type: markdown
  content_template >
    ## Hello {{ user.name }}
    This card is on the page {{ page.path }}
entities:
  - entity: sensor.time

Regarding Covers entity card and getting the icon_template to work not only for the entity card but also for the more-info popup, I tried the suggested configuration (moving the icon_template into the entity attributes) after downloading the 0.0.6 version.
It still works for the card, but not for the more-info popup where it still shows the default HA icons.

I took at look at the developer tools to see what the attributes are for the cover, and “icon” is not listed. Does this mean it won’t work for the more-info popup for covers?

I’ve investigated this and I think the problem is due to the more-info component maintaining its own instance of the hass object (which contains a dictionary of all entity states) and not inheriting the one from the card (which is modified by card-templater when you use state and/or attribute templating). I don’t think there is a way I can update card-templater to solve this without the templated states leaking to other cards.

If you use the Browser Mod custom component, you may be able to work around this. Take a look here:

Replacing more-info dialogs

The examples use standard cards, but you should be able to do something like this:

popup_cards:
  cover.my_test_blind1:
    title: Test Blind
    card:
      type: 'custom:card-templater'
      card:
        type: entities
        entities:
          - cover.my_test_blind1
      entities:
        - entity: cover.my_test_blind1
          attributes:
            icon_template: |-
              {%- if is_state("cover.my_test_blind1","closed")-%}
                 tlo:tilt-blind-close
              {%- else -%}
                tlo:tilt-blind-open
              {%- endif %}

Good day. Is it possible to use name_template to present a part of the entity_id name?

So I have the followint entity: switch.tower_docker_adb
I would like to get just “adb”; this is what i’ve tried with no luck.

  - type: 'custom:card-templater'
    card:
      type: entities
      title: unraid
      entities_template: >-
        {{ states.switch | selectattr("attributes.status", "equalto",
        "started") | map(attribute="entity_id") | list | tojson }}
        
      name_template: >-
        {{ switch.attributes.name }}

thank you

You would need the name_template to apply to each entity, which isn’t possible the way you are doing it.

You would need to somehow have the template in entities_template return a list of entity objects in JSON format. One way which may work is something like:

- type: 'custom:card-templater'
  card:
    type: entities
    title: unraid
    entities_template: >-
      {%- set data = namespace(switches=[]) -%}
      {%- for switch in states.switch | selectattr("attributes.status", "equalto", "started") -%}
      {%- set data.switches = data.switches + [{"entity": switch.entity_id, 
      "name": switch.attributes.name}] -%}
      {%- endfor -%}
      {{ data.switches | list | tojson }}

thank you for your help. I tried it with no luck. It may not be possible.

Did you get an error (or an empty card) when you tried it that way or did it just not give the expected name?

If it was the former, try it like this instead (this splits the entity id of the switch by the underscore character and uses the last part as the name):

- type: 'custom:card-templater'
  card:
    type: entities
    title: unraid
    entities_template: >-
      {%- set data = namespace(switches=[]) -%}
      {%- for switch in states.switch | selectattr("attributes.status", "equalto", "started") -%}
      {%- set data.switches = data.switches + [{"entity": switch.entity_id, 
      "name": switch.entity_id.split("_") | last }] -%}
      {%- endfor -%}
      {{ data.switches | list | tojson }}

Thank you very much. It was the former. i’ll try your revised suggestion.

I have just released a new beta (0.0.8b1) with the following changes:

  • Switched to subscribeRenderTemplate for template processing, should allow this to work for non-admin users
  • Added additional url template variables - all the properties of the Javascript location object should now be exposed under the page object, for example:
    • page.href
    • page.protocol
    • page.host

Hello, I’m trying to have a fold-entity-row be expanded by default if the light(group)'s state is ‘on’ but no matter what I try it doesnt do what I want. (stays closed as default)

This is my code, hope someone can help or point out an possible error since I’m new to yaml and just installed Home Assistant a few days ago

entities:
  - head: group.all_lights
    type: 'custom:fold-entity-row'
    open_template: |
      {% if is_state('group.all_light','on') %}
        true
      {% else %} 
        false 
      {% endif %}
type: entities

You don’t actually have the card-templater there, you need to have something like this:

type: 'custom:card-templater'
card:
  type: entities
  entities:
    - head: group.all_lights
      type: 'custom:fold-entity-row'
      open_template: |
        {% if is_state('group.all_light','on') %}
          true
        {% else %} 
          false 
        {% endif %}
entities:
  - group.all_lights

(You need to have entities twice in the config, so card-templater knows what entities to monitor for changes when processing the templates)

thanks for the quick reply,
I understand what was missing but the intended effect isn’t working.
I tried to set both to false after manually turning off all lights didn’t work, but even with both set to false
[…]

{% if is_state('group.all_light','on') %}
          false
        {% else %} 
          false 
        {% endif %}

[…]
the fold entity card is still open by default.