Custom UI: Last time automation was triggered

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

Thanks. To clarify, Custom UI doesn’t work in Lovelace or only this particular snippet of code?

The problem is that only some of the functionality in custom_ui still works in lovelace but there is no documentation anywhere that tells you what does or does not work. So everything you want from custom_ui is trial and error.

I’ve found that everything I really need in custom_ui is already provided by other lovelace cards/entity custom components so I just completely removed custom_ui.

Thanks for clarifying.

Did anybody find out, if/how this secondary info is possible in native lovelace UI Entities Cards oder Custom Cards now?