Custom UI: Last time automation was triggered

No errors in log or developer tools/console

This is a great idea ! :clap:

Here is my vision, I changed it to display the detail of last triggered. Default display is ‘x day’ if it’s more than 24hours, and I wanted to have more details. This displays days, hours, minutes and seconds (rounded) from last triggered, only if different from 0. (ex : 13h 24m 15s, or 1d 24m 30s) :

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

Tried to do the same with script.* but I can’t make it work ?

2 Likes

At this point I’d suggest trying on a virgin Docker installation of Home Assistant to minimize all other possible problems and interferences with your configuration.

Very nice @Mister_Slowhand! Indeed, sometimes it can be useful to get a more precise idea of when exactly something happened. :grinning:

As for script state cards, alas I cannot get extra_data_template for work with them, for time display or for anything else either. :cry:

Do you think it’s a bug ?

Hi, very useful!
Is it possible to use this formula in sensors or other domains like light, binary sensors …?
I’m not a programmer, how can I change this code to replace the one of “deault value”.
Thank you very much.

Since HASS 0.70.1 I got the error:
ERROR (MainThread) [frontend.js.latest.201805310] https://xxx.xxx.xx/local/custom_ui/state-card-custom-ui.html:27:14429 Uncaught TypeError: Cannot read property ‘relativeTime’ of undefined

Anyone got a solution?

1 Like

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?