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

trying the card in markdown content_template to show the available js variables:

    - type: custom:card-templater
      card:
        type: markdown
        content_template: >
          ## Hello {{user.name}}
          You are admin: {{user.is_admin}} and owner {{user.is_owner}}
          This card is on the page {{page.path}} using {{theme}}, and
          {{page.pathname}} is the path name of the current page
          {{page.href}} - the full url of the current page
          {{page.protocol}} - the protocol (http/https) of the current page
          {{page.host}} - the host
        entities:
          - entity: sensor.time

but all I see is:

even when only using the {{user.name}} I see that single dotā€¦

no error in inspector. Is this card no longer supporting the markdown options?

It looks like this issue is that the markdown card natively supports templating, and also passes in variables. These were overriding the ones passed by card-templater,

Iā€™ve been working on an alpha version of the card, which refactors the templating method, and Iā€™ve implemented a fix for this issue in version 0.1.0-alpha2.

This version should also improve how quickly it responds to updates, as it now subscribes to template updates rather than directly checking for state updates.

1 Like

Hi Steven,

Thanks!, I can confirm this to work now.

Must confess the test view I am running this markdown template card on experiences huge lags because of this card. Take it out and the page flies by.

What can be wrong in this simple code that it is not rounding?

type: custom:card-templater
card:
  type: gauge
  entity: sensor.house_1_power
  state_template: '{{ states("sensor.house_1_power") | round(0, default=0) }}'

Have You ever solved it?

Iā€™m trying to use state_template working without success so far.
With the following code, I get the name property changed to expected value.

type: custom:card-templater
card:
  type: entities
  entities:
    - entity: sensor.grand_puits_semaine
      name_template: |
        {{states("sensor.grand_puits_semaine") | round(2)}}
  title: Grand puits
entities:
  - sensor.grand_puits_semaine

However with state_template instead of name_template nothing changes
I made this test as well

state_template: '{{"AAA"}}'

The original value is not replaced by ā€œAAAā€
What am I missing?
Thank you

EDIT: based on this post https://community.home-assistant.io/t/lovelace-card-templater-card-jinja2-card-templating-in-lovelace/114268/81 I managed to get state_template working. Here is the code

card:
  show_header_toggle: false
  type: entities
type: custom:card-templater
entities:
  - entity: sensor.grand_puits_semaine
    state_template: |
      {{states("sensor.grand_puits_semaine") | round(2)}}

There is a side effect to this solution, name_template does not work anymore. What would be the solution then?

How can I template the tap_action inside this entities_template?

entities_template: >-
              {%- set my_name = '[[name]]' -%} {%- set data =
              namespace(my_entities=[]) -%} {%- for item in
              expand(area_entities( my_name ))|map(attribute =
              'entity_id')|select('match',
              'light|fan|sensor|binary_sensor')|reject('search',
              'Z-Wave|ping|firmware|octoprint|node|select|number|switch|update|battery|device|lux')
              -%} {%- set better_name = states[item].name -%} {%- if my_name in
              better_name -%} {%- set better_name =
              better_name.split(my_name)[1] -%} {%- endif -%} {%- set
              better_name = (better_name|regex_replace(find="_[0-9]",
              replace='')|replace('_',' '))-%} {%- set data.my_entities =
              data.my_entities + [{"entity": item, "name": better_name,
              "tap_action": " \n",  "action": "toggle"}] -%}  {%- endfor -%} {{
              data.my_entities | list | tojson }}

Iā€™ve tried all sorts of permutations of "tap_action": "action": "toggle" but canā€™t find one that works

EDIT: found the solution on the HA discord.

"tap_action": {"action": "toggle" }

Why is my code not working?! What am I doing wrong?
The template works and gives back a string with a url in it.
Please can someone help me? :slight_smile:

type: custom:card-templater
card:
  type: picture
  entities:
    - entity: sensor.steam_76561197981585794
  image_template: |
    {{ state_attr('sensor.steam_76561197981585794','game_image_header') }}

Hi

