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

Just noticed that you seem to have the “s” missing from group.all_lights in the template, this may just have been a typo in your post but, if not, your template should probably be:

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

I’ve fixed the typo already and also tried with a single entity like a lamp wothout any results.
(I copy pasted the code earlier as example)

It seems that the issue is due to templates always returning strings. Since the open option is a boolean type rather than a string it seems that this prevents fold-entity-row from getting the correct value.
This is because it treats anything which is not equal (and of the same type) to false (!== false) as true, effectively mapping “false” to true.

There is a workaround, by templating the whole entities option as doing it that way, rather than templating individual options, avoids this issue. This works for me:

type: 'custom:card-templater'
card:
  type: entities
  entities_template: |
    - head: group.all_lights
      type: 'custom:fold-entity-row'
      open: {{ is_state('group.all_lights','on') | lower }}
entities:
  - group.all_lights

(I simplified the template for open there since is_state returns True or False and so this can just be converted to lower case to get true or false)

3 Likes

lots of thanks!
It is working as intended now!

Just wanted to say thanks for this. I used it to make a template history-graph card.

title: Graphs
id: graphs
panel: true
cards: 
  - type: vertical-stack
    cards:
       - type: entities
         entities: 
            - input_number.graph_hours
       - type: 'custom:card-templater'
         card:
           type: history-graph
           hours_to_show_template: "{{ states('input_number.graph_hours')|float}}"
           title: Temperature
           refresh_interval: 30
           entities:
            - sensor.bedroom_temperature
            - sensor.kitchen_temperature
            - sensor.entry_temperature
            - sensor.thermostat_temperature
            - sensor.thermostat_temperature_2
            - sensor.relay_temperature
         entities: 
            - input_number.graph_hours


2 Likes

Still trying to get my bearing …
I wanted a picture card so I can have an action on the click. I wanted to resolve the picture path with a template that way I can have a different picture for each state.
Do I have a typo or am I expecting something it was not design for. I saw something earlier in the thread about covers that makes me thing this will not work, but right now I don’t see why …

Here is the card

type: 'custom:card-templater'
card:
  type: picture
  image_template: >-
                 {{% if is_state('cover.almond_plus_garagedooropener_two_car_4_1', 'open')  %}
                     '/local/enterprise_shuttle_bay_cover/BayOpened.jpg'   
                 {% elif is_state('cover.almond_plus_garagedooropener_two_car_4_1', 'closed') %}
                      '/local/enterprise_shuttle_bay_cover/BayOpened.jpg'
                 {% elif is_state('cover.almond_plus_garagedooropener_two_car_4_1', 'opening') %}
                     '/local/enterprise_shuttle_bay_cover/BayOpening.gif'
                 {% elif is_state('cover.almond_plus_garagedooropener_two_car_4_1', 'closing') %}
                       '/local/enterprise_shuttle_bay_cover/BayClosing.gif'
                 {% else %}
                       '/local/enterprise_shuttle_bay_cover/question-mark-1019820__340.jpg'
                 {% endif %}}
  tap_action:
    action: none
  hold_action:
    action: none
entities:
  - cover.almond_plus_garagedooropener_two_car_4_1

I am just getting started with templating and attempting to experiment with a very simple example, but for the life of me I cannot get this to work. Basically I have an input_text entity called input_text.sunroom_camera_image, and the current value of the entity is sunroom. The following is what I currently have in my lovelace config:

    - type: 'custom:card-templater'
        card:
          type: picture-glance
          title_template: "/local/cam_captures/{{ states.input_text.sunroom_camera_image.state }}.jpg"
          entities:
            - camera.sun_room
          image_template: "/local/cam_captures/{{ states.input_text.sunroom_camera_image.state }}.jpg"

I am actually trying to replace the image name with the value that is currently in the input_text entity. Since I have had so much trouble getting this to work, for test purposes I put the same value in for the title_template: of the lovelace card. I have tried a number of ways to format title_temple:, but the title always displays as a dash (-). I have copied the same string to Developer Tools/Templates, and it displays correctly as follows:

"/local/cam_captures/sunroom.jpg"

Can anyone point me in the right direction?

Currently, the card doesn’t automatically pick up what entities the template needs to monitor so you need to tell it, like this:

    - type: 'custom:card-templater'
        card:
          type: picture-glance
          title_template: "/local/cam_captures/{{ states.input_text.sunroom_camera_image.state }}.jpg"
          entities:
            - camera.sun_room
          image_template: "/local/cam_captures/{{ states.input_text.sunroom_camera_image.state }}.jpg"
      entities:
        - input_text.sunroom_camera_image

