#List time since last activation for select automations

1 messages ยท Page 1 of 1 (latest)

tidal yoke
#

I would like to add a tile to my dashboard that lists how long ago a selection of automations were run, and have them disappear if that time is greater than 24 hours.

Any suggestions on how to go about doing this?

proper kestrel
#
type: markdown
content: |-
  {% for state in states.automation -%}
    {% if state.attributes['last_triggered'] != none -%}
      {% if (now() - state.attributes['last_triggered']) < timedelta(days=1) -%}
        {{ state.name }} - {{ ((now() - state.attributes['last_triggered'])|string).split('.')[0] }} ago
      {% endif -%}
    {% endif -%}  
  {% endfor -%}
title: Triggered Automations from last 24h
#

that is not a great example but maybe gives you an idea of an approach to take?

old shadow
#

probably doable with auto entities card too

old shadow
#

took me a while lol but there you go :

type: custom:auto-entities
filter:
  template: >-
    {% set list = states.automation %}{% for element in list %}{% set
    element_trigger = (now() - state_attr(element.entity_id, "last_triggered"
    )).total_seconds() <= 86400 %}{% if element_trigger %}{{
    element.entity_id~"\n" }}{% endif %}{% endfor %} 
show_empty: true
card:
  type: entities
  show_header_toggle: false
#

this card (using mushroom auto entities) list all automation triggered in the last 24h

sage patio
#

Man. I started writing a response earlier but wasn't able to finish it. Y'all already got it taken care of. ๐Ÿ‘

tidal yoke
#

Also, I do have Mushroom installed from HACS, but I'm not able to get this YAML to work. I just get a blank box.

old shadow
#

try the template in the dev tabs to see if it gives any entity id

#

works on my side but heh, maybe all your automations last trigger date are over 24 hours ago

#

if you wanna display only some automation this template is not the best tho.
if you wanna just hide some automation just replace the template with that and populate the list exclusions with your automations to ignore

{% set exclusions = ["AUTOMATION  1 ID", "AUTOMATION 2 ID", .......] %}{% set list = states.automation | rejectattr('entity_id', 'in', exclusions) %}{% for element in list %}{% set
    element_trigger = (now() - state_attr(element.entity_id, "last_triggered"
    )).total_seconds() <= 86400 %}{% if element_trigger %}{{
    element.entity_id~"\n" }}{% endif %}{% endfor %} 
sage patio
#

@old shadow Neither of your examples worked for me. Initially, it seems like there's something wrong with the element_trigger as it would not return properly in Dev Tools. For me, this is what it took to return: {{ as_timestamp(now()) - as_timestamp(state_attr('automation.arriving_leaving_home', 'last_triggered')) }}

old shadow
#

idk then

#

can't understand why it's not working for you guys, works great on my side

#

but sure you can transform the datetime values to timestamps and it'll work great too

old shadow
sage patio
#

Weird question: what time zone are you in? I'm GMT-4. Are you in GMT?

old shadow
#

CEST/UTC+2

#

should not impact tho

#

timedate variables take in consideration time zones

tidal yoke
old shadow
#

yeah my message was very unclear, use this:

type: custom:auto-entities
filter:
  template: >-
    {% set list = states.automation %}{% for element in list %}{% set element_trigger = as_timestamp(now()) - as_timestamp(state_attr(element.entity_id, 'last_triggered')) <= 86400 %}{% if element_trigger %}{{ element.entity_id~"\n" }}{% endif %}{% endfor %}  
show_empty: true
card:
  type: entities
  show_header_toggle: false
old shadow
#

see if you get results on the right

#

or any error

#

if you don't get any result, try this :

{% set list = states.automation %}{% for element in list %}{% set element_trigger = as_timestamp(now()) - as_timestamp(state_attr(element.entity_id, 'last_triggered')) >= 0 %}{% if element_trigger %}{{ element.entity_id~"\n" }}{% endif %}{% endfor %}  
tidal yoke
#

Both of these result in a ValueError result.

sage patio
#

I'm at a loss as to why this is not working for me either. The whole date/time part returns fine (if I hard-code an entity) in Dev Tools. The list automation domain works (when I output the for to a list).

old shadow
#

I cant understand why its not working for you guys

#

The code looks fine and return expected values on my side

old shadow
#

Could you screenshot it ?

old shadow
#

Try this and post the error if there is any

{% set exclusions = [""] %}{% set list = states.automation | rejectattr('entity_id', 'in', exclusions) %}{% for element in list %}{% set
    element_trigger = (now() - state_attr(element.entity_id, "last_triggered"
    )).total_seconds() <= 86400 %}{% if element_trigger %}{{
    element.entity_id~"\n" }}{% endif %}{% endfor %}
tidal yoke
old shadow
#

Try this

{{state_attr("AUTOMATION ID", "last_triggered")}}

AND REPLACE AUTOMATION ID with the id of one of your automation

#

Give me the result and the result type if it does not give you an error

tidal yoke
old shadow
#

Hmm

#

Try this

{{now()}}

#

Your error has shown that one the the two values return as null

#

Or maybe you have an automation that has nevers been activated

#

Thats probably the reason now that i think about it

#

Could you check if an automation has never been triggered and add it in the exclusion array

tidal yoke
tidal yoke
old shadow
#

Thats it then

tidal yoke
#

How do I add to the exclusion tray?

old shadow
#

I could not get why it did not work with you guys

tidal yoke
#

Or better yet, can I just use explicit inclusion?

old shadow
#

Ill get to my pc and change tge yaml for you to fix that

#

Yup

tidal yoke
#

Thank you!

