Uptime No Longer Working

That condition is wrong. That will be on every hour on the hour (from startup) for 5 minutes.

Another stupid question:
“unit_of_measurement” is deprecated for all sensors or only for the uptime sensor?
And in case of all sensors, how to change it?

uptime only

1 Like

Where doesn’t it display properly?

I’m only using it in an entities card and and it works fine.

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