Thanks you soooo much! I knew it had to be something simple.

I really like this example but it picks up duplicates because for example, my phone is detected via bluetooth as well as being on my network to improve detection. This creates multiple device_trackers. I only want to see one entry. I can’t change to using ‘person’ to consolidate because I want to see visitors listed as well. How can I exclude, for example, by partial name match like entity_id contains BT ? Or anyone got a better idea?

You’re a bit restricted by what Jinja2 filters Home Assistant supports. One way of doing this is to use loops instead. You can either try to build up the JSON with string concatenation in the loop, or use a workaround like below using a Jinja2 namespace (to work around loops having a separate variable scope) and list concatenation (use to the list.append method being treated as unsafe).

type: 'custom:card-templater'
card:
  type: entities
  title: Who's at Home
  entities_template: >-
    {%- set data = namespace(entities=[]) -%}
    {%- for entity in states.device_tracker | selectattr("state", "equalto",
    "home") -%}
      {%- if not "bt" in entity.entity_id -%} 
        {%- set data.entities = data.entities + [entity.entity_id] -%}
      {%- endif -%}
    {%- endfor -%}
    {{ data.entities | list | tojson }}
entities:
  - sensor.time
1 Like

I have now released version 0.0.9.

This has the following changes:

  • Ability to exclude an option ending with _template from being treated as a template - this done by adding ! to the end (e.g. content_template!: "{{state}}" for the content_template option in lovelace-home-feed-card)
  • Treat “true”/“false” template values as boolean to make it easier to template cards which take boolean options.
  • Fixed issue where the theme template variable was always using the backend selected theme rather than the currently selected one
  • Pass isPanel to child card for improved compatibility with cards such as iframe when view is in panel mode

Hi,Is it possible to show different cards depending on the status of a true / false sensor I want a card to be displayed if the sensor is true and a different card if the sensor is false?

I have an interesting issue. I’m using the code below and the card appears as a red line until I navigate to another view and then it loads properly. I’m using this on my tablets mounted to the wall with the custom header plug-in that has them in a kiosk mode so switching view’s is not normally possible. Any ideas on how to solve this?

- type: custom:card-templater
            entities:
              - sensor.time_date
            card:
                type: alarm-panel
                style: |
                    .card-header {
                    font-size: 30px;
                    }
                entity: alarm_control_panel.house_alarm
                name_template: >-
                    {{as_timestamp(now()) | timestamp_custom('%A %b %d   %I:%M %p') | replace(' 0',' ')}}
                states:
                  - arm_away
                  - arm_home
                  - arm_night

What version are you using? If you are not on the latest version (0.0.9) can you try updating to that and see if it helps? That fixed an issue which was reported relating to the display of cards such as the iframe card when in panel mode (they would only take up part of the page). Although this doesn’t appear to be directly related to your issue, while reproducing the issue before creating the fix, I did get the same issue with the red line and the fix also seemed to have stopped that happening.

You can potentially do this by templating the type property using type_template, but this works best when the other options are the same between the different cards (e.g. switch between an entities or a glance card). For example:

type: 'custom:card-templater'
card:
  type_template: >-
    {{ "entities" if states.input_boolean.display_entities.state == "on" else "glance" }}
entities:
  - light.light1
  - light.light2

For anything other that that, it would probably be easier to use two Conditional cards in a Vertical Stack. You could still use the card-templater inside the Conditional card if you need to template other options in the cards.

1 Like

I have Updated everything.
Hass Supervisor: 217
Home Assistant 0.108.5
Card Templater 0.0.9
It seems to work on my PC the first time but on my android phone and tablets I need to switch views for it to work properly.

I’m trying to change the state value with this configuration. But just can’t get it to work. What am I doing wrong? The state just showing 22 so the template is not applied at all.

card:
  columns: 2
  entities:
    - entity: sensor.altandrr_access_control
      state_template: >
        {{ "One" if states.sensor.altandrr_access_control.state == "22" else
        "Not One" }}
  show_header_toggle: false
  title: Places
  type: entities
type: 'custom:card-templater'

The state_template option belongs under the entities collection of the card-templater rather than the templated card, like this:

card:
  columns: 2
  show_header_toggle: false
  title: Places
  type: entities
type: 'custom:card-templater'
entities:
    - entity: sensor.altandrr_access_control
      state_template: >
        {{ "One" if states.sensor.altandrr_access_control.state == "22" else
        "Not One" }}

(card-templater will pass its own entities to the templated card, so you don’t need it twice)

1 Like

Super, thank you very much!