Uptime No Longer Working

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.

yeah, too bad we can’t use the last_boot sensor like that. O well. Might not be that useful anyways…

Im using this code, and its not working, just showing unavailable.

  - platform: uptime
    name: Hassio Drifttid Dagar
    
  - platform: template
    sensors:
      hassio_uptime:
        friendly_name:  Hassio Drifttid
        value_template: >
          {# use now() to update every minute #}
          {% set t = now() %}
          {{ relative_time(strptime(states('sensor.hassio_drifttid_dagar'), '%Y-%m-%dT%H:%M:%S.%f%z')) }}
        availability_template: >
          {{ strptime(states('sensor.hassio_drifttid_dagar'), '%Y-%m-%dT%H:%M:%S.%f%z') is not string }}

1

Why isnt Im seeing some uptime?

because `sensor.hassio_drifttid_dagar’ is not a timestamp sensor.

Okey, but its your code from 4 days ago so I just assumed it would be working. =(

So how to change the code to get it to work?

You won’t be able to use the code I posted. The code I posted expects a sensor with a datetime stamp. I.e. 2020-21-12T00:00:00.000+00:00

Your sensor is a sensor based in days. You’ll have to do the calculation.

this reminds me, sorry for the delay, why wont last_boot show a relative_time():

note it doesn’t display milliseconds, so the strptime must be adjusted? Don’t know how to take those out though

edit

wait: its this:
take out the .%f

sorry.

{{ relative_time(strptime(states('sensor.last_boot'), '%Y-%m-%dT%H:%M:%S%z')) }}

Solution for myself:

  - platform: uptime
    name: Hassio Drifttid Dagar
    
  - platform: template
    sensors:
      hassio_drifttid:
        friendly_name: 'Hassio Drifttid'
        value_template: >-
          {% set up_time = states.sensor.hassio_drifttid_dagar.state | float / 1 | round(2) | int %}
          {% if up_time == 0 %}
            Just restarted.
          {% else %}
            {% set hours = (up_time * 24) %}
            {{ hours }}timmar
          {%- endif -%}

This worked until now. Now it dont work anymore.

Help please!

Please read the thread… There’s about 20 different solutions.

Not to be a drag but when I used your code, it wasnt usable as you said, I corrected the code and it worked for a while. And now I get a, in my opinion, a sad respons to my question.
If you know something that work please point me to that post. Since upgrading makes some solutions obsolete!

Im no expert and need some help on the way.

This post calculates minutes. Remove the > 5 and adjust the result to days or hours by dividing by the appropriate amount.

I am no expert either but I found this code slightly modified from one posted above works best for me:

- platform: template
  sensors:
    homeassistant_uptime:
        friendly_name: HomeAssistant 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}}

That ones been posted a few times in this thread already, including an updated version. This is why I asked him to read the other posts. I have no clue if he’s actually done that.

I understand but I think I am like him and just confused by the amount of different methods to do this and l expect he has ran into the same problems as me and that is that some of the methods posted here did not work for me. The above seems to work for me so far but if you have a better solution please post it and then I can mark that as being the solution to put this topic to a close and save confusion. Thank you for you knowledge and input

You’re the thread owner, I’ll gladly mark this as a solution as long as you’re ok with it. It’s the updated streamlined version of what you just posted.

Thank you, I will test over the next 24 hours and then update the thread, thank you :+1: