When using the extellent Custom UI by @andrey, if you use show_last_changed on an automation it doesn’t show the last time it was run, but instead tracks its state just like with any other sensor. Fair enough.
Instead, to display the last time the automation was triggered here is the extra_data_template I added to my customize_glob.yaml file to print something like 42 seconds ago.
sorry for the silly question…
does this require the use of a custom-ui card for automations?
Or can I call the data template via a customize_glob without setting a custom-ui card?
automation.*:
custom_ui_state_card: state-card-custom-ui // is this line required?
extra_data_template: >-
if(!attributes.last_triggered)return null;
var t,s=(new Date()-new Date(attributes.last_triggered))/1e3;
return(
(t=Math.floor(s/86400))?t+(t>1?" days":" day"):
(t=Math.floor(s/3600))?t+(t>1?" hours":" hour"):
(t=Math.floor(s/60))?t+(t>1?" minutes":" minute"):
(t=Math.floor(s))!==1?t+" seconds":" second"
)+" ago";
@cameron@Valiceemo Indeed, I forgot to add it to my shared snippet, but to leverage Custom UI fully I also have the following entry in my customize_glob configuration so that all state cards have the Custom UI one by default:
customize_glob:
"*.*":
custom_ui_state_card: state-card-custom-ui
[...other specific entries below...]
Maybe using a custom sensor optionally with a scan_interval parameter or an automation with a service call to persistent_notification.create would do, but you’ll probably have to list all the automations that you want to poll, compare their last_triggered attributes and pick the most recent one.
If you’re willing to code a bit in Python, you could base yourself on the Min/max sensor sensor and modify it to compare specific attributes instead of just states. That would make your comparison logic reusable and your YAML files less complex.
@renemarc Thank you for the inspiration by sharing this!
A while ago, I asked Andrey how to easily recreate show_last_changed within extra_data_template. Thanks to his feedback then, I was able to use the following code in customize_glob:
This thread inspired me to check out Custom UI. I implemented renemarc’s latest code, and it works great for the group view, with each automation showing it’s latest trigger. But when I click on an automation, every automation shows the time of the last automation triggered.
Mine isn’t right either but, instead of showing the time of the last triggered automation like yours, it shows 6 hrs ago for all despite several being triggered after that.
@Coolie1101 Splitting your configuration into separate YAML files like you did is a great step towards keeping things clean and modular. However, in this case you need to specify to what pattern this rule will apply, hence the following in my customize_glob.yaml file:
The first rule means apply state-card-custom-ui to every state cards.
The second one means add the extra_data_template to all automations.
Do mind the space indentations, they matter a lot in YAML files! Use the Configuration Validation tool inside the Configuration panel to check your config files before restarting HASS to avoid any invalid configurations.