#List time since last activation for select automations
1 messages ยท Page 1 of 1 (latest)
you could probably do this with a markdown card
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?
probably doable with auto entities card too
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
Man. I started writing a response earlier but wasn't able to finish it. Y'all already got it taken care of. ๐
Thank you! Is there any way to make it only do so for a given list of automations rather than all automations?
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.
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 %}
@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')) }}
welp
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
try using the values of @sage patio with this as 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 %}
Weird question: what time zone are you in? I'm GMT-4. Are you in GMT?
CEST/UTC+2
should not impact tho
timedate variables take in consideration time zones
I'm very unclear on what you're saying to do. Sorry.
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
I still just get a blank box. https://i.imgur.com/MvOp6Nf.png
try this in the dev tab > 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 %}
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 %}
Both of these result in a ValueError result.
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).
I cant understand why its not working for you guys
The code looks fine and return expected values on my side
Whats the error ?
Could you screenshot it ?
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 %}
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'
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
That seemed to work correctly. I mean, it's the time and date it was triggered, not time since then, but still. https://i.imgur.com/5FIQUaB.png
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
That also works. https://i.imgur.com/zXOpXsj.png
Yes, I do have some automations that have never activated
Thats it then
How do I add to the exclusion tray?
I could not get why it did not work with you guys
Or better yet, can I just use explicit inclusion?
Thank you!
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)
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.
Ill make the modification later if you want
That would be much appreciated. Thank you. โค๏ธ
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...
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
Thank you! I'll give this a try after I get home from work.
it probably need some optimisations tho i'm a novice at this
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.
yeah the now() is inaccurate, it does not use local timezone
saw that just after sending it
Oh, that's super weird
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 ?
since
๐
If it matters, my timezone is central europe
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
๐
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.