swipee
August 27, 2021, 11:06am
44
Yes, it works. I had a look in my config file and I’ve apparently added another type of entity as well:
sensor:
#HASS uptime
- platform: uptime
name: Uptime
- platform: template
sensors:
uptimev2:
friendly_name: Uptimev2
value_template: >
{{ (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) // 60|int }}
- platform: template
sensors:
home_assistant_uptime:
friendly_name: Home Assistant Uptime
value_template: >-
{% set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
{% set minutes = (up_time // 60)|int %}
{% set hours = minutes // 60 %}
{% set days = hours // 24 %}
{% set minutes = minutes % 60 %}
{% set hours = hours % 24 %}
{% set days = days % 7 %}
{% macro phrase(value,name) %}
{%- set value = value %}
{%- set end = 's' if value > 1 else '' %}
{{- '{} {}{}'.format(value,name,end) if value|int > 0 else '' }}
{%- endmacro %}
{% set text = [phrase(days,'day'),phrase(hours,'hour'),
phrase(minutes,'min')]
|select('!=','')|list|join(', ') %}
{% set last_comma = text.rfind(',') %}
{% if last_comma != -1 %}
{% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
{% endif %}
{{text}}
Seems there are others with similiar errors and recommended checking the template editor for debugging but maybe that is what you’ve already tried?:
Paste that line into Home Assistant’s Template Editor and debug it there.
1 Like
Friedi
August 27, 2021, 12:55pm
45
Yep. Other errors …
It was just a typo. What about AI?
Hi, I have a question:
Is this still the correct way to get the uptime sensor?
sensor:
- platform: uptime
name: Time Online
Or should it follow the new template format?
template:
- sensor:
- name: "Server uptime"
state: ???
Thank you.
Yes, but isn’t it a template sensor? I feel that code follows the legacy sensor configuration format
no, where did you get the idea this is a template sensor? some of the others above are, but this is a core integration
1 Like
AllHailJ
(J Gent)
January 3, 2022, 1:50pm
51
Here is how I would do it:
{{ states('sensor.ha_uptime') }}
{% set total_seconds = as_timestamp(utcnow())-as_timestamp(states('sensor.ha_uptime')) %}
{{ total_seconds }}
{% set days = (total_seconds / 86400) | int %}
{{ days }}
{% set hours = ((total_seconds % 86400) / 3600 ) | int %}
{{ hours }}
{% set minutes = ((total_seconds % 3600) / 60 ) | int %}
{{ minutes }}
{% set seconds = (total_seconds % 60) | int %}
{{ seconds }}
{{ [days, hours, minutes, seconds]|join(':') }}
You can cut & paste this in to developer templates and you should get something like this:
if you want a nicer state than the uptime sensor, why not use the template sensor posted above somewhere:
template:
- sensor:
- unique_id: ha_uptime_phrase
name: Ha uptime phrase
icon: mdi:history
state: >-
{%- set up = now().timestamp()-as_timestamp(states('sensor.uptime')) %}
{%- macro phrase(name,divisor,mod=None) %}
{%- set value = ((up//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}}
petro
(Petro)
January 3, 2022, 2:08pm
53
That output is basically identical to a timedelta as a string.
{{ utcnow() - states('sensor.ha_uptime') | as_datetime | default(utcnow()) }}
AllHailJ
(J Gent)
January 3, 2022, 2:26pm
54
This would not give the OP the days as requested.
Am I missing something?
petro
(Petro)
January 3, 2022, 2:45pm
55
You overwrote total_seconds with a time from 2 days ago, so your as_timestamp calc has a difference of 2 days. OP is asking for days hours and minutes, not seconds. So in that regard, they are both wrong. Either way, this is the real difference (without incorrectly overwriting the difference)
AllHailJ
(J Gent)
January 3, 2022, 2:51pm
56
DUH! Missed changing yours. Sorry for the stupidity.
drinfernoo
(drinfernoo)
February 6, 2022, 4:00pm
57
This might be an old post, but I solved this via:
sensor:
- platform: uptime
template:
- sensor:
- name: Uptime Relative
state: >-
Up {{ states("sensor.uptime") | as_datetime | relative_time }}
2 Likes
AllHailJ
(J Gent)
February 6, 2022, 4:37pm
58
But this does not give you the HH:MM:SS format that the OP wanted.
drinfernoo
(drinfernoo)
February 6, 2022, 4:40pm
59
Whoops! I guess I solved a different problem… I didn’t read closely enough
virgilm
February 10, 2022, 4:47pm
60
In case anyone else is struggling with this, the documentation is confusing to say the least.
sensor:
- platform: uptime
name: "HA Runtime"
makes your builtin sensor be called now sensor.ha_runtime, because… why not? it’s quite obvious right?, even if the docs say: “name: Name to use in the frontend.” (should be “while adding an underscore for good measure and changing the entity name for you”)
1 Like
arganto
February 10, 2022, 4:53pm
61
What did you expect instead? This normalization is common everywhere, isn’t it?
virgilm
February 10, 2022, 5:16pm
63
I have to agree that is obvious to someone who lives and breaths HA, I get that. It was not obvious for me.
Maybe you care to explain why this is too:
sensor:
- platform: onewire
names:
28-8367301e64ff: temp_one_wire
But the sensor is called: sensor.28_8367301e64ff_temperature → note the underscore and the suffix.
I understand that under the hood any character you don’t like gets translated to underscore, but I fail to understand how is that obvious. Normal engineering decision I would say, but it should be documented, that’s all I’m saying.