Broken Markdown card

I do have a markdown card on my dashboard which displays triggered automations in the last 24 hours (with timestamp).
It worked perfectly fine until the last update of HA.
Now, the card is empty.
And I can’t figure out why…
So if someone is able to tell me what needs to be changed, it would be greatly appreciated.
this is the code behind the card :

type: markdown
content: |-
  {% for state in
  states.automation|sort(attribute='attributes.last_triggered',reverse=true) %} 
    {% if (now() - state.attributes.last_triggered).total_seconds() < 86400 %}
      {%- set t = (as_timestamp(state.attributes.last_triggered)|timestamp_custom('%H:%M', True)) -%}
      **{{t}}** &nbsp;&nbsp; _{{}}_
    {% endif %}
  {% endfor %}

If you have an automation that has never triggered, the value of its last_triggered is null. That causes an error in the template when it attempts to perform the calculation determining if last_triggered is within the last 24 hours.

The template should select automations whose last_triggered value is not none. In addition, it should probably only select automations that are on (thereby excluding any automations that may have triggered in the past 24 hours but were subsequently turned off).

The following template was tested and confirmed to work on my system containing several automations that have never triggered and others that have been turned off.

type: markdown
content: |
  {% for state in states.automation 
    | selectattr('attributes.last_triggered', 'ne', none)
    | selectattr('state', 'eq', 'on')
    | sort(attribute='attributes.last_triggered', reverse=true)
    if now() - state.attributes.last_triggered < timedelta(hours=24) %}
    {%- set t = state.attributes.last_triggered.timestamp() | timestamp_custom('%H:%M', True) -%}
    **{{t}}** &nbsp;&nbsp; _{{}}_
  {% endfor %}

Works like a charm !!
Many thanks (for fixing AND improving my basic version)

1 Like