Template to see which automations ran in last x mins

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.

we just cross posted and both came to the same conclusion why it wasn’t working and why int fixed it…

2 Likes

I think we both deserve the right to pat ourselves on the back for that simultaneous flash of insight. :slight_smile:

2 Likes

Is it possible to use this as a sensor too?

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 -%}

You need to set N entity id for it to update on or call a manual update,

It won’t see an entity id automatically

In your startup logs it should tell you this.

Thx, is there an entity you can suggest for this. Maybe the time sensor?

Yes, date time would work - you’ll want something fairly frequent to ensure the template updates.

sensor.time will trigger the template sensor every minute.

Alternately, if you only have a few automations, or only wish to track a few automations, you can list them in entity_id.

entity_id: automation.first_automation, automation.second_automation, automation.third_automation

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:

{{state.attributes.friendly_name}}, {{last|timestamp_custom("%H:%M:%S")}};

Given this template sensor:

      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 -%}

It produces this for its state:

Screenshot%20from%202019-03-31%2010-16-32

1 Like

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?

Trying to get this working as I have an automation running I cannot find. What is the best code to use in the template. editor?

if you don’t need a dedicated sensor, you could also do this in a Lovelace card:

  - type: custom:auto-entities
    card:
      type: entities
      title: All Automations on
      show_header_toggle: false
    filter:
      include:
        - domain: automation
          state: 'on'
          options:
            secondary_info: last-triggered
    sort:
      method: attribute
      attribute: last_triggered
      reverse: true
2 Likes