Uptime in days hours and minutes

Hello.

I was hoping to use the uptime sensor but show the result in days hours and minutes.

This just gives unknown but I do not know enough about it to know why (code copied from on here from another use case)

  - platform: uptime
    name: "HA Uptime"
    unit_of_measurement: minutes
      
  - platform: template
    sensors:
      uptime_days:
        value_template: >-
          {% set value = states('sensor.uptime') %}
          {% set last_updated = as_timestamp(strptime(value, "%Y-%m-%d  %H:%M:%S")) %}
          {% set seconds = now().timestamp()-last_updated %}
          {% set hours = seconds / 3600 %}
          {% if seconds / ( 60 * 60 ) > 1 %}
            {{ seconds //  ( 60 * 60 ) }} hours ago
          {% else %}
            {{ seconds // 60 }} minutes ago
          {% endif %}



Is your sensor named sensor.uptime?

yep, well I think so from this:

image

Your problem is here:


set value = states('sensor.uptime')

set last_updated = as_timestamp(strptime(value,...

sensor.uptime is a value in minutes not a Unix datetime object. Thus the timestamp function wonā€™t work.

Thereā€™s definitely an easier way to do this ( no need to convert minutes to seconds then back again) butI donā€™t have time to rewrite it just now. Untested but this should work:

  - platform: template
    sensors:
      uptime_days:
        entity_id: sensor.time
        value_template: >-
          {% set seconds = states('sensor.uptime')|int * 60 %}
          {% set hours = seconds / 3600 %}
          {% if seconds / ( 60 * 60 ) > 1 %}
            {{ seconds //  ( 60 * 60 ) }} hours ago
          {% else %}
            {{ seconds // 60 }} minutes ago
          {% endif %}

Note this will only update every minute and requires you to make sensor.time, see:

EDIT: I do have time to do it:

  - platform: template
    sensors:
      uptime_days:
        entity_id: sensor.uptime
        value_template: >-
          {% set minutes = states('sensor.uptime')|int %}
          {% set hours = minutes / 60 %}
          {% if minutes / 60 > 1 %}
            {{ minutes //  60  }} hours ago
          {% else %}
            {{ minutes }} minutes ago
          {% endif %}

you donā€™t need the sensor.time with this one.

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?