Can anyone see anything wrong with this:

      card:
        type: custom:auto-entities
        card:
          type: custom:card-templater
          card:
            type: custom:paper-buttons-row
        filter:
          include:
            - entity_id: light.lounge
              options:
                buttons_template: >-
                  {%- set data = namespace(entities=[]) -%}
                  {%- for s in state_attr("this.entity_id", "hue_scenes") -%}
                  {%- set data.entities = data.entities + [{"icon": "mdi:lightbulb"}] -%}
                  {%- endfor -%}
                  {{ data.entities | list | tojson }}

I get an error that there are ā€˜Missing buttonsā€™

Thanks
Andy

Iā€™m not familiar with the card but is the filter section meant to be in the paper buttons card? need to indent it all further if so

Just wondering if this is a known issue. When using card templater with a gauge card and a needle, every time the entity state changes then the needle resets to zero and then moves to the correct position resulting in constant flicking of the needle. Any ideas if this can be fixed?

I have a water level sensor which gives integer values 0ā€¦4 for 5 different levels. What Iā€™m trying to achieve is, that instead of the number a text is shown in the gauge. How can I show that text in the gauge, but still make the gauge react correctly with its needle to the numbers 1ā€¦5? What I tried is:

type: custom:card-templater
card:
  type: gauge
  entity: sensor.frischwasser
  min: 0
  max: 4
  needle: true
  segments:
    - from: 0
      color: '#db4437'
    - from: 0.5
      color: '#ffa600'
    - from: 1.5
      color: '#ffcc00'
    - from: 2.5
      color: '#19c70b'
    - from: 3.5
      color: '#179806'
