Last triggered template suddenly gives an error: TypeError: '<' not supported between instances of 'str' and 'datetime.datetime'

Hi,

Hopefully someone has a pointer for me why the following template does not work anymore in once HA instance (it still works in my second HA instance)

I get the error: TypeError: '<' not supported between instances of 'str' and 'datetime.datetime'

I have tried a lot to make it work again, I think it’s related to some state of “last_triggered” being in a wrong format somehow… But I am unsure.

I use the code in the developer tools:

|D|H|M||Name|
|----:|----:|----:|:--:|:----|
{% for state in states.automation
   | selectattr('state', 'ne', 'unavailable')
   | selectattr('attributes.last_triggered', 'ne', None)
   | selectattr('attributes.last_triggered')
   | sort(attribute='attributes.last_triggered', reverse=true) -%}
  {%- 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;' -%}
  |{{days}}|{{hrs}}|{{(t.seconds//60)%60}}||_{{state.name}}_|
{% endfor %}

Any pointers would be very much appreciated!

Check all your triggered automation timestamps with:

{% for state in (states.automation
 | selectattr('attributes.last_triggered','defined')) -%}

 {%- set t = state.attributes.last_triggered -%}
 {{ t }}
{% endfor %}

I found a few with weird execution times! corrected those and it running again… don’t know how they got in a wrong state…