Template help - list based off attribute state (running/active automations)

Needing some help on a template to list all running automations.

One similar to the below, however, will utilise the “Current: 1” attribute state.

Where… automation…
And… attribute: Current = 1

{%- for light in states.light -%}
          {%- if light.state == 'on' -%}
          {{ light.entity_id }},
          {%- endif -%}
          {%- endfor -%}

If you only want to display this information in the front end you can use the auto-entities custom card (I also use the fold entity row card):

Screenshot 2021-12-29 at 11-19-46 Administration - Home Assistant

entities:
  - card:
      head:
        label: Disabled Automations
        type: section
      padding: 0
      type: custom:fold-entity-row
    filter:
      exclude:
        - state: 'on'
        - state: unavailable
      include:
        - domain: automation
    sort:
      ignore_case: true
      method: name
    type: custom:auto-entities
  - card:
      head:
        label: Missing Automations
        type: section
      padding: 0
      type: custom:fold-entity-row
    filter:
      exclude:
        - attributes:
            current: '= 0'
        - attributes:
            current: '= 1'
      include:
        - domain: automation
    show_empty: true
    sort:
      ignore_case: true
      method: name
    type: custom:auto-entities
  - card:
      head:
        label: Running Automations
        type: section
      padding: 0
      type: custom:fold-entity-row
    filter:
      exclude:
        - state: unavailable
        - attributes:
            current: '= 0'
      include:
        - domain: automation
    show_empty: true
    sort:
      ignore_case: true
      method: name
    type: custom:auto-entities
  - card:
      head:
        label: Running Scripts
        type: section
      padding: 0
      type: custom:fold-entity-row
    filter:
      exclude:
        - state: 'off'
      include:
        - domain: script
    show_empty: true
    sort:
      ignore_case: true
      method: name
    type: custom:auto-entities
show_header_toggle: false
title: Autofilled Lists
type: entities
3 Likes

As always, very useful, Tom, thanks.

However, I want to use the template for a ‘widget’ on an Android device.

Hmmm, maybe it can be done by checking the current value:
Just a PoC:

{% for state in states.automation -%}
  {% if state.attributes.current != 0 %}
     {{ state.entity_id  }}
   {% endif -%}
{%- endfor %}

One can make it better en nicer, but I hope it helps in finding a suitable solution.

{{ states.automation | selectattr('attributes.current', 'eq', 1) | map(attribute='entity_id') | list }}

1 Like

That’s indeed nicer than my PoC syntax. I’d however suggest selectattr('attributes.current', 'gt', 0) instead of selectattr('attributes.current', 'eq', 1), cause automations can run parallel, and most likely setting the current to more than 1.

1 Like

I agree … but it seems like the author of this topic has, as they say, “left the chat”.

I wanna thank you for this, both 123 and Recte. 123’s answer could/should be marked as solution/answer for this topic. :white_check_mark:

I was looking for the same thing and I’m happy to find this topic. I want to push this one step further:

"Give me the number of automations which have been triggered (were running) during the last 24 hours"

Can I achieve this by a simple template sensor? I guess I would somehow need to cross-check/compare the last_triggered time with the (current minus 24 hours) time, right? Anyone with a syntax PoC for this?

since midnight

{{ states.automation | selectattr('attributes.last_triggered', 'defined') | selectattr('attributes.last_triggered', '>', today_at()) | map(attribute='entity_id') | list }}

24 hours

{{ states.automation | selectattr('attributes.last_triggered', 'defined') | selectattr('attributes.last_triggered', '>', now() - timedelta(days=1)) | map(attribute='entity_id') | list }}
1 Like

Unfortunately both give me

TypeError: '>' not supported between instances of 'NoneType' and 'datetime.datetime'

It’s not a syntax thing as ‘gt’ gives the same result. Data type mismatch?

The reason it doesn’t work likely is that “you” are comparing two different types.
If you enter {{ states.automation | map(attribute="attributes.last_triggered") | list}} you will notice that it shows something like:

[datetime.datetime(2022, 5, 30, 5, 4, 21, 439137, tzinfo=datetime.timezone.utc), ...

The result of {{ now() - timedelta(days=1) }} is something like:
2022-06-01 10:32:05.118346+00:00

My “solution” broke in 2022.6, so I removed it. I don’t have time to puzzle for a solution at the moment.

If I leave out ‘defined’ then it works. :grinning:

{{ states.automation | selectattr('attributes.last_triggered') | selectattr('attributes.last_triggered', '>', now() - timedelta(days=1)) | map(attribute='name') | list | join('\n') }}
{{ states.automation | selectattr('attributes.last_triggered') | selectattr('attributes.last_triggered', '>', today_at()) | map(attribute='name') | list | join('\n') }}

Without that test it will include automations that lack a last_triggered attribute (new automations that have never been triggered have no last_triggered). As a consequence, the template will fail when it processes the new automation while testing if its non-existent last_triggered is greater than today_at().


EDIT

Correction. New, never-triggered automations do have a last_triggered attribute but its value is null (none). It’s unavailable automations that lack a last_triggered attribute (a rare category of automations).

It doesn’t. even with a lot automations that doesn’t have the Attribute last_triggered the template works as expected. :man_shrugging: :grinning:

You’re right; my mistake. The issue arises for automations whose state is unavailable and genuinely don’t have a last_triggered attribute (it doesn’t occur for automations that have never been triggered because they do actually have a last_triggered attribute but its value is simply null).

Here’s what happens when the template encounters an unavailable automation:

The following template excludes unavailable automations (no last_triggered attribute) and never-triggered automations (last_triggered has no value).

I had a similar question that @tom_l answered. His answer includes a “Missing Automations” section and I do have 1 listed in there. And I am puzzled. I believe it’s an old automation that I ended up replacing by another automation (renamed? or created a new one? don’t remember). So I guess my question is, why is this automation “missing” and how can I make it go away?

Go to Settings → Devices & Services → Entities tab. Find the “restored” automation and delete it.

You have to do this whenever you delete a yaml automation that has a unique id.