Uptime in days hours and minutes

Try:

{% set uptime = states('sensor.uptime')|float %}
{% set days = uptime/60/24 %}
{% set up_dys = days|int %}
{% set hours = (days-up_dys)*24 %}
{% set up_hrs = hours|int %}
{% set up_mns = ((hours-up_hrs)*60)|int %}
{{ up_dys }} days, {{ up_hrs }} hours, {{ up_mns }} mins
1 Like

FYI, sensor.uptime updates every 30 seconds, so no need to use sensor.time to get the template sensor to update.

BTW, one other way to do this would be to declare three variants of sensor.uptime, then …

Thanks everyone will give them all a play and see how I go.

Presumably the new sensor will be a time based one so will say 5 minutes ago, rather than 5 minutes up time

FWIW, my suggestions were based on this statement in your original post:

If you want something different you’ll need to specify exactly what you want.

Let us know how it goes.

Yeah I removed it in my second rushed attempt.

Your version using the three uptime sensors could end up like this without some modular arithmetic:

2 days, 48 hours, 2880 minutes.

Unless I’m missing something?

D’oh! LOL! Yep.

There are a couple of other discussions around this on here and we (well @petro mainly if I remember correctly) ended up with the following for uptime.

      ha_uptime:
        friendly_name: HA Uptime
        value_template: >
          {% if states('sensor.uptime') == '0.0' %} 
            Just restarted...
          {% else %}
            {% set up_time = (states('sensor.uptime') | float * 86400) %}

            {% set minutes = (up_time // 60) | int %}
            {% set hours = (minutes // 60) %}
            {% set days = (hours // 24) %}
            {% set weeks = (days // 7) %}

            {% 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(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), 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 }}
          
          {% endif %}
1 Like

I used a different approach:


- platform: template
  sensors:
    custom_ha_last_boot:
      friendly_name: HA ultimo riavvio
      entity_id: sensor.uptime
      device_class: timestamp
      value_template: "{{ (as_timestamp(now()) - (states('sensor.ha_uptime') | float ) *60*60) | timestamp_custom('%Y-%m-%dT%H:%M', true) }}"

sensor.uptime is based on “hours” as unit_of_measurement.
You can change this and update the math formula in the template.

That way UI will show the value in human readable format and in current UI language :wink:

Simone

Io ho provato a copiarlo così come lo hai scritto, ma in convalida mi da errore

I was using Simone’s suggestion (thanks), which worked perfectly up until last week.

Now unfortunately The sensor value resets to zero after reaching the first minute.
The only thing I changed was updating to Home Assistant Core 0.117.1.

I’m guessing this is linked to one of the breaking changes listed for 0.0117.1.
#13 Templates - Auto-updating now()

The entity_id: attribute was recently removed from template entities because the
templates are now able to find all referenced entities automatically. This removal
introduced a new problem where templates using the current time could no longer use
entity_id: sensor.time to ensure periodic updates.

With apologies to those who have spent the last couple of releases adding
workarounds to their templates, we are now introducing an auto-refresh feature to
time-based templates.

It is thus no longer necessary to reference sensor.time , sensor.date or manually
update template entities when now() or utcnow() is present in the template.

The template will automatically be updated when:

  • A referenced entity changes state.
  • At the start of each minute when now() or utcnow() is present in the template.

Please note, if you have a time-based template where you do not want it to update
periodically it will now have to be reworked to not use now() .

Any ideas please, as I don’t really understand the syntax of this value template.

Thanks.

EDIT

Have now managed to get around this by using another template.

    sensors:
      uptime_clearer:
        friendly_name: "Clearer Uptime"
        value_template: >-
          {% set uptime = states.sensor.ha_runtime.state | int %}
          {% set days = (uptime / 86400) | int %}
          {%- if days > 0 -%}
            {{ days }} days, {{ (uptime - (days * 86400)) | int | timestamp_custom('%-H Hours, %-M Minutes', false) }}
          {%- else -%}
            {{ uptime | int | timestamp_custom('%-H Hours, %-M Minutes', false) }}
          {%- endif -%}

Hi!
Please how can I modify this for to show only D, H, M?
Now it show even W (weeks)

Thanks!

ha_uptime:
        friendly_name: HA Uptime
        value_template: >
          {% if states('sensor.uptime') == '0.0' %} 
            Appena riavviato...
          {% else %}
            {% macro phrase(value, name) %}
            {%- set value = value | int %}
            {{- '{}{}{}'.format(value, name, end) if value | int > 0 else '' }}
            {%- endmacro %}
      
            {% set weeks = (states('sensor.uptime') | int / 7) | int %}
            {% set days = (states('sensor.uptime') | int) - (weeks * 7) %}
            {% set hours = (states('sensor.uptime') | float - states('sensor.uptime') | int) * 24 %}
            {% set minutes = (hours - hours | int) * 60 %}
 
            {{ [ phrase(weeks, 'w'),phrase(days, 'd'),phrase(hours, 'h'),phrase(minutes, 'm') ] | select('!=','') | list | join(', ') }}
          {% endif %}     

Remove the line

{% set weeks = (states('sensor.uptime') | int / 7) | int %}

And remove this from the second last line:

phrase(weeks, 'w'),

are you 100% sure?
I don’t remember, but I think I had already tried it, and it hadn’t worked.

Dont work, “not available”

Sorry forgot one place, in the line starting with

{% set days

Remove the following:

- (weeks * 7)

sensor.uptime 's ‘unit_of_measurement’ is decrepated. I was using ‘hours’. In combination with the following sensor:

- platform: template
  sensors:
    ha_uptime:
      friendly_name: "Home Assistant uptime"
      value_template: >-
          {% if states('sensor.uptime') == '0.0' %}
            Just restarted...
          {% else %}
            {% set up_time = (states('sensor.uptime') | float * 3600) %}

            {% set minutes = (up_time // 60) | int %}
            {% set hours = (minutes // 60) %}
            {% set days = (hours // 24) %}
            {% set weeks = (days // 7) %}

            {% 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(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), 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 }}

          {% endif %}

Since 2020.12.0 unit_of_measurement is not used in this sensor and the output of that sensor is now:

{{ states.sensor.uptime.state }}:

2020-12-14T10:13:11.806446+01:00

How do I need to change the sensor.ha_uptime to provide me with a working sensor again?

Something like this ?

EDIT: this doesn’t update in template editor, not sure if it will when it’s a sensor ?
EDIT: yes it does, every minute

EDIT 2: just added the default uptime sensor, the entity in lovelace already shows the time in hours and mins

- platform: template
  sensors:
    ha_uptime:
      friendly_name: "Home Assistant uptime"
      value_template: >-
        {% set up_time =  as_timestamp(now()) - as_timestamp(states('sensor.ha_uptime')) %}

        {% if up_time == 0 %}
          Just restarted...
        {% else %}
          {% set minutes = (up_time // 60) | int %}
          {% set hours = (minutes // 60) %}
          {% set days = (hours // 24) %}
          {% set weeks = (days // 7) %}

          {% 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(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), 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 }}

        {% endif %}
5 Likes

Seems to be working :+1:

If this sensor is just a timestamp I don’t think it is correct to be named uptime anymore… May be last_start or something like that… It’s just inactive date without change until next restart… Nothing to do with uptime. At least the documentation need template example on how to actually use the sensor.

Had to change sensor.ha_uptime to sensor.uptime as that is what my uptime sensor is called. But it works!

Agree with the above, it’s not really uptime anymore unless you build a sensor like mine.