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
If you create an automation using a text editor, and provide it with an id, it will be registered by Home Assistant and traces will be created for it whenever it triggers. If you don’t provide an id, traces will not be created for it and it won’t be registered.
If you then delete the automation’s code from the file and either restart Home Assistant or execute Reload Automations, the automation is effectively gone but it’s not deregistered. It will still appear in the list of automations as a gray “ghost” entry. You have to remove the “ghost” via the UI (click its information icon, etc).
In case anyone else was looking, I did manage to figure out how to get it to show correctly.
- type: markdown
title: Zones Triggered
content: |
|D|H|M||Time||Name|
|---:|----:|----:|:--:|:---:|:--:|:----|
{% for state in expand('group.ad2pi_sensors')
| sort(attribute='last_updated', reverse=true) -%}
{%- set t1 = state.last_updated.timestamp() | timestamp_custom('%H:%M') -%}
{%- set t = now() - state.last_updated -%}
{%- 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 %}
Changing all the ‘last_changed’ to ‘last_updated’ did the trick.
Still trying to figure out how to do the same thing in this markdown card style, but for listing out all the changes to a state for a single state. Things that I could apply this to would be the opensky flight callsign (see this thread) or showing a listing of the latest 3d printed filenames fed from Octoprint
I’m using this code that is still working on 2022.6.0 2022.6.2:
# Automations of the last 8 hours, but do not display more than 40
**Zeit Name**
{% for state in (states.automation
| rejectattr('attributes.last_triggered', 'in', ['None', 'none', 'unknown'])
| selectattr('attributes.last_triggered')
| selectattr('state', 'ne', 'unavailable')
| sort(attribute='attributes.last_triggered', reverse=true)) [0:40] -%}
{% if state.attributes.last_triggered and (now() - state.attributes.last_triggered).total_seconds() < 28800 %}
{%- set t = (as_timestamp(state.attributes.last_triggered) | timestamp_custom('%H:%M', true)) -%}
{{t}} _{{state.name}}_
{% endif -%}
{% endfor %}
Thanks! I tried your code and got no output also. This is my formerly working code.
|H|M||Name|
|----:|----:|:--:|:----|
{% for state in states.automation
|selectattr('attributes.last_triggered')
|sort(attribute='attributes.last_triggered', reverse=true)
if (now() - state.attributes.last_triggered).total_seconds() < 43200 %}
{%- 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 ' ' -%}
|{{hrs}}|{{(t.seconds//60)%60}}||_{{state.name}}_|
{% endfor %}