is there an easy edit to make this show the last_changed on sensors?
I quickly changed it into:
|D|H|M||Time||Name|
|---:|----:|----:|:--:|:---:|:--:|:----|
{% for state in states.sensor
|sort(attribute='last_changed', reverse=true) -%}
{%- set t1 = state.last_changed.timestamp() | timestamp_custom('%H:%M') -%}
{%- set t = now() - state.last_changed -%}
{%- set days = t.days if t.days > 0 else ' ' -%}
{%- set hrs = t.seconds//3600 %}
{%- set hrs = hrs if hrs > 0 else ' ' -%}
|{{days}}|{{hrs}}|{{(t.seconds//60)%60}}||{{t1}}||_{{state.name}}_|
{% endfor %}
did it for sensors…
(was confused on the attributes ‘last_changed’ bit it does work after all)
now the next challenge: how to show the most active sensors (as in changing most frequently), ill post a new topic
I was hoping to adapt this for my home alarm system to see where detection/motion was last triggered.
By changing the above to “states.binary_sensor” this would include all binary sensors, but how would I go about limiting it to only the ones associated with my alarm system (door contacts and motion)?
Create a group containing all of your alarm system sensors (like group.alarm_system). Instead of using states.binary_sensor in the template, use expand('group.alarm_system') and now it will only consider its entities and not all binary_sensors.
no such thing as coincidence as a matter of fact I had been experimenting with the exact same thing in mind, and tried:
{% for state in expand('group.doors')| selectattr('last_changed') | sort(attribute='last_changed', reverse=true) -%}
{{ state.last_changed.timestamp() | timestamp_local }} : {{ state.name }}
{% endfor %}
only to find out, this doesnt exactly do what I had hoped for. It lists all members of the group, and shows their last changed time. What it doesnt do is show a historic list of the last opened doors.
this is what happens, and Ive been opening and closing the front door and hallway door more than a few times:
If you mean like the Logbook then a template can’t do that because it only has access to last_changed and nothing prior to that. Logbook uses the database to show the history of a door’s openings/closings.
LoL and I am going to that what I was hoping to achieve is what Marius already had in mind … which was a “log”. So if I open the front door, then the back door, then the front door again – this doesn’t show the order of things, just the “last changed” so each sensor would never show up more than once.
Anyhow, I’m still very happy with a list of the last changed states for the sensors, and just wanted to say that this bit of markup was written so well to be easily adaptable for other than just the original intent, which was for “automations” triggered.
If there is a simple way of making this more like a log instead, I’d be all over it too. If not, still more than happy as it is. Thanks again!
For future reference, templates have access to an entity’s state and attributes within Home Assistant’s State Machine (predominantly, the current state of all entities). Templates don’t have access to the database where the history of an entity’s previous states is stored.
FWIW, the SQL Sensor integration allows you to query the database (any database) and display the result. However, I don’t have any experience with it and I am not sure it can be used to display multiple results in the manner you want.
Thats really neat. However, a real newbie question… please forgive me
I add a markdown card, select “show code editor” and am presented with:
"type: markdown
content: >-
The Markdown card allows you to write any text. You can style it bold, italicized, ~strikethrough~ etc. You can do images, links, and more.
I delete the three lines unto the line that starts " The Markdown" and then paste in the code starting with |D|H|M| as per the example posted.
It doesn’t work - I’ve obviously missed something, and don’t really know how to use the markdown card you have posted. Please point me in the right direction.
Has something recently broken the formatting of the markdown or was there something wrong I did in the first place?
The 2 pieces of code below has been working fine for several months. First set of code works perfectly fine, but the second set of code was modified based on expand(‘group.xxx’) .
The second piece also uses last_changed instead of last_triggered from the original code.
- type: markdown
title: Automations Triggered
content: |
|D|H|M||Time||Name|
|---:|----:|----:|:--:|:---:|:--:|:----|
{% for state in states.automation
|selectattr('attributes.last_triggered')
|sort(attribute='attributes.last_triggered', reverse=true) -%}
{%- set t1 = state.attributes.last_triggered.timestamp() | timestamp_custom('%H:%M') -%}
{%- set t = now() - state.attributes.last_triggered -%}
{%- set days = t.days if t.days > 0 else ' ' -%}
{%- set hrs = t.seconds//3600 %}
{%- set hrs = hrs if hrs > 0 else ' ' -%}
|{{days}}|{{hrs}}|{{(t.seconds//60)%60}}||{{t1}}||_{{state.name}}_|
{% endfor %}
- type: markdown
title: Zones Triggered
content: |
|D|H|M||Time||Name|
|---:|----:|----:|:--:|:---:|:--:|:----|
{% for state in expand('group.ad2pi_sensors')
|sort(attribute='last_changed', reverse=true) -%}
{%- set t1 = state.last_changed.timestamp() | timestamp_custom('%H:%M') -%}
{%- set t = now() - state.last_changed -%}
{%- set days = t.days if t.days > 0 else ' ' -%}
{%- set hrs = t.seconds//3600 %}
{%- set hrs = hrs if hrs > 0 else ' ' -%}
|{{days}}|{{hrs}}|{{(t.seconds//60)%60}}||{{t1}}||_{{state.name}}_|
{% endfor %}
I’ve been having a lot of warnings in the logs with the list of recently triggered automation because all automation do not have a last_triggered attribute…
One only needs to add selectattr(‘attributes.last_triggered’, ‘defined’) to filter them out.
{% for state in (states.automation | selectattr('attributes.last_triggered', 'defined') | selectattr('attributes.last_triggered') | sort(attribute='attributes.last_triggered', reverse=true)) if (now() - state.attributes.last_triggered).days < 1 -%}
{{ state.attributes.last_triggered.timestamp() | timestamp_custom('%d/%m %H:%M:%S') }} : {{ state.name }}
{% endfor %}
Oh really ? That’s weird, there must be other cases… Is the attribute removed or reset at reboot ? Any link with history database ?
I guess I should dig deeper to find which automations generate those warnings