Uptime in days hours and minutes

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

Can you psot what your sensor configuration looks like. I am a complete puttz when it comes to coding but I am trying to understand. I appreciate your assistance very much.
Thankyou

Below is the contents of my uptime_sensor.yaml. It’s included in the configuration.yaml.

- platform: uptime
  name: Uptime

- platform: template
  sensors:
    ha_uptime:
      friendly_name: "Home Assistant uptime"
      value_template: >-
        {% set up_time =  as_timestamp(now()) - as_timestamp(states('sensor.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 %}
4 Likes

if you’re interested here is a streamlined version of that. Makes it easy to add or remove any unwanted values:

2 Likes

Great work to get the uptime working again but as for the Automations this code is difficult to work with, at least for me. I need numeric states or at least integers but this is a string attribute.

This is what I’ve concluded works for me wanting to measure the uptime in minutes and evaluate a numeric state on the same:

  #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 }}

And then I evaluate the uptime based on the uptimev2 numeric state, for example between 2 and 3 minutes:

I need to ask …
This code works for you? In my case the sensor is not available du to an error of the calculation.
When I put this {{ (as_timestamp(now()) - as_timestamp(states(‘sensor.uptime’))) // 60|int }} to the dev tools I get the following error:
TypeError: unsupported operand type(s) for -: ‘float’ and ‘NoneType’

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?:

1 Like

Yep. Other errors …
It was just a typo. What about AI? :wink:

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.

did you read Uptime - Home Assistant ?

1 Like

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