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

Yes, you are correct. I get a popup on the left bottom which says Connection lost…

Here is an annoying thing that happens occasionally on refreshing the frontend or switching between tabs. Another refresh will always fix it it tho. It just started happening after I updated to v110.

ex1

I get it on one other card (state-switch) with a very similar error and a refresh clears that one up too.

ex2

I know that state-switch isn’t one of yours and I plan on posting this to that thread too but I thought it might give you more insight as to what could be causing it.

Hi @chipware,
this looks awesome, i wanted to check with you on how you doing the calculation for the cost . Are you setting up some custom entities based on reading you are getting. If so possible to share your example :slightly_smiling_face:

@joiboi

You have to turn watts into KWH, then KWH into Cost by multiplying by your utility’s cost per KWH. So:

In sensors.yaml:

- platform: integration
  source: sensor.centralite_3210_l_d07f4f0b_electrical_measurement
  name: "Server Plug Kwh"
  unit_prefix: k
  method: left
  round: 2

In utility_meter.yaml:

server_plug_kwh_daily:
  name: "Server Plug Kwh Daily"
  source: sensor.server_plug_kwh
  offset: 00:00:01
  cycle: daily

server_plug_kwh_monthly:
  name: "Server Plug Kwh Monthly"
  source: sensor.server_plug_kwh
  cycle: monthly
  offset:
    days: 1

That will accumulate KWH daily and monthly, resetting at midnight and the first of the month.

Then back in sensors.yaml:

- platform: template
  sensors:
    server_plug_cost_daily:
      friendly_name: Server Plug Cost Daily
      unit_of_measurement: '$' 
      value_template: "{{ (0.10853 * states('sensor.server_plug_kwh_daily') | float) | round(2) }}"
    server_plug_cost_monthly:
      friendly_name: Server Plug Cost Monthly
      unit_of_measurement: '$'
      value_template: "{{ (0.10853 * states('sensor.server_plug_kwh_monthly') | float) | round(2) }}"
1 Like

Hi @chipware,
Appreciate your reply and the same code you are using :smiley:

i currently am using some plugs which is configured via ESPhome. it gives me both Wattage value ( in Watts) and also a daily_energy (in Kwh) , so the plug keeps a cumulative value of the Kwh, although i found if i were to remove the plug this value is set to “0”.

i am guessing the best approach would be to take the “raw” watt value and and create a new template sensor to give me the watt ==> Kwh followed by applying the cost factor.

in your example above , i was wondering with the “sensor.centralite_3210_l_d07f4f0b_electrical_measurement” , do you get a Kwh directly from the sensor so are you also going through a conversion within Home assistant?

He s using the integration sensor to obtain energy from power:

- platform: integration
  source: sensor.centralite_3210_l_d07f4f0b_electrical_measurement
  name: "Server Plug Kwh"
  unit_prefix: k
  method: left
  round: 2

Please note the choice of integration method is critical for getting sensible values from this sensor. See which method to select here:

2 Likes

thanks @tom_l explanation :smiley: , its making sense , time to implement and see how it goes :smiley:

