🔹 template-entity-row - Put templates in an entities row

I want to give a shout-out to Thomas for this excellent custom integration. There are a number of systems which break up a single system into multiple entities (e.g., ring, zwave, etc.). It’s very painful to present the desired information for such systems in hass. You can do it via templates but this has the drawback of having to restart hass if you have to make changes. Another alternative was to use multiple custom integrations, which has the drawback of being very difficult to read and understand. For example, I have a Family card that displays the location of my family members, and proximity from home as secondary information:

image

To generate this card, my previous yaml was:

cards:
  - card:
      entities:
        - entity: person.john_smith
          secondary_info: >-
          {% if states('person.john_smith') != 'home' and 
              states('sensor.john_smith_proximity') != 'unknown' %} 
            {{ states('sensor.john_smith_proximity') }} minutes from home 
          {% else %}
          {% endif %}
          type: 'custom:secondaryinfo-entity-row'
        - entity: person.jane_smith
          secondary_info: >-
          {% if states('person.jane_smith') != 'home' and 
              states('sensor.jane_smith_proximity') != 'unknown' %} 
            {{ states('sensor.jane_smith_proximity') }} minutes from home 
          {% else %}
          {% endif %}
          type: 'custom:secondaryinfo-entity-row'
        - entity: person.jenny_smith
        - entity: person.joe_smith
      show_header_toggle: false
      title: Family
      type: 'custom:hui-entities-card'
    entities:
      - device_tracker.john_smith_iphone
      - sensor.john_smith_proximity
      - device_tracker.jane_smith_iphone
      - sensor.jane_smith_proximity
      - device_tracker.jenny_smith_ipad
      - device_tracker.joe_smith_ipad
    type: 'custom:config-template-card'
    variables:
      - 'states[''device_tracker.john_smith_iphone''].state'
      - 'states[''sensor.john_smith_proximity''].state'
      - 'states[''device_tracker.jane_smith_iphone''].state'
      - 'states[''sensor.jane_smith_proximity''].state'

This was a nightmare to debug. Now, with template-entity-row, my yaml is:

entities:
  - entity: person.john_smith
    secondary: >-
      {% if states('person.john_smith') != 'home' and 
            states('sensor.john_smith_proximity') != 'unknown' %} 
        {{ states('sensor.john_smith_proximity') }} minutes from home 
      {% else %}
      {% endif %}
    type: 'custom:template-entity-row'
  - entity: person.jane_smith
    secondary: >-
      {% if states('person.jane_smith') != 'home' and
            states('sensor.jane_smith_proximity') != 'unknown' %} 
        {{ states('sensor.jane_smith_proximity') }} minutes from home 
      {% else %}
      {% endif %}    
    type: 'custom:template-entity-row'
  - entity: person.jenny_smith
  - entity: person.joe_smith
show_header_toggle: false
title: Family
type: entities
2 Likes

This is really great!

I have used it for displaying the following

Where it tells me how long a utility has been on (or off) and how much money has been spend on it today (whenever it’s more than €0.01, otherwise it’s hidden).

If anyone cares, I share my config here.

2 Likes

Great card, thank you. Exactly what I need. How can I use this.entity from a auto-entities-card to display an attribute as state value?

I tried it like this:

- type: custom:auto-entities
  card:
    type: entities
    title: Last seen
  filter:
    include:
      - attributes:
          last_seen: ">0"
        options:
            type: custom:template-entity-row
            state: "{{states['this.entity'].attributes.last_seen }}"

Don’t use states that way. Ever.
https://www.home-assistant.io/docs/configuration/templating/#states

I even tried
"{{state_attr('this.entity','last_seen') }}"

but this displays only None

auto-entities sets the entity: property of each row.
Templates lets you access the rows configuration in the config variable.
Try "{{state_attr(config.entity, 'last_seen')}}

1 Like

That works. Thank you very much!

I may be misconfiguring something but the underscore function _(<key>) doesn’t seem to be working for me:

entities:
  - entity: binary_sensor.master_bedroom_occupancy
  - entity: binary_sensor.master_bedroom_occupancy
    type: 'custom:template-entity-row'
  - entity: binary_sensor.master_bedroom_occupancy
    state: '{{ _(state.binary_sensor.default.off) }}'
    type: 'custom:template-entity-row'
title: Template-entity-row-test
show_header_toggle: false
type: entities

image

