I agree; it was a desperate theory to explain why the template might not be working consistently across various systems.
I agree the original version of my template was flawed. It wasn’t tested with a comprehensive data set (i.e. it worked for me at the time I tried it on my test server but no further). I’ve amended my post and applied Klogg’s correction.
I think I know why int worked for you. The as_timestamp had evaluated to None and int cast that to 0. That makes last > current work without failing.
The example from above works for me, but I the state stays empty when I use it as a sensor like this:
- platform: template
sensors:
last_automation:
value_template: >-
{% set mins = 60 %}
{%- for state in states.automation -%}
{% set last = as_timestamp(state.attributes.last_triggered)|int %}
{% set current = as_timestamp(now()) - (mins*60) %}
{%- if last > current -%}
{{state.entity_id}}:
last triggered: {{last|timestamp_custom("%H:%M") }}
{% endif %}
{%- endfor -%}
Now the template sensor will fire only if one of the listed automations runs (well, to be precise, if there’s any change to its state or any of its attributes).
Given that the template is now part of a template sensor, I suggest you modify what it reports to the sensor’s state. This produces a compact result:
last_automation:
entity_id: automation.test_automation, automation.reset_sensor2_topic
value_template: >-
{% set mins = 5 %}
{%- for state in states.automation -%}
{% set last = as_timestamp(state.attributes.last_triggered)|int %}
{% set current = as_timestamp(now()) - (mins*60) %}
{%- if last > current -%}
{{state.attributes.friendly_name}}, {{last|timestamp_custom("%H:%M:%S")}};
{% endif %}
{%- endfor -%}
I have to many automations to list them, so I will go with sensor.time
Thanks
EDIT:
Ok so it works like this now, but I had to go down to 5 min (instead of 60 mins) to be able to see an output.
Specially after restarting ha the state will stay empty because there are many automations triggered and I think the number of the characters in this list exceeds the maximum number of characters (I think 255 characters).
Is there a better way to visualise this?