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

Actually…that’s not correct.

I tried it yesterday and I saw in the dev tools states page that it changed the actual state there.

here it is without the entity being templated (it’s the “binary_sensor.nuc_memory_usage_high” entity):

ex

and here it is after the templated config using the code from above You can see that it’s the same as the memory percent used sensor:

ex2

You won’t see the state change in the dev-tools states page until you have the templating code active in lovelace and then you need to restart HA. Then it shows the updated state.

But you can see the result of the template on the actual state because the entity-filter card (which is filtering for the “on” state) isn’t displaying the entity because the actual state is no longer “on”; it’s now “73.9%”.

I’ve done some testing using a copy of the hass object, rather than the original and, unfortunately, I don’t think this will actually be possible. The most I’ve been able to do is make it so templating the state of an entity doesn’t interfere with other cards on the page and doesn’t affect the Developer Tools/States page (and I’ll release a filx for this shortly). However, the issue with cards such as entity-filter is that it isn’t possible for the card-templater card to pass the templated states to the card without it also affecting the filtering (as everything is using the hass object passed to it).

Ok, thanks for trying.

I’ll have to see if I can figure out another way to do it. Or not… It’s not really something necessary. Just something I thought would be nice to have so I can see the actual number on that sensor instead of navigating to another page to see it.

Do you know of another custom component that allows us to template the displayed state of an entity?

I’m not aware of another component that would allow that, but as a workaround you could use name_template, something like this:

- type: 'custom:card-templater'
  card:
    type: entity-filter
    show_empty: false
    card: 
      type: glance
    state_filter: 
      - "on"
    entities:
      - binary_sensor.ha_update_avail_template
      - binary_sensor.potential_breaking_changes
      - entity: binary_sensor.nuc_memory_usage_high
        name_template: "NUC High Memory Usage - {{ states.sensor.memory_use_percent.state }}%"
      - binary_sensor.haapi_mqtt_test
      - input_boolean.low_batteries
  entities:
    - entity: binary_sensor.nuc_memory_usage_high

That would override the display name of the entity to have the percentage memory usage concanenated to the end.

1 Like

Good idea! Thanks!

It’s not perfect but it works great for what I needed.

I think you’ve posted this in the wrong thread, this is for the card-templater card which allows you to use Jinja2 templates. I think you need to post this here:

Apologies. Yes I did post it in the wrong topic.

Having said that I’m not getting anywhere with debugging it so I might try it with this card instead as I’m slightly less incompetent with jinja.

Would it be possible to use this card to make weblink?
In my example below i want a card with 2 collumns:
First the state of my sensor “sensor.spel_game_01” then the state of “sensor.spel_titel_01” that should be an clickable url from the state of the sensor “sensor.spel_url_01”
Screenshot

  - type: 'custom:card-templater'
    card:
      type: weblink
      show_header_toggle: false
      columns: 2
      title: Spel 01
      entities:
        - entity: sensor.spel_titel_01
          name_template: >-
            {{ states.sensor.spel_game_01.state }}
          url: >-
            {{ states.sensor.spel_url_01.state }}
    entities:
      - sensor.spel_titel_01

This card only allows the configuration of the card or the states of the entities to be templated. Unless the card you are templating allows the state to be displayed as a link (I’m not sure what card you are trying to use there, since weblink doesn’t actually seem to be a valid card) you won’t be able to use this card to do what you want.

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!