New in home assistant. Trying to do garbage collection (https://github.com/bruxy70/Garbage-Collection) and can not get the the days left to display. Wonder if anyone would know what I’m doing wrong. If I go to developer tools-templates I can see the correct data but they will now show on the png.

Would you be able to post the config of the card as text (formatted as a code block) rather than an image? I think you need populate the entities collection for the outer card-templater card, but it’s hard to tell from an image.

cards:
  - card:
      entities:
        - sensor.red_bin
      entity: sensor.red_bin
      name_template: '{{ states.sensor.red_bin.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/red1.png
        '1': /local/garbage_bins/red2.png
        '2': /local/garbage_bins/red3.png
      type: picture-entity
    type: 'custom:card-templater'
  - card:
      entities:
        - sensor.green_bin
      entity: sensor.green_bin
      name_template: '{{ states.sensor.green_bin.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/green1.png
        '1': /local/garbage_bins/green2.png
        '2': /local/garbage_bins/green3.png
      type: picture-entity
    type: 'custom:card-templater'
  - card:
      entities:
        - sensor.yellow_bin
      entity: sensor.yellow_bin
      name_template: '{{ states.sensor.yellow_bin.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/yellow1.png
        '1': /local/garbage_bins/yellow2.png
        '2': /local/garbage_bins/yellow3.png
      type: picture-entity
    type: 'custom:card-templater'
  - card:
      entities:
        - sensor.kerbside
      entity: sensor.kerbside
      name_template: '{{ states.sensor.kerbside.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/kerb11.png
        '1': /local/garbage_bins/kerb21.png
        '2': /local/garbage_bins/kerb31.png
      type: picture-entity
    type: 'custom:card-templater'
type: horizontal-stack

You appear to have the entities option nested under the picture-entity card, but this belongs under card-templater like this:

cards:
  - card:
      entity: sensor.red_bin
      name_template: '{{ states.sensor.red_bin.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/red1.png
        '1': /local/garbage_bins/red2.png
        '2': /local/garbage_bins/red3.png
      type: picture-entity
    type: 'custom:card-templater'
    entities:
      - sensor.red_bin
  - card:
      entity: sensor.green_bin
      name_template: '{{ states.sensor.green_bin.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/green1.png
        '1': /local/garbage_bins/green2.png
        '2': /local/garbage_bins/green3.png
      type: picture-entity
    type: 'custom:card-templater'
    entities:
      - sensor.green_bin
  - card:
      entity: sensor.yellow_bin
      name_template: '{{ states.sensor.yellow_bin.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/yellow1.png
        '1': /local/garbage_bins/yellow2.png
        '2': /local/garbage_bins/yellow3.png
      type: picture-entity
    type: 'custom:card-templater'
    entities:
      - sensor.yellow_bin
  - card:
      entity: sensor.kerbside
      name_template: '{{ states.sensor.kerbside.attributes.days }} days'
      show_name: true
      show_state: false
      state_image:
        '0': /local/garbage_bins/kerb11.png
        '1': /local/garbage_bins/kerb21.png
        '2': /local/garbage_bins/kerb31.png
      type: picture-entity
    type: 'custom:card-templater'
    entities:
      - sensor.kerbside
type: horizontal-stack
1 Like

OMG, thank you, that was it. And I’m trying to fix this for last 4 days :slight_smile: Thank you very much

trying to achieve something really simple in theory, but I keep getting “Error!”:

type: 'custom:card-templater'
card:
  entities:
  - entity: switch.my_hifi
    name_template: {{ sensor.hifi_title_status }}
  type: entities
entities:
  - sensor.hifi_title_status

this is pseudocode to signify what I want to achieve (albeit I tried this precise code too, I tried many different combinations using states. and many more, but can’t figure what’s wrong

You either need to surround your template with quotes like:

name_template: '{{ sensor.hifi_title_status }}'

or use this format:

name_template: >-
  {{ sensor.hifi_title_status }}
1 Like

Thank you… that was embarassingly simple!
I am not sure why I cannot add something like sensor.hifi_title_status.split(‘,’)[0] tho … it works in the templates tester

it does working with this code tho

'{% set now_playing = states.sensor.hifi_title_status.state_with_unit.split(",")[0] %} 
{{ now_playing }}'

that’s good I guess :slight_smile:

Any idea why mini-media-player refreshes much slower when passed through card-templater?

I have 2 cards right now, one with mini-media-player only, and one with mini-media-player inside card-templater.

I can take an action on either card (change source, volume,etc) and it will always show the updated value on the mini-media-player only one within a very few seconds, while the one inside card-templater takes several, like 30 seconds or so.

@Steven_Rollason Thanks for writing and supporting this - great work!

Is there any way to use wildcards in templates? I have a lot of NMap-Tracker entities that show up as device trackers, but I just want to show if they are connected to the network - Home/Away isn’t really appropriate.

Here’s what I have so far - I really want to avoid having to cut and paste the template for 50+ devices:

- type: 'custom:card-templater'
  card:
    entities:
      - entity: device_tracker.a8_a1_59_**_**_**
      - entity: device_tracker.b8_27_eb_**_**_**
      - entity: device_tracker.00_0f_60_**_**_**
      - entity: device_tracker.b8_e8_56_**_**_**
      - entity: device_tracker.1c_65_9d_**_**_**
    title: Computers
    type: entities
  entities:
    - entity: device_tracker.a8_a1_59_**_**_**
      state_template: >
        {{ "On" if states.device_tracker.a8_a1_59_**_**_**.state == "home"
        else "Off" }}

Something like - entity: device_tracker.* or - entity.device_tracker.all?

There’s nothing built-in for this, but you could use this card together with the auto-entities card to do it.

The best way to use it would be to add all those device_trackers to a group called group.nmap_trackers and then the configuration would look like this:

type: 'custom:auto-entities'
card:
  type: 'custom:card-templater'
  card:
    type: entities
filter:
  include:
    - group: group.nmap_trackers
      options:
        state_template: |
          {{ "On" if states.this.entity_id.state == "home" else "Off" }}

This works because of two things:

  • If you don’t define the entities option on the card you are templating with card-templater, then card-templater will pass its entities option (which, in turn, it is getting from auto-entities) on to it.
  • auto-entities will automatically replace the text this.entity_id with the entity id of each entity, so states.this.entity_id.state becomes states.device_tracker.whatever.state if the matching entity is device_tracker.whatever for example.

Thank you for that! It almost works…
image

I think I saw somewhere that entity names that start with a number are a problem?

Try this instead:

state_template: |
          {{ "On" if states("this.entity_id") == "home" else "Off" }}

That should work better with entity ids starting with numbers.

Excellent! Thank you. I was trying this:

{{ "On" if states[states.this.entity_id].state == "home" else "Off" }}

but I wasn’t getting very far. Is there a way to cast variables as different types?