Uptime No Longer Working

I use it this way:

- platform: uptime
  name: uptime_of_hassio
- platform: template
  sensors:
    template_uptime_of_hassio:
      value_template: >
        {% set time = (as_timestamp(states.sensor.date_time_iso.state) - as_timestamp(states.sensor.uptime_of_hassio.state))/60 | float %}
        {% set days = (time / (24*60)) | int %}
        {% set hours = ((time / 60) - ((time / (24*60)) | int ) * 24) | int %}
        {% set minutes = ((((time / 60) - (time / 60) | int) * 60) | round(1)) | int %}
        {% set seconds = ((time - (time | int)) * 60) | int %}
        {{days}}d {% if hours < 10 %}0{{ hours }}{% else %}{{ hours }}{% endif %}:{% if minutes < 10 %}0{{ minutes }}{% else %}{{ minutes }}{% endif %}:{% if seconds < 10 %}0{{ seconds }}{% else %}{{ seconds }}{% endif %}  
     

     

In lovelace it displays like this: 0d 03:04:08

pretty much everywhere but entities :rofl: :joy:

?

Looks fine to me:
IMG_0680

Edit: Oh, everywhere but… sorry.

Yah, it works in entities card, but not entity, picture-elements, etc. For some reason (Unless they fixed this) most of the cards do not check for sensors with the timestamp device class.

1 Like

Thanks for the heads up.
Any idea on how to fix it?
thanks

{{ (as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) / 60 > 5 }}

Amazing. Basically, I guess I was using the mod function instead of division.
thanks for the quick help!

the mod function was taking your total time and returning the remainder after dividing it by 3600. So you’d less than 5 minutes for 5 minutes every hour essentially.

Yup, I see that now. Amazing forum. thanks again.

better to be caught now than to come back in 9 months saying “my automations sometimes don’t fire”. That would have been a bitch to track down

Thank you all for the reply’s, extremely helpful

I have found though that all the above methods work but only update every 60 seconds and are around 25 seconds behind uptime

I could easily live with that but just in case is there a way to fix it?

Something is wrong…

back for some extra info and added the ‘old’ template, which calculates correctly based on the last_boot sensor:

this is the code in the dev template editor:

{{as_timestamp(states('sensor.last_boot'))}}     

          {%- set boot = as_timestamp(now())-as_timestamp(states('sensor.last_boot')) %}

          {%- macro phrase(name, divisor, mod=None) %}
            {%- set value = ((boot // 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),
                            phrase('hour', 60*60),
                            phrase('min', 60),
                            phrase('sec', 1, 60)]
                            |select('!=','')|list %}
          {{values[:-1]|join(', ') ~ ' and ' ~ values[-1] if values|length > 1 else
            values|first}}

#old version
          {% set boot = as_timestamp(now()) - as_timestamp(states('sensor.last_boot')) %}
          {% set seconds = boot|int %}
          {% set minutes = (boot // 60)|int %}
          {% set hours = minutes // 60 %}
          {% set days = hours // 24 %}
          {% set weeks = days // 7 %}

          {% set seconds = seconds % 60 %}
          {% set minutes = minutes % 60 %}
          {% set hours = hours % 24 %}
          {% set days = days % 7 %}

          {% macro phrase(value,name) %}
            {%- set value = value|int %}
            {%- 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'),phrase(seconds,'sec')]|select('!=','')
                         |list|join(', ') %}
          {% set last_comma = text.rfind(',') %}
          {% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}

          {{text}}

seems the streamlined version doesn’t mod the hours correctly.
Adding the Mod for the hours in the calculation:

          {%- set values = [phrase('week', 60*60*24*7),
                            phrase('day', 60*60*24),
                            phrase('hour', 60*60, 24),
                            phrase('min', 60,60),
                            phrase('sec', 1, 60)]
                            |select('!=','')|list %}

makes the calculation correct again:

should they be added to the other time units too, according to the old template:

#          {% set seconds = seconds % 60 %}
#          {% set minutes = minutes % 60 %}
#          {% set hours = hours % 24 %}
#          {% set days = days % 7 %}

I’m getting this - when I look at last_reboot sensor:

What’s going on with the hours?

Thanks!!!

check 1 post up…

duh! Thanks! :blush:

@eBoon, @Mariusthvdb I edited the post to have the correct template. Days was also wrong.

2 Likes

thanks Petro, had it done already :wink: and working perfectly:


but see what happens with the previously mentioned relative_time() on the last_boot sensor

      last_boot_relative:
        friendly_name: Last boot relative
        value_template: >
          {{relative_time(states.sensor.last_boot.last_changed)}}

      last_boot_template:
        friendly_name: Last boot template
        value_template: >
          {{as_timestamp(states('sensor.last_boot'))|timestamp_custom('%X, %d %m')}}

why would relative_time() be so off?

I did restart my instance, (not reboot the host), some 23 hours ago, and as such maybe the relative_time() to the last_boot sensor (which records the last boot of the host) was set ?

Never realized the relative_time() function was relative to the last restart of the instance too…

feels like a bug, when reading this:

relative_time converts datetime object to its human-friendly “age” string.

since the datetime object is correctly used in the other template posted above.

You’re comparing last_boot to uptime. Uptime is the time home assistant has been on, last_boot is how long your OS has been on. Not a bug, just different information.

Yes I understand that. But in all honesty I expected relative_time() on last_boot to use the recorded last_boot time, and not the uptime (of the restart).

relative_time accepts any datetime object… The context of the datetime object is meaningless. If anything, it proves that last_changed can’t be used for the last_boot sensor because it changed the state on restart and you have to use the method I posted above…

        {# use now() to update every minute #}
        {% set t = now() %}
        {{ relative_time(strptime(states('sensor.uptime'), '%Y-%m-%dT%H:%M:%S.%f%z')) }}

Remember, states get globally set on restart so last_changed reflects that.