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

This plugin lets you add a row to an entities card that you can make say anything you’d like by using Home Assistants jinja2 backend templating engine.

Want to display an attribute of an entity rather than it’s state? This might be for you.

11 Likes

yes! magic!
hope to be able to do this now, or is this not yet available for the fold-entity-row?

anyways, getting better in very iteration… :+1:

would you allow to change(template) the active color also? would love to be able to create some alert entities using red or other signaling colors to stand out. Might a bit early for a feature request… sorry if I seem greedy, this is just almost too good to be true now. Thanks!

thanks for sharing, very interesting!
have a question - how to add tap action to entity?

Would it be possible to add an entity option so that lovelace will display all of that entity’s attributes by default but default values can be overriden if user-defined. So the yaml could look like so:

entities:
  - type: 'custom:template-entity-row'
    entity: person.john_smith
    # icon not defined, so use person.john_smith's default icon
    secondary: "{% if is_state('person.john_smith') == 'home' || is_state('sensor.john_smith_proximity') == 'unknown') %} '' {% else %} states('sensor.john_smith_proximity') + ' minutes from home' {% endif %}"

Update: moving this to feature request in github

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