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.
As for script state cards, alas I cannot get extra_data_template for work with them, for time display or for anything else either.
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?
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";
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.
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':''));"
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!
ok. thanks for the explanation.
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!
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?