Displaying automations by last triggered

So lately I’m creating a bunch of automations, testing and sometimes I like to double-check and make sure they triggered correctly.

I am using the template below to see the automations trigged in the last 5 hours:

{% set mins = 360 %}
{%- for state in states.automation|sort(attribute='last') -%}
  {% set last = as_timestamp(state.attributes.last_triggered) %}
  {% set current = as_timestamp(now()) - (mins*60) %}
  {%- if (last != None) and last > current -%}
    {{state.entity_id}}: 
       last triggered: {{last|timestamp_custom("%H:%M") }}
  {% endif %}
{%- endfor -%}

It results in something like this:

automation.someone_arrived:
last triggered: 12:57

automation.disarm_alarm:
last triggered: 12:57

automation.sunset_lights:
last triggered: 17:21

automation.announce_visitor:
last triggered: 16:40

automation.dark_mode:
last triggered: 18:20

What I’d like to see is the last 5 or 10 automations sorted by when they were triggered, with the newest entry on top.

Is there any sorting I can add to this template? Any add-on I should look for? A new automation?

Thanks in advance!

did you give auto-entities a try? it can sort based on attributes, and also use count https://github.com/thomasloven/lovelace-auto-entities#sorting-entities

you wouldn’t need a backend template at all

Thanks a lot! I installed auto-entities and already got it working just like I wanted, thanks for pointing the way!

In case anyone wants to do the same, after installing auto-entities I add the following custom card:

type: 'custom:auto-entities'
show_empty: false
card:
  type: entities
  title: Last five automations
  show_header_toggle: false
filter:
  include:
    - domain: automation
      options:
        secondary_info: last-triggered
sort:
  method: last_triggered
  count: 5

I’ll just have to figure if I can turn off the toggle.

2 Likes

ever figure out how to turn off the toggles?

Hey, overtime I changed to using the logbook card which shows more details and no buttons. It’s quite similar:

card:
  type: logbook
  hours_to_show: 1
  title: Histórico
filter:
  include:
    - domain: automation
      options:
        secondary_info: last-triggered
    - domain: script
      options:
        secondary_info: last-triggered
show_empty: false
type: custom:auto-entities
view_layout:
  position: sidebar

3 Likes