Uptime No Longer Working

relative time already does that. all you’d have to add is ‘ago’ after the template:

{{ relative_time(states.sensor.uptime.last_changed) }} ago

But you’d REALLY need to implement the availability_template to ensure it’s ‘unavailable’ when the calculation fails (So that it doesn’t read unavailable ago)

yeah, I only posted the snippet for the longer template you posted, not as a full replacement.

About the

what do you mean by that? relative_time() only mentions the biggest of the available units?

relative_time converts datetime object to its human-friendly “age” string. The age can be in second, minute, hour, day, month or year (but only the biggest unit is considered, e.g., if it’s 2 days and 3 hours, “2 days” will be returned).

so if you need the phrase, relative_time() isnt of much help

If you’re referring to a phrase as 10 hours, 3 minutes, then yes, relative_time does not do that. It would only return 10 hours.

yeah, thought that was was the OP liked to see

my phrase template (which was heavily copied from a couple of fellows here, not taking any credit) also drops in weeks and seconds :wink:

pretty sure I built it for you a few years back :wink:

haha, probably!

you had a big hand in this too, but it mysteriously stopped calculating passed and remaining days correctly…

FYI, here’s a streamlined version

          {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}

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

For other languages or variations to this template, see here:

7 Likes

nice!!
and it works equally well with the last_boot sensor.
magic.

It’ll work with any time difference in seconds.

I have not updated yet, but use uptime in many of my automations as a condition to keep them from running during restart. To be honest I added these conditions years ago, and it’s just worked so I have not looked for better ways to do it. I dont care much about displaying the uptime, just want to keep automations from triggering during the first X minutes after a restart.

What is the recommended way to do that these days? Most of my automations with an uptime trigger are set to 1-2 minutes, and a few are set longer for devices that take a little longer to report status sometimes.

So I have a “HA not restarted recently sensor” which was based off minutes of uptime… I just want to show the minutes similar to this:

but without the word minutes… removing “relative_time” just breaks the whole thing and I’m lost with all these time formatting things.

This sensor is used as a condition to keep automations from triggering after a restart…

- platform: template
  sensors:
    home_assistant_not_restarted_recently:
      value_template: "{{ states('sensor.ha_process_uptime_sensor')| float > 6 }}"

And with the old uptime sensor the state was just a numeric value… I’m not sure if it’d be easier to redo the whole restarted recently sensor, or just make another sensor that provides just the numeric number of minutes like it was before.

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

EDIT: Had math reversed.

1 Like

My automations used to be:

    - condition: numeric_state
      entity_id: sensor.uptime
      above: .1

and is now:

    - condition: template
      value_template: "{{ (((as_timestamp(now()) - as_timestamp(states('sensor.uptime'))) % 3600) / 60) | int > 5 }}"

This new condition is set for 5 minutes.

1 Like

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.