Uptime in days hours and minutes

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.

First, this is not my code, I got if from @Jasper8472.

Looks ok, you did reload the template entities ?

Have you got a sensor called sensor.uptime ?

Maybe try and copy the template into the template editor and see if it works ?

remove platform: uptime from your sensor.

You’ve inserted it twice.

Hi!
Same problem after last update.

- platform: uptime
  name: Uptime
  scan_interval: 60

- platform: template
  sensors:
      uptime_ha:
        friendly_name: 'Uptime Home Assistant'
        icon_template: mdi:home-assistant
        value_template: >-
          {% set up_time =  as_timestamp(now()) - as_timestamp(states('sensor.uptime')) %}

          {% if up_time == 0 %}
            Только запущен...
          {% 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(недель, 'неделю'), phrase(дней, 'день'), phrase(часов, 'ч.'), phrase(минут, 'мин') ] | select('!=','') | list | join(', ') %}
            {% set last_comma = text.rfind(',') %}
            {% if last_comma != -1 %}
              {% set text = text[:last_comma] + ' и' + text[last_comma + 1:] %}
            {% endif %}

          {{ text }}

          {% endif %}

In template it is work


in condition no

before that there was another template, it also stopped working, started looking for others, but all to no avail.

Condition

Don’t think you need scan_interval as it only updates on start

Also, you have translated the variables in the set text line:

{% set text = [ phrase(недель, 'неделю'), phrase(дней, 'день'), phrase(часов, 'ч.'), phrase(минут, 'мин') ] | select('!=','') | list | join(', ') %}

Should be:

{% set text = [ phrase(weeks, 'неделю'), phrase(days, 'день'), phrase(hours, 'ч.'), phrase(minutes, 'мин') ] | select('!=','') | list | join(', ') %}
2 Likes

Super, it`s work! Sencs.

Since Home Assistant 2020.12.0, to know the minutes since HA started (to avoid automations run at startup) a fix for uptime:

- platform: uptime
  name: 'HA uptime moment'

- platform: template
  sensors:
    ha_uptime:
      friendly_name: 'HA uptime minutes'
      value_template: >
        {{ (as_timestamp(now()) - as_timestamp(states('sensor.ha_uptime_moment'))) / 60 }}

Then in automations, use

  condition:
    - condition: numeric_state
      entity_id: sensor.ha_uptime
      above: 10
6 Likes

For minutes I think that should be

      value_template: >
        {{ (as_timestamp(now()) - as_timestamp(states('sensor.ha_uptime_moment'))) / 60 }}
2 Likes

Yes. 3600 is for hours

finally how can I get the data templated? can someone paste the sensor code?
thanks