Display recently triggered automations

Well I seem to have “ghosts” from long removed automations…
Removed them via developper tools and now they’re gone for good. Thanks ^^

If you create an automation using a text editor, and provide it with an id, it will be registered by Home Assistant and traces will be created for it whenever it triggers. If you don’t provide an id, traces will not be created for it and it won’t be registered.

If you then delete the automation’s code from the file and either restart Home Assistant or execute Reload Automations, the automation is effectively gone but it’s not deregistered. It will still appear in the list of automations as a gray “ghost” entry. You have to remove the “ghost” via the UI (click its information icon, etc).

Still really hoping that someone could have a look at this and advise as to what’s wrong with the way I’ve got it coded?

What about only showing the single last triggered automation from a list of defined automations.

Show me what you have created so far and I’ll help you complete it.

In case anyone else was looking, I did manage to figure out how to get it to show correctly.

- type: markdown
  title: Zones Triggered
  content: |
    |D|H|M||Time||Name|  
    |---:|----:|----:|:--:|:---:|:--:|:----|  
    {% for state in expand('group.ad2pi_sensors') 
       | sort(attribute='last_updated', reverse=true) -%}
      {%- set t1 = state.last_updated.timestamp() | timestamp_custom('%H:%M') -%}
      {%- set t = now() - state.last_updated -%}
      {%- set days = t.days if t.days > 0 else ' ' -%}
      {%- set hrs = t.seconds//3600 %}
      {%- set hrs = hrs if hrs > 0 else ' ' -%}
      |{{days}}|{{hrs}}|{{(t.seconds//60)%60}}||{{t1}}||_{{state.name}}_|
    {% endfor %}

Changing all the ‘last_changed’ to ‘last_updated’ did the trick.

Still trying to figure out how to do the same thing in this markdown card style, but for listing out all the changes to a state for a single state. Things that I could apply this to would be the opensky flight callsign (see this thread) or showing a listing of the latest 3d printed filenames fed from Octoprint

I loved this! The 2022.6 update seems to have broken my implementation of it though.
Anyone else?

Same here…

I’m using this code that is still working on 2022.6.0 2022.6.2:

# Automations of the last 8 hours, but do not display more than 40 

        **Zeit      Name**
        {% for state in (states.automation
          | rejectattr('attributes.last_triggered', 'in', ['None', 'none', 'unknown'])
          | selectattr('attributes.last_triggered')
          | selectattr('state', 'ne', 'unavailable')
          | sort(attribute='attributes.last_triggered', reverse=true)) [0:40] -%}
          {% if state.attributes.last_triggered and (now() - state.attributes.last_triggered).total_seconds() < 28800 %}
            {%- set t = (as_timestamp(state.attributes.last_triggered) | timestamp_custom('%H:%M', true)) -%}
            {{t}} &nbsp;&nbsp; _{{state.name}}_
          {% endif -%}
        {% endfor %}

Thanks! I tried your code and got no output also. This is my formerly working code.

|H|M||Name|
|----:|----:|:--:|:----|
{% for state in states.automation
 |selectattr('attributes.last_triggered')
 |sort(attribute='attributes.last_triggered', reverse=true)
if (now() - state.attributes.last_triggered).total_seconds() < 43200 %}
{%- set t = now() - state.attributes.last_triggered -%}
{%- set days = t.days if t.days > 0 else '&nbsp;' -%}
{%- set hrs = t.seconds//3600 %}
{%- set hrs = hrs if hrs > 0 else '&nbsp;' -%}
  |{{hrs}}|{{(t.seconds//60)%60}}||_{{state.name}}_|
{% endfor %}

Copied your code into a Markdown Card and is working fine… strange.

for me on 2022.6.4 I am getting this error when trying it in the template editor

UndefinedError: ‘homeassistant.util.read_only_dict.ReadOnlyDict object’ has no attribute ‘last_triggered’

I don’t have the link at hand, but there is a bug in the template editor causing this in HA version 2022.6.

UPDATE: Template error in Template Editor while it works fine as custom template sensor · Issue #71360 · home-assistant/core · GitHub

You can try the suggestion in that report to filter on certain attributes being defined before using their values.

1 Like

The state of one of your automations is unknown (go to Developer Tools → States and confirm it) and so it doesn’t have a last_triggered attribute. The existing template doesn’t handle that situation gracefully.

I suggest you add a filter to select only automations that have a defined last_triggered attribute (it’s the same advice parautenbach offered).

{% for state in states.automation
 |selectattr('attributes.last_triggered', 'defined')
 |selectattr('attributes.last_triggered')
... etc ...
3 Likes

… add a filter to select only automations that have a defined last_triggered attribute (it’s the same advice parautenbach offered
^ IS the solution

Excellent, thank you!

Not sure if this is still working to date ? If so can someone post there working script?

The only significant modification to the original code was described on June 9 (three posts before your post). To prevent an error, it ensures it only references automations that have a last_triggered attribute.

If you want it to be very thorough about which automations are excluded/included, you can add the following three selectattr filters to the template:

{% for state in states.automation
 | selectattr('state', 'ne', 'unavailable')
 | selectattr('attributes.last_triggered', 'defined')
 | selectattr('attributes.last_triggered', 'ne', None)
  ... etc ...

Just wanted to say thank you to all of you for providing me this super tool, also for debugging.

Hi this isn’t working for me. I am running 2022.11.4 Core and get the error:
TypeError: '<' not supported between instances of 'str' and 'datetime.datetime'

The exact same code works fine for sensors though.
The following does work:

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

I’m not sure if it is relevant, but when I ran the following, I got 2 different formats of timestamp back - one with a ‘T’ between date and time, and one without.

{{ states.automation
  | selectattr('attributes.last_triggered')
  | map(attribute='attributes.last_triggered')
  | list | join(', ')  }}

produces:

2022-11-27 09:14:34.558767+00:00,
2022-11-13T09:05:19.314270+00:00,

Any ideas?