Custom UI: Last time automation was triggered

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.

automation.*:
  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";

It’s minified JavaScript to reduce the space it takes in the entities listing, and can be localized easily.

custom-ui-automation-last-triggered

Sure beats having to hunt down the value in the dev tools! Enjoy! :grinning:

14 Likes

This looks great. I have been using Custom_Ui for a while now and can’t seem to get your code to work. Can you give a idea of how to implement it.

Thanks

  customize_glob:
    "*.*":
      custom_ui_state_card: state-card-custom-ui
    automation.*:
      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";
1 Like

it works great!

that’s mine and it works (all 2 spaces indentation in configuration.yaml)

  customize_glob:
    "*.*":
      custom_ui_state_card: state-card-custom-ui
    automation.*:
      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";

Love your code, really useful.

YOu think is possible to show in front end the LAST automation triggered?

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";

that line i believe is required at least for me it was…

Indeed it was…got it running.
Only just discovering Custom UI…love it!
And how much easier is it to use a customize_glob

1 Like

@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...]
1 Like

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:

automation.*:
  custom_ui_state_card: state-card-custom-ui
  extra_data_template: >
    if (attributes.last_triggered) return window.hassUtil.relativeTime(new Date(attributes.last_triggered)); else return null;

The result looks like this:

37

4 Likes

Oooh, nice! :smiley: Much shorter indeed, even if text is not localized. Thanks @fanaticDavid!

I just replaced my verbose implementation with yours. It makes for a leaner configuration, I like!

For added brevity, I used a ternary operator so that your line shortens to:

extra_data_template: >-
  return attributes.last_triggered ? window.hassUtil.relativeTime(new Date(attributes.last_triggered)) : null;

6 Likes

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.

58%20AM
37%20AM

Any ideas?

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.

Just looked at mine again, and it is showing 9 hrs, which is hen HA was last restarted (and there are a couple HA start event automations).

Hey, tried implementing this but, get a blank card on the frontend, any ideas?

What I did was:
Added “customize_glob: !include customize_glob.yaml” to my configuration.yaml.
In “customize_glob.yaml” I put the following:

And on the Frontend I get the followng:

@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:

"*.*":
  custom_ui_state_card: state-card-custom-ui
automation.*:
  extra_data_template: >-
    return attributes.last_triggered ? window.hassUtil.relativeTime(new Date(attributes.last_triggered)) : null;

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. :white_check_mark:

For more details you can check out my working configuration on GitHub.

Sorry, what I actually have in the yaml is:

automation.*:
  custom_ui_state_card: state-card-custom-ui
  extra_data_template: >-
  return attributes.last_triggered ? window.hassUtil.relativeTime(new Date(attributes.last_triggered)) : null;

I tried with what you recommend and what you posted initially, but it doesn’t apply on the frontend but, show up as attributes for each Automation

You still haven’t indented the same as @renemarc, look where his line beginning “return” is compared to yours.