petro
(Petro)
December 17, 2020, 2:54pm
8
relative time already does that. all you’d have to add is ‘ago’ after the template:
{{ relative_time(states.sensor.uptime.last_changed) }} ago
But you’d REALLY need to implement the availability_template to ensure it’s ‘unavailable’ when the calculation fails (So that it doesn’t read unavailable ago
)
yeah, I only posted the snippet for the longer template you posted, not as a full replacement.
About the
what do you mean by that? relative_time()
only mentions the biggest of the available units ?
relative_time
converts datetime object to its human-friendly “age” string. The age can be in second, minute, hour, day, month or year (but only the biggest unit is considered, e.g., if it’s 2 days and 3 hours, “2 days” will be returned).
so if you need the phrase, relative_time() isnt of much help
petro
(Petro)
December 17, 2020, 2:59pm
10
If you’re referring to a phrase as 10 hours, 3 minutes
, then yes, relative_time does not do that. It would only return 10 hours
.
yeah, thought that was was the OP liked to see
my phrase template (which was heavily copied from a couple of fellows here, not taking any credit) also drops in weeks and seconds
petro
(Petro)
December 17, 2020, 3:03pm
12
pretty sure I built it for you a few years back
haha, probably!
you had a big hand in this too, but it mysteriously stopped calculating passed and remaining days correctly…
petro
(Petro)
December 17, 2020, 4:31pm
14
FYI, here’s a streamlined version
{%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
{%- macro phrase(name, divisor, mod=None) %}
{%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
{%- set end = 's' if value > 1 else '' %}
{{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
{%- endmacro %}
{%- set values = [
phrase('week', 60*60*24*7),
phrase('day', 60*60*24, 7),
phrase('hour', 60*60, 24),
phrase('min', 60),
phrase('sec', 1, 60)
] | select('!=','') | list %}
{{ values[:-1] | join(', ') ~ ' and ' ~ values[-1] if values | length > 1 else values | first }}
For other languages or variations to this template, see here:
@KameDomotics ,
@AcidSleeper
12 d, 5 h, 35 m format.
{%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.hassio_drifttid_dagar')) %}
{%- macro phrase(name, divisor, mod=None) %}
{%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
{{- '{} {}'.format(value, name) if value | int > 0 else '' }}
{%- endmacro %}
{%- set values = [
phrase('w', 60*60*24*7),
…
7 Likes
petro:
{%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
{%- macro phrase(name, divisor, mod=None) %}
{%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
{%- set end = 's' if value > 1 else '' %}
{{- '{} {}{}'.format(value, name, end) if value | int > 0 else '' }}
{%- endmacro %}
{%- set values = [
phrase('week', 60*60*24*7),
phrase('day', 60*60*24),
phrase('hour', 60*60),
phrase('min', 60),
phrase('sec', 1, 60)
] | select('!=','') | list %}
{{ values[:-1] | join(', ') ~ ' and ' ~ values[-1] if values | length > 1 else values | first }}
nice!!
and it works equally well with the last_boot sensor.
magic.
petro
(Petro)
December 17, 2020, 4:58pm
16
It’ll work with any time difference in seconds.
ptdalen
December 17, 2020, 5:21pm
17
I have not updated yet, but use uptime in many of my automations as a condition to keep them from running during restart. To be honest I added these conditions years ago, and it’s just worked so I have not looked for better ways to do it. I dont care much about displaying the uptime, just want to keep automations from triggering during the first X minutes after a restart.
What is the recommended way to do that these days? Most of my automations with an uptime trigger are set to 1-2 minutes, and a few are set longer for devices that take a little longer to report status sometimes.
Bartem
December 17, 2020, 6:18pm
18
So I have a “HA not restarted recently sensor” which was based off minutes of uptime… I just want to show the minutes similar to this:
petro:
homeassistant_uptime:
friendly_name: HomeAssistant Uptime
value_template: >
{# use now() to update every minute #}
{% set t = now() %}
{{ relative_time(strptime(states('sensor.uptime'), '%Y-%m-%dT%H:%M:%S.%f%z')) }}
availability_template: >
{{ strptime(states('sensor.uptime'), '%Y-%m-%dT%H:%M:%S.%f%z') is not string }}
but without the word minutes… removing “relative_time” just breaks the whole thing and I’m lost with all these time formatting things.
This sensor is used as a condition to keep automations from triggering after a restart…
- platform: template
sensors:
home_assistant_not_restarted_recently:
value_template: "{{ states('sensor.ha_process_uptime_sensor')| float > 6 }}"
And with the old uptime sensor the state was just a numeric value… I’m not sure if it’d be easier to redo the whole restarted recently sensor, or just make another sensor that provides just the numeric number of minutes like it was before.
petro
(Petro)
December 17, 2020, 6:21pm
19
{{ ( as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) // 60 }}
EDIT: Had math reversed.
1 Like
My automations used to be:
- condition: numeric_state
entity_id: sensor.uptime
above: .1
and is now:
- condition: template
value_template: "{{ (((as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) % 3600) / 60) | int > 5 }}"
This new condition is set for 5 minutes.
1 Like
petro
(Petro)
December 17, 2020, 6:22pm
21
That condition is wrong. That will be on every hour on the hour (from startup) for 5 minutes.
maurizio53
(Maurizio Fabiani)
December 17, 2020, 6:28pm
22
Another stupid question:
“unit_of_measurement” is deprecated for all sensors or only for the uptime sensor?
And in case of all sensors, how to change it?
tom_l
December 17, 2020, 7:09pm
24
Where doesn’t it display properly?
I’m only using it in an entities card and and it works fine.
naitsimp
(Christian)
December 17, 2020, 7:27pm
25
I use it this way:
- platform: uptime
name: uptime_of_hassio
- platform: template
sensors:
template_uptime_of_hassio:
value_template: >
{% set time = (as_timestamp(states.sensor.date_time_iso.state) - as_timestamp(states.sensor.uptime_of_hassio.state))/60 | float %}
{% set days = (time / (24*60)) | int %}
{% set hours = ((time / 60) - ((time / (24*60)) | int ) * 24) | int %}
{% set minutes = ((((time / 60) - (time / 60) | int) * 60) | round(1)) | int %}
{% set seconds = ((time - (time | int)) * 60) | int %}
{{days}}d {% if hours < 10 %}0{{ hours }}{% else %}{{ hours }}{% endif %}:{% if minutes < 10 %}0{{ minutes }}{% else %}{{ minutes }}{% endif %}:{% if seconds < 10 %}0{{ seconds }}{% else %}{{ seconds }}{% endif %}
In lovelace it displays like this: 0d 03:04:08
petro
(Petro)
December 17, 2020, 7:45pm
26
pretty much everywhere but entities
tom_l
December 17, 2020, 7:53pm
27
?
Looks fine to me:
Edit: Oh, everywhere but… sorry.