Display recently triggered automations

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.

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 '&nbsp;' -%}
      {%- set hrs = t.seconds//3600 %}
      {%- set hrs = hrs if hrs > 0 else '&nbsp;' -%}
      |{{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 &nbsp;&nbsp;&nbsp;&nbsp; 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 %}