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

Weblink is part of standard lovelace enities card.

Iā€™ve used the weblink in an entities card before, but you need to use it as an option under the entities collection on the entities card, you canā€™t use it as a card in its own right.
I donā€™t think youā€™ll be able to recreate your mockup with just the card-templater and entities cards.

I went another route and made a ā€œcustom:html-template-cardā€

Is this the card you used? Looks good, may have to try that myself. :grinning:

Yes thats the one.
Super customizable

Hi,
Great card ! Iā€™m trying to template the state of a CO2 sensor entity (unit of measurement is ppm). I want it to display ā€œvery goodā€, ā€œgoodā€, ā€œnormalā€ and ā€œbadā€ depending of the different values (state) of the sensor. I thought your card will be useful to do so BUT it will still display the unit of measurement next to the string (see the attached picture). Is there any way to get rid of it ?


My yaml :

  - type: custom:card-templater
    card:
      type: entities
      title: 'Bedroom'
      show_header_toggle: false
      entities:
        - entity: sensor.netatmo_bedroom_co2
          name: "Air quality"
    entities:
      - entity: sensor.netatmo_bedroom_co2
        state_template: >-
          {% if states('sensor.netatmo_bedroom_co2')|int >= 1000 %}
            Bad
          {% else %}
            Good
          {% endif %}

Also, is it possible to template the secondary_info of an entity ? That would be greatly useful.

Iā€™ve just released a Beta version (0.0.6b1) which allows setting attributes as well as the state of the entity.

This can be used like this (without using a template):

type: 'custom:card-templater'
card:
  ...
entities:
  - entity: sensor.my_sensor
     state_template: >
       {{ "One" if states.sensor.my_sensor.state == "1" else "Not One" }}
     attributes:
       unit_of_measurement: "Testing"

or like this (using a template):

type: 'custom:card-templater'
card:
  ...
entities:
  - entity: sensor.my_sensor
     state_template: >
       {{ "One" if states.sensor.my_sensor.state == "1" else "Not One" }}
     attributes:
       unit_of_measurement_template: >
         {{ states.sensor.my_sensor.state }}
1 Like

Great addition, thank you so much !
Any chance to be able to icon_template ? I can see that I am able to define an icon using the without template method, but when tried to use icon_template with the template method, it didnā€™t workā€¦ ?

edit. actually, it seems like the template method (I tried with unit_of_measurement_template) is not working ? Did I do something wrong?

edit-2. OK got it, we must add the state_template line in order to get the attributes working, right ? What if I only need to change the attributes and not the state ?

Iā€™ve just deployed 0.0.6b2, which allows templaing attributes without the state.
Incidentally, you mentioned templating the icon. This can actually be templated without using using state templating at all. Just do it like this (using a temperature sensor as an example):

type: 'custom:card-templater'
card:
  type: entities
  entities:
    - entity: sensor.bedroom_temperature
      icon_template: |
        {{ "mdi:" + "home" }}
entities:
  - entity: sensor.bedroom_temperature
1 Like

This is perfect ! Thank you Steven, you rock!

I have this beef with Home Assistant as a whole, but surely, there is not a reason why we can only have templates in attributes that end in "_template". Why even distinguish? If weā€™re using the ā€œcard-templaterā€, then surely weā€™re using templates. And even if weā€™re not, a string or a number is a valid template on its own.

Is there some technical reason why itā€™s useful to identify templatable attributes by their names with "_template"?

Iā€™d done it like this since you may not want to template every option of the card you are wrapping in card-templater. Since this card needs to call the API to process the templates, it is more efficient to only do that for those properties which have templates. The only other way to identify these would be to parse the value of each property to see if it is a template, which would be harder (and more prone to error) than using the name of the property to determine this.

This makes sense. For something running browser-side, it makes sense to be able to avoid a round-trip to the server. However, I still have the issue with Home Assistant proper.

Iā€™m running into a simple getting started problem that I canā€™t get over. Hope someone has some ideas.
Using version 0.0.5 (HA 0.103.2).

I have the following:

      - type: entities
        title: Covers
        show_header_toggle: false
        entities:
          - entity: cover.my_test_blind1

      - type: 'custom:card-templater'
        card:
          type: entities
          title: Covers
          show_header_toggle: false
          entities:
            - entity: cover.my_test_blind1

The ordinary entities card works as expected.
The second one using templater has the card displaying fine but says Entity not available: cover.my_test_blind1
[EDIT] Solved. Didnā€™t notice I needed to add the entities: for the templater itself.

      - type: 'custom:card-templater'
        card:
          type: entities
          title: Covers
          show_header_toggle: false
          entities:
            - entity: cover.my_test_blind1
        entities:
          - cover.my_test_blind1

Iā€™m trying to use this to change an icon based on state.

I can get this to work using the following (Iā€™m using custom icons tlo:xxx)

      - type: 'custom:card-templater'
        card:
          type: entities
          title: 'Covers - custom Card Templater'
          show_header_toggle: false
          entities:
            - entity: cover.my_test_blind1
              name: Test Blind Icon state
              icon_template: >
                {{ "tlo:tilt-blind-close" if is_state("cover.my_test_blind1","closed") else "tlo:tilt-blind-open" }}

However, when I change the icon_template to the following, the icon is missing whether the icon is encapsulated in quotes or not:

              icon_template: >-
                {% if is_state("cover.my_test_blind1","closed")-%}
                  "tlo:tilt-blind-close"
                {% else -%}
                  "tlo:tilt-blind-open"
                {% endif %}

Your second example works for me if I remove the quotes around the icon names (not using the custom icons, since I donā€™t have those). Here is an example that worked for me:

icon_template: |-
  {% if is_state("device_tracker.my_phone","home")-%}
    mdi:home
  {% else -%}
    mdi:map-marker
  {% endif %}

The only difference was that it has icon_template: |- rather than icon_template: >- (I actually entered it as the latter and the GUI editor changed it to the former).

If it still doesnā€™t work, try changing the opening {% for your if, else, and endif to {%- (hyphen after the percent symbol). This will drop any leading newlines which may cause issues (you are already using -%} to close them (which drops trailing newlines).

Its working nowā€¦Thanks!!

Perhaps beyond scope, but wanted to ask, for the ā€œmore-infoā€ pop up card is there a way to template the icon for the pop up card? For example, for the blinds icon templating that was discussed above, when I click on the my_test_blind1 entity, the more-info pop up card is displayed, but it is displaying the HA native window open/closed icon instead of my custom icon.

In the latest beta version I expanded the state templating support so you can use it to template attributes. You should be able to do what you want by doing this:

type: 'custom:card-templater'
card:
  type: entities
  title: 'Covers - custom Card Templater'
  show_header_toggle: false
  entities:
    - entity: cover.my_test_blind1
      name: Test Blind Icon state
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 %}

This is basically moving the templating of the icon from the card configuration to the state templating, so it should affect both the display on the card and the more-info popup.