old shadow
#

try this:

{% set exclusions = [""] %}{% set list = states.automation | rejectattr('entity_id', 'in', exclusions) %}{% for element in list %}{% set element_last_trigger = state_attr(element.entity_id, "last_triggered") %}{% if element_last_trigger == none %} {% continue %}{% endif %}{% set element_trigger = (now() - element_last_trigger).total_seconds() <= 86400 %}{% if element_trigger %}{{element.entity_id~"\n" }}{% endif %}{% endfor %}

I tried it with an automation that never triggered

#

should work

#

if it gives you a list of ids, you can then use the card as bellow:

type: custom:auto-entities
filter:
  template: >-
    {% set exclusions = [""] %}{% set list = states.automation | rejectattr('entity_id', 'in', exclusions) %}{% for element in list %}{% set element_last_trigger = state_attr(element.entity_id, "last_triggered") %}{% if element_last_trigger == none %} {% continue %}{% endif %}{% set element_trigger = (now() - element_last_trigger).total_seconds() <= 86400 %}{% if element_trigger %}{{element.entity_id~"\n" }}{% endif %}{% endfor %}
show_empty: true
card:
  type: entities
  show_header_toggle: false
#

(could have used the rejectattr fonction instead of a IF test at each for iteration , but could not get it to work, lazyness got me there)

tidal yoke
#

Okay, so this shows a list of automations, but two issues. One, it just shows them with a toggle rather than listing when they last ran. Two, it's exclusion-based rather than inclusion-based.

old shadow
#

Ill make the modification later if you want

tidal yoke
sage patio
#

After reading through y'alls progress, an automation that has never triggered makes sense (because it does not have a datetime). ๐Ÿคฆโ€โ™‚๏ธ
I had a similar problem when working with a template for Grocy items. One item did not have a category so it totally broke everything...

old shadow
#

There you go, it's now inclusif instead of exclusif and display the infos you want:

type: custom:auto-entities
show_empty: false
filter:
  template: |-
    {% set ns = namespace(select=[]) %}
        {% set list=["ANTOMATION ID #1", "ANTOMATION ID #2"]%}
          {% for entity in list %}
          
          {% set entity_last_trigger = state_attr(entity, "last_triggered") %}
          {% if entity_last_trigger == none %} {% continue %}
          {% endif %}
          {% set entity_last_trigger_date = (now() - entity_last_trigger).total_seconds() > 86400 %}
          {% if entity_last_trigger_date %} {% continue %}
          {% endif %}
          
          {% set icon = state_attr(entity, "icon") %}
          {% if icon == none %} {% set icon = "mdi:robot" %}
          {% endif %}
          {% set ns.select = ns.select + [{
              'type': 'custom:mushroom-template-card',
              'icon': icon,
              'primary': state_attr(entity, "friendly_name"),
              'secondary': state_attr(entity, "last_triggered").strftime('%d/%m/%Y %H:%M'),
              'icon_color': "primary"
          }] %}
        {% endfor %} {{ ns.select }}

All you have to do is replacing the "AUTOMATION ID #1" and "AUTOMATION ID #2" with your automations id (you can add as many as you like)

#

using mushroom-template-card

#

since you aleady have the mushroom card addon

tidal yoke
#

Thank you! I'll give this a try after I get home from work.

old shadow
#

it probably need some optimisations tho i'm a novice at this

tidal yoke
#

This is super close! The only thing is that it shows the time it last triggered (and, actually, is wrong - it says that my automation that triggered at midnight triggered at 22:00) rather than the time since it last triggered. I tried tinkering with it to change that, but I'm just too lost.

old shadow
#

yeah the now() is inaccurate, it does not use local timezone

#

saw that just after sending it

tidal yoke
#

Oh, that's super weird

old shadow
#

yup, idk why but it's the reason

#

it's a quick fix tho

#

i'm not sure to get what you want tho, the time it last triggered or the time since it triggered ?

tidal yoke
#

since

old shadow
#

๐Ÿ‘Œ

tidal yoke
#

If it matters, my timezone is central europe

old shadow
#

replace 'secondary': state_attr(entity, "last_triggered").strftime('%d/%m/%Y %H:%M'), by ```'secondary': time_since(as_datetime(state_attr("automation.calcifer_activation_automatique_brumisateur", "last_triggered"))) ,

#

not tested tho

#

but should work

#

Hm lol

#

Thats the id of one of my entitie

#

Replace the id with entity

tidal yoke
#

๐Ÿ‘

#

That works!

#

type: custom:auto-entities
show_empty: false
filter:
template: |-
{% set ns = namespace(select=[]) %}
{% set list=["automation.midnight", "ANTOMATION ID #2"]%}
{% for entity in list %}

      {% set entity_last_trigger = state_attr(entity, "last_triggered") %}
      {% if entity_last_trigger == none %} {% continue %}
      {% endif %}
      {% set entity_last_trigger_date = (now() - entity_last_trigger).total_seconds() > 86400 %}
      {% if entity_last_trigger_date %} {% continue %}
      {% endif %}
      
      {% set icon = state_attr(entity, "icon") %}
      {% if icon == none %} {% set icon = "mdi:robot" %}
      {% endif %}
      {% set ns.select = ns.select + [{
          'type': 'custom:mushroom-template-card',
          'icon': icon,
          'primary': state_attr(entity, "friendly_name"),
          'secondary': time_since(as_datetime(state_attr(entity, "last_triggered"))) ,
          'icon_color': "primary"
      }] %}
    {% endfor %} {{ ns.select }}
#

Thank you!

#

This is exactly what I was hoping for.