The result of the template must be _(state.binary_sensor.default.off).

The template {{ _(state.binary_sensor.default.off) }} will evaluate to an error because _ is undefined.

{{ '_(state.binary_sensor.default.off)' }} will work, though.

1 Like

I am still having some confusion about how to take advantage of the localization feature in template-entity-row. The below configuration works:

entities:
  - entity: binary_sensor.master_bedroom_occupancy
    state: |-
      {% if is_state('binary_sensor.master_bedroom_occupancy','on') %}
        {{ '_(state.binary_sensor.occupancy.on)' }}
      {% else %}
        {{ '_(state.binary_sensor.occupancy.off)' }}
      {% endif %}  
    type: 'custom:template-entity-row'
type: entities

But is there a more efficient way of doing this, something like, e.g.:

state: {{ '_(eval('state.binary_sensor.occupancy.'+states('binary_sensor.master_bedroom_occupancy'))) }}  

state: "_(state.binary_sensor.occupancy.{{states('binary_sensor.master_bedroom_occupancy')}})"

Make sure you understand why before you copy it.

1 Like

HI,

this is new to me, where is the underscore function described or mentioned? (or is it simply the concatenation Underscore between single words used in regular code everywhere)
secondly, is this binary_sensor.default a true binary_sensor in your config, or is ‘default’ also some reserved name in the template? What do you want the template to show in the card?

Thomas added it as a new feature in the latest release of template-entity-row to support localized strings:

My understanding is shaky, but I believe the underscore function _(<key>) is a JavaScript function that maps a key to localized string:

a yes, I had missed the completely, thanks for pointing it out on https://github.com/thomasloven/lovelace-template-entity-row#options

Have always been thinking the system takes care of localization anyway, so don’t really understand when this would be necessary?

@thomasloven there’s a small but important typo there:

document.querySelector("home-assistant").hass.resouces

should be:

document.querySelector("home-assistant").hass.resources

It’s not a javascript function in this case. It’s an identifier which I search for in the processed template and replace.
The name _ is commonly used for localization in several programming languges, though. That’s why I chose it.

trying to display all GitHub sensors with their respective versions and last_changed on secondary, I can’t find the correct syntax to realize that. Could someone please help me find the template (or maybe direct config setting, tried last_changed obviously, or secondary_info: last-changed but didn’t work) I need?

  - type: custom:auto-entities
    card:
      type: entities
      title: Github repos
      show_header_toggle: false
    filter:
      include:
        - entity_id: sensor.github*
          options:
            type: custom:template-entity-row
            state: >
              {% if state_attr(config.entity,'latest_release_url') %}
              {{ state_attr(config.entity,'latest_release_url').split('tag/')[1]}}
              {% else %} Not set
              {% endif %}
            secondary: "{{ config.entity.last_changed}}"
          sort:
            method: name

gives me:

secondly, Id like to be able to click the entities and reveal more-info. Can’t we tap these template-entity-row 's at all? No handle appears, and clicking does nothing…

thanks for having a look

last_changed and last_updated are a bit odd ones…

{{ states[config.entity].last_changed }}

1 Like

better!

bit of formatting makes it more readable:

            secondary: "{{ as_timestamp(states[config.entity].last_changed)| timestamp_custom('%X - %D') }}"

Thomas, how can we reveal the more-info on these entities?

HI Thomas,

Since updating to 105.1/2 the above card using your auto-entities and template-entity-row is in a state of turmoil… it shows like this now:
Feb-07-2020 10-35-04

every few seconds flickers to its ‘supposed to be state’ like posted above for a split second and then back to the full templates again…

Could it be either of these cards is having issues with HA 105?

this is the config for the cards:

  - type: custom:auto-entities
    card:
      type: entities
      title: Github repos
      show_header_toggle: false
    filter:
      include:
        - entity_id: sensor.github*
          options:
            type: custom:template-entity-row
            state: >
              {% if state_attr(config.entity,'latest_release_url') %}
              {{ state_attr(config.entity,'latest_release_url').split('tag/')[1]}}
              {% else %} Not set
              {% endif %}
            secondary: >
              {{ state_attr(config.entity,'latest_commit_message')}}

which has been showing just fine up to now like posted here

This is great, thanks. It would be even better if it behaves like a normal card. So when I click it, it would be nice when it opens the entity. Is this possible too?

Something like tap_action in the glance card.

1 Like