Custom UI: Last time automation was triggered

relativeTime is not available now. I am using the following in my customize_glob.yaml to obtain the same information:

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

Is this available only for automation or for other classes ? I ask this because using this template for device trackers i get errors in log…

Any idea why the logged time is off by +4hrs?

My current time is 2:47pm and the logged time is:

last_triggered: 2018-06-05T18:47:26.942125+00:100

I think the internal time is in UTC.

It should work for other classes as well, but I wasn’t able to get it working for device_tracker.

Any idea how I can rectify that?

Current default time zone: 'America/New_York'
Local time is now:      Tue Jun  5 15:05:34 EDT 2018.
Universal Time is now:  Tue Jun  5 19:05:34 UTC 2018.

@renemarc
Would it be possible to add a convert/format option to the code?

You can always use timestamp_custom. For example,

{{ as_timestamp(states.automation.abode_home_at_night.attributes.last_triggered) | timestamp_custom('%I:%M:%S %p', true) }} returns the last_triggered in local time.

How should I write to include this: entities['sensor.power'].state) in:

extra_data_template:
"if(!attributes.last_triggered)return null;
 var d,h,m,s=(new Date()-new Date(attributes.last_triggered))/1e3;
 d=Math.floor(s/86400);
 s=s-(d*86400);
 h=Math.floor(s/3600);
 s=s-(h*3600);
 m=Math.floor(s/60);
 s=Math.floor(s-(m*60));
 return((d>0?d+'d ':'')+(h>0?h+'h ':'')+(m>0?m+'m ':'')+(s>0?s +'s':''));"
1 Like

I assume you want to have multiple extra templates, at least one of which is that multiline bit of code? Then this array syntax should work:

automation.example:
  extra_data_template:
    - ${entities['sensor.power'].state}
    - >-
      if(!attributes.last_triggered)return null;
      var d,h,m,s=(new Date()-new Date(attributes.last_triggered))/1e3;
      d=Math.floor(s/86400);
      s=s-(d*86400);
      h=Math.floor(s/3600);
      s=s-(h*3600);
      m=Math.floor(s/60);
      s=Math.floor(s-(m*60));
      return((d>0?d+'d ':'')+(h>0?h+'h ':'')+(m>0?m+'m ':'')+(s>0?s +'s':''));

The code above depends on the presence of a timestamp as an attribute to an entity, in this case last_triggered for automations. Some components do have a date string available (file sensor, waqi, sun…) but most do not, unfortunately.

Do have a look at the Attributes column in your http://homeassistant:8123/dev-state to see what’s possible! :grinning:

ok. thanks for the explanation.:+1:

I have automations that perfectly indicate the last execution time using the show_last_change switch in custom-UI. Are you sure this is still required?

I’m using the latest Custom UI release to date (20180625) and if I switch to show_last_change I still get the last state change time for any given automation (i.e. enabled or disabled), but not the last time the automation was actually triggered. So yes, still needed! :grinning:

I tried to make a extra_data_template for binary_sensors where
{{ states.binary_sensor.door.last_updated }}
gives
2018-08-06 16:15:13.564100+00:00

but this is not working.
“binary_sensor.*”:
extra_data_template: >-
if(!last_updated)return null;
var t,s=(new Date()-new Date(attributes.last_updated))/1e3;
return(
(t=Math.floor(s/86400))?t+(t>1?" dagar":" dag"):
(t=Math.floor(s/3600))?t+(t>1?" timmar":" timme"):
(t=Math.floor(s/60))?t+(t>1?" minuter":" minut"):
(t=Math.floor(s))!==1?t+" sekunder":" sekund"
)+" sedan";

Anyone got an idea why?

Need some help with this.

Can someone give a step by step instruction on where to put the code and what files to add and where, and how to get in to show on a card with automations.

Im on Hassio 0.86.2

step 1 - go to the custom-ui github page and follow the instructions there for install it:

step 2 - in configuration.yaml add this:

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

there was a post above that said that relativeTime doesn’t work anymore. So if the above doesn’t work then try this:

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

the “customize_glob:” is under the “homeassistant:” section in the configuration.yaml

I think that should be all you need. If it doesn’t work then come back and hopefully someone else can chime in with more help.

no more help needed, can confirm this is the correct setup. relativeTime doesn’t work, second template works perfectly:

32

restore state is preserved also:

46

This is a great feature that I want to implement but I’m struggling to get it to work in my UI. And I can’t work out why.

The secondary line (showing how long ago the automation was triggered) isn’t appearing in my UI. However if I click for more-info on an entity, I see the last triggered info in the more-info entity-state card.

Here are the relevant configs.

Configuration.yaml

 homeassistant:
      customize: !include customize.yaml
      customize_glob: !include customize_glob.yaml
    frontend:
      themes: !include_dir_merge_named themes/
      extra_html_url:
        - /local/custom_ui/state-card-custom-ui.html
      extra_html_url_es5:
        - /local/custom_ui/state-card-custom-ui-es5.html
    customizer:
      custom_ui: local

customize_glob.yaml

"*.*":
    custom_ui_state_card: state-card-custom-ui
automation.*:
  custom_ui_state_card: state-card-custom-ui
  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";
$ls /config/www/custom_ui   
scripts-es5.js.LICENSE  scripts.js.LICENSE  state-card-custom-ui-es5.html     state-card-custom-ui.html
scripts-es5.js.map      scripts.js.map      state-card-custom-ui-es5.html.gz  state-card-custom-ui.html.gz

In lovelace, doesn’t work anymore.
In states UI, still working.

But since 0.93 (I guess) you have something like that natively. Go in Configuration, Automations. All your automations are there (even if not in the configuration.yaml file, thus not editable in UI) with the date/time last triggered as the secondary information !