Display recently triggered automations

no such thing as coincidence :wink: as a matter of fact I had been experimenting with the exact same thing in mind, and tried:


{% for state in expand('group.doors')| selectattr('last_changed') | sort(attribute='last_changed', reverse=true) -%}
{{ state.last_changed.timestamp() | timestamp_local }} : {{ state.name }}
{% endfor %}

only to find out, this doesnt exactly do what I had hoped for. It lists all members of the group, and shows their last changed time. What it doesnt do is show a historic list of the last opened doors.

this is what happens, and Ive been opening and closing the front door and hallway door more than a few times:

probably per design, just different from what I had hoped for

If you mean like the Logbook then a template can’t do that because it only has access to last_changed and nothing prior to that. Logbook uses the database to show the history of a door’s openings/closings.

yes I see that (now)… Because I did want to see the historic order, I quickly made a mockup using the Custom integration Variable, which can do that:

this way a quick glance of the intruders last motion is recorded :wink:

1 Like

LoL and I am going to that what I was hoping to achieve is what Marius already had in mind … which was a “log”. So if I open the front door, then the back door, then the front door again – this doesn’t show the order of things, just the “last changed” so each sensor would never show up more than once.

Anyhow, I’m still very happy with a list of the last changed states for the sensors, and just wanted to say that this bit of markup was written so well to be easily adaptable for other than just the original intent, which was for “automations” triggered.

If there is a simple way of making this more like a log instead, I’d be all over it too. If not, still more than happy as it is. Thanks again!

For future reference, templates have access to an entity’s state and attributes within Home Assistant’s State Machine (predominantly, the current state of all entities). Templates don’t have access to the database where the history of an entity’s previous states is stored.

FWIW, the SQL Sensor integration allows you to query the database (any database) and display the result. However, I don’t have any experience with it and I am not sure it can be used to display multiple results in the manner you want.

if you open a new thread, Ill be happy to post what I have now, and it works just as you describe. But don’t want to further derange 123’s thread.

Thats really neat. However, a real newbie question… please forgive me

I add a markdown card, select “show code editor” and am presented with:

"type: markdown
content: >-
The Markdown card allows you to write any text. You can style it bold,
italicized, ~strikethrough~ etc. You can do images, links, and more.

For more information see the Markdown
Cheatsheet
."

I delete the three lines unto the line that starts " The Markdown" and then paste in the code starting with |D|H|M| as per the example posted.

It doesn’t work - I’ve obviously missed something, and don’t really know how to use the markdown card you have posted. Please point me in the right direction.

Many thanks,

The sample text you see in a newly created Markdown card should be replaced. Not just the first few lines of it, all of it.

Thank you so much. That worked. I don’t know I didn’t work that out before… very much appreciate your help.

Has something recently broken the formatting of the markdown or was there something wrong I did in the first place?

The 2 pieces of code below has been working fine for several months. First set of code works perfectly fine, but the second set of code was modified based on expand(‘group.xxx’) .
The second piece also uses last_changed instead of last_triggered from the original code.

          - type: markdown
            title: Automations Triggered
            content: |
              |D|H|M||Time||Name|
              |---:|----:|----:|:--:|:---:|:--:|:----|
              {% for state in states.automation
                 |selectattr('attributes.last_triggered')
                 |sort(attribute='attributes.last_triggered', reverse=true) -%}
                {%- set t1 = state.attributes.last_triggered.timestamp() | timestamp_custom('%H:%M') -%}
                {%- set t = now() - state.attributes.last_triggered -%}
                {%- 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 %}
          - type: markdown
            title: Zones Triggered
            content: |
              |D|H|M||Time||Name|  
              |---:|----:|----:|:--:|:---:|:--:|:----|  
              {% for state in expand('group.ad2pi_sensors') 
                 |sort(attribute='last_changed', reverse=true) -%}
                {%- set t1 = state.last_changed.timestamp() | timestamp_custom('%H:%M') -%}
                {%- set t = now() - state.last_changed -%}
                {%- 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 %}

But what i see is this:

Did this ever happen?

The new thread is over here, but no, not yet :slight_smile:

Edit: Has happened, but unfortunately not quite what I was looking for with using a custom card.
Still would prefer these markdown cards instead.

Could anyone point me to what’s wrong with the formatting of the Zones Triggered markdown above?

I’ve been having a lot of warnings in the logs with the list of recently triggered automation because all automation do not have a last_triggered attribute…
One only needs to add selectattr(‘attributes.last_triggered’, ‘defined’) to filter them out.

{% for state in (states.automation | selectattr('attributes.last_triggered', 'defined') | selectattr('attributes.last_triggered') | sort(attribute='attributes.last_triggered', reverse=true)) if (now() - state.attributes.last_triggered).days < 1 -%}
{{ state.attributes.last_triggered.timestamp() | timestamp_custom('%d/%m %H:%M:%S') }} : {{ state.name }}
{% endfor %}

Only a newly created automation will lack a last_triggered attribute.

Oh really ? That’s weird, there must be other cases… Is the attribute removed or reset at reboot ? Any link with history database ?
I guess I should dig deeper to find which automations generate those warnings

No, the last_triggered attribute is unaffected by a restart (and preserves its value on startup).

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.