Templating a list of entities

I’m trying to list all automations that have triggered in the last 4 hours, but cannot get it working.

This is what I want, and renders perfectly in developer tools, but won’t render at all in a card.

type: entity-filter
entities: {% for automation in states.automation|selectattr('attributes.last_triggered')|selectattr('attributes.last_triggered','gt',now()-timedelta(hours=4)) %}
  - {{ automation.entity_id }}{% endfor %}
state_filter:
  - 'on'
card:
  type: entities

I’ve also tried card-templater, slightly modifying an example config:

type: custom:card-templater
card:
  type: entities
  entities_template: >-
    {{ states.automation | selectattr('last_triggered') |
    map(attribute='entity_id') | list | tojson }}

Neither are working. What am I missing?

1 Like

In many (probably all) cases, cards do not allow templating

So…not all :slight_smile: … look at this one, last one on the page
thomasloven/lovelace-auto-entities: :small_blue_diamond:Automatically populate the entities-list of lovelace cards (github.com)

I fixed it for you… :wink:

For your consideration:

1 Like

Thanks, the built-in I missed … :slight_smile:

1 Like

This should be all you need:

{{ states.automation | selectattr('attributes.last_triggered', 'defined') | rejectattr('attributes.last_triggered','none') | selectattr('attributes.last_triggered', '>', now() - timedelta(hours=4)) | map(attribute='entity_id') | list }}

EDIT: You’d need to use this with auto-entities custom card btw.

2 Likes

Love this! I was just considering resorting to a markdown card before going to bed last night. Since I’m just looking for easy access to modify automations on the fly, I’ve modified this to include links to each automation’s trace:

|Automation|Last Triggered|
|:-|----:|
{% for state in states.automation | selectattr('attributes.last_triggered','defined') | rejectattr('attributes.last_triggered','none') | selectattr('attributes.last_triggered', '>', now() - timedelta(hours=4)) | sort(attribute='attributes.last_triggered', reverse=true) -%}
  {%- set t = relative_time(state.attributes.last_triggered)+' ago' -%}
  |[{{state.name}}](/config/automation/trace/{{state.attributes.id}})|_{{t}}_|
{% endfor %}

Here it is in all its glory:
image

1 Like

I’ve marked your answer as the solution despite ultimately going another route, because I suspect future search engine voyagers will find yours most relevant.

1 Like