entities:
  - entity: sensor.frischwasser
    state_template: >
        {% set mapper =  {"0" : "< 1/3", "1" : "1/3 ... 1/2", "2" : "1/2 ...
        2/3", "3" : "2/3 ... 3/4", "4" : "> 3/4"} %}
        {{mapper[states.sensor.frischwasser.state] if
        states.sensor.frischwasser.state in mapper else "invalid"}}

but the gauge wants numeric values. Any chance to get the numeric values to the gauge renderer, and afterwards change the text? I managed to misuse the ā€œunit_of_measurementā€-field as my text:

    attributes:
      unit_of_measurement_template: >
        {% set mapper =  {"0" : "< 1/3", "1" : "1/3 ... 1/2", "2" : "1/2 ...
        2/3", "3" : "2/3 ... 3/4", "4" : "> 3/4"} %}
        {{mapper[states.sensor.frischwasser.state] if
        states.sensor.frischwasser.state in mapper else "invalid"}}

but then I still have the number in front of it.

type: custom:card-templater
card:
  type: picture-entity
  show_state: true
  show_name: true
  camera_view: auto
  image: https://www.xyz.us/{{ now().strftime('%Y/%m/%d') }}/image.jpg
  entity: fan.guest_bedroom_fan

This doesnā€™t seem to work. Am I missing something?

image: needs to be image_template:

Still no joy

Figured it outā€¦

type: custom:card-templater
card:
  type: picture-entity
  show_state: true
  show_name: true
  camera_view: auto
  image_template: >-
    {{ as_timestamp(states.sensor.date.state) |
    timestamp_custom('https://www.xyz.us/%Y/%m/%d/image.jpg') }}
  entity: fan.guest_bedroom_fan
entities:
  - sensor.date

Guys iā€™m tryng to use template with Gallery Card but idk what iā€™m doing wrong.

I have this card working fine:

type: custom:gallery-card
entities:
  - path: media-source://media_source/local/Cameras/Porteiro/2024/01/06
    include_video: false
file_name_format: YYYYMMDD-HHmmss
file_name_date_begins: 20
caption_format: D/M h:mm A

But i want the path to be a template and update everyday or get input from date_input entity.

Got it working with Config Template Card

card:
  type: custom:gallery-card
  entities:
    - path: >-
        ${'media-source://media_source/local/Cameras/Porteiro/'+(states['input_datetime.date_input'].state).replaceAll('-','/')}
      include_video: false
  file_name_format: YYYYMMDD-HHmmss
  file_name_date_begins: 20
  caption_format: D/M h:mm A
entities:
  - input_datetime.date_input
type: custom:config-template-card

Iā€™m feeling dumb but cannot work out why this is not working. I am trying to build a gauge that dynamically sets the max value to the oven setpoint with the current temperature on the gauge.

max_template: is ignored regardless of what I put in there. For a simple test, I put a raw number. The result is no scale shown on the gauge. (Changing to max: 200 results in a standard gauge, as expected).

Anyone? Any help much appreciated.

image

type: custom:card-templater
card:
  type: gauge
  entity: >-
    sensor.thermador_prd486wdgu_68a40eab143c_340_cooking_oven_status_currentcavitytemperature
  min: 0
  max_template: 200
  name: Oven
entities:
  - >-
    sensor.thermador_prd486wdgu_68a40eab143c_340_cooking_oven_status_currentcavitytemperature

Thanks so much for this card, the concept is so usefull!!!

Iā€™m trying to modify the field ā€œorp_setpointā€ in custom:pool-monitor-card
I replaced orp_setpoint with orp_setpoint_template and then I tried different syntaxes, without sucess.

type: custom:card-templater
card:
  type: custom:pool-monitor-card
  language: fr
  show_last_updated: true
  ph: sensor.analyseur_d_eau_ph
  ph_setpoint: 7.4
  orp: sensor.analyseur_d_eau_oxidation_reduction_potential
  orp_setpoint_template: >-
    MY TEMPLATE
  salinity: sensor.analyseur_d_eau_salinity
  salinity_setpoint: 6000
  salinity_step: 1000
  tds: sensor.analyseur_d_eau_total_dissolved_solids
  tds_setpoint: 4000
  tds_unit: ppm
  tds_step: 1000

In the place of ā€œMY TEMPLATEā€, I tried to apply following syntaxes,but none was sucessful.
Did I miss something??:
Isnā€™t it supposed to work with custom cards?

{{ 792 if states('sensor.analyseur_d_eau_ph')|round(1)==7 else 
785 if states('sensor.analyseur_d_eau_ph')|round(1)==7.1 else 
779 if states('sensor.analyseur_d_eau_ph')|round(1)==7.2 else 
773 if states('sensor.analyseur_d_eau_ph')|round(1)==7.3 else 
766 if states('sensor.analyseur_d_eau_ph')|round(1)==7.4 else 
760 if states('sensor.analyseur_d_eau_ph')|round(1)==7.5 else 
754 if states('sensor.analyseur_d_eau_ph')|round(1)==7.6 else 
748 if states('sensor.analyseur_d_eau_ph')|round(1)==7.7 else 766}}

I also tried with other syntax:

{% set ph = states('sensor.analyseur_d_eau_ph')|round(1) %}
{% if states('sensor.analyseur_d_eau_ph')|round(1)==7 %}792
{% elif states('sensor.analyseur_d_eau_ph')|round(1)==7.1 %}785
{% elif states('sensor.analyseur_d_eau_ph')|round(1)==7.2 %}779
{% elif states('sensor.analyseur_d_eau_ph')|round(1)==7.3 %}773
{% elif states('sensor.analyseur_d_eau_ph')|round(1)==7.4 %}766
{% elif states('sensor.analyseur_d_eau_ph')|round(1)==7.5 %}760
{% elif states('sensor.analyseur_d_eau_ph')|round(1)==7.6 %}754
{% elif states('sensor.analyseur_d_eau_ph')|round(1)==7.7 %}748
{% else %}766
{% endif %}
{% set ph = states('sensor.analyseur_d_eau_ph')|round(1) %}
{% if ph==7 %}792
{% elif ph==7.1 %}
{% elif ph==7.2 %}779
{% elif ph==7.3 %}773
{% elif ph==7.4 %}766
{% elif ph==7.5 %}760
{% elif ph==7.6 %}754
{% elif ph==7.7 %}748
{% else %}766
{% endif %}

Any help would be more than welcome :slight_smile: :slight_smile: