Uptime No Longer Working

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:

Thanks petro and liamstears!

One question on your code petro.
Is there a way to translate week, day, hour, min and sec to swedish?

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

          {%- 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),  <--- change week to vecka (swedish)
                     phrase('day', 60*60*24, 7),  <-- and so on and on
                     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 }}

but!! When “day” turns to “day(s)” the “s” it adds dont translate to good swedish.
EX: Day - days, week - weeks and in swedish dag - dagar, vecka - veckor.

How to get this: “2 veckor, 2 dagar, 10 timmar, 24 min och 54 sek”
instead of: “2 weeks, 2 days, 10 hours, 24 mins och 54 secs”

sorry a different template would need to be developed for non-english.

Hi!
how can i change it to have this phrase?

12 d, 5 h, 35 m

Please can you help me?
Thanks

@KameDomotics,
@AcidSleeper

12 d, 5 h, 35 m format.

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

          {%- macro phrase(name, divisor, mod=None) %}
            {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
            {{- '{} {}'.format(value, name) if value | int > 0 else '' }}
          {%- endmacro %}
          
          {%- set values = [ 
                     phrase('w', 60*60*24*7), 
                     phrase('d', 60*60*24, 7),
                     phrase('h', 60*60, 24),
                     phrase('m', 60), 
                     phrase('s', 1, 60) 
                 ] | select('!=','') | list %}
                        
          {{ values | join(', ') }}

Any Language

Replace the English words with the proper word in your langauge.

          {%- set word_for_and = 'and' %}
          {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.hassio_drifttid_dagar')) %}

          {%- macro phrase(name, plural_name, divisor, mod=None) %}
            {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
            {%- set name = plural_name if value > 1 else name %}
            {{- '{} {}'.format(value, name) if value | int > 0 else '' }}
          {%- endmacro %}
          
          {%- set values = [ 
                     phrase('week', 'weeks', 60*60*24*7), 
                     phrase('day', 'days', 60*60*24, 7),
                     phrase('hour', 'hours', 60*60, 24),
                     phrase('minute', 'minutes', 60), 
                     phrase('second', 'seconds', 1, 60) 
                 ] | select('!=','') | list %}
                        
          {{ values[:-1] | join(', ') ~ ' ' ~ word_for_and ~ ' ' ~ values[-1] if values | length > 1 else values | first  | default }}

Any Language with duration entities (requires availability template)

template:
  - sensor:
      - name: "Daily Furnace Runtime"
        state: >
          {%- set word_for_and = 'and' %}
          {%- set up_time = (states('sensor.daily_furnace_uptime') | as_timedelta).seconds %}

          {%- macro phrase(name, plural_name, divisor, mod=None) %}
            {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
            {%- set name = plural_name if value > 1 else name %}
            {{- '{} {}'.format(value, name) if value | int > 0 else '' }}
          {%- endmacro %}
          
          {%- set values = [ 
                     phrase('week', 'weeks', 60*60*24*7), 
                     phrase('day', 'days', 60*60*24, 7),
                     phrase('hour', 'hours', 60*60, 24),
                     phrase('minute', 'minutes', 60), 
                     phrase('second', 'seconds', 1, 60)
                 ] | select('!=','') | list %}
                        
          {{ values[:-1] | join(', ') ~ ' ' ~ word_for_and ~ ' ' ~ values[-1] if values | length > 1 else values | first  }}
        availability: "{{ states('sensor.daily_furnace_uptime') | as_timedelta is not none }}"
7 Likes

No, I don’t want W and S…
Only D H M

then remove those lines

GREAT! Big BIG Thanks :+1:

Working great!

Thanks! Works fine

Got some questions that are prob really simple but I’m stupid lol

phrase('week', 60*60*24*7)

What would the correct values here be for months? And for years?

And how would I change this

{{ values | first }}

To show just the first and second values? And also to show only the first, second and third values?

Thanks in advance

Thanks @petro , it works great! I´d be nice to use this value in a badge, unfortunately when I try this only the year of a complete date shows up… probably it has a simple solution.
I´ve found this topic but no solution

regards,

@petro Hi there! Since 2021.4 I have problem with Template variable warnings i the log.

Can someone help with that. What to add and where?

  - platform: template
    sensors:
      hassio_drifttid:
        friendly_name: 'Hassio Drifttid'
        value_template: >-
          {%- set word_for_and = 'och' %}
          {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
          
          {%- macro phrase(name, plural_name, divisor, mod=None) %}
            {%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
            {%- set name = plural_name if value > 1 else name %}
            {{- '{} {}'.format(value, name) if value | int > 0 else '' }}
          {%- endmacro %}
          
          {%- set values = [ 
                     phrase('dag', 'dagar', 60*60*24, 7),
                     phrase('timme', 'timmar', 60*60, 24),
                     phrase('min', 'min', 60)
                 ] | select('!=','') | list %}
                        
          {{ values[:-1] | join(', ') ~ ' ' ~ word_for_and ~ ' ' ~ values[-1] if values | length > 1 else values | first }}

Warning:

2021-04-21 19:26:15 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: No first item, sequence was empty. when rendering '{%- set word_for_and = 'och' %} {%- set up_time = as_timestamp(now())-as_timestamp(states('sensor.uptime')) %}
{%- macro phrase(name, plural_name, divisor, mod=None) %}
{%- set value = ((up_time // divisor) % (mod if mod else divisor)) | int %}
{%- set name = plural_name if value > 1 else name %}
{{- '{} {}'.format(value, name) if value | int > 0 else '' }}
{%- endmacro %}
{%- set values = [
phrase('dag', 'dagar', 60*60*24, 7),
phrase('timme', 'timmar', 60*60, 24),
phrase('min', 'min', 60)
] | select('!=','') | list %}
{{ values[:-1] | join(', ') ~ ' ' ~ word_for_and ~ ' ' ~ values[-1] if values | length > 1 else values | first }}'

Link: https://community.home-assistant.io/t/template-variable-warning/304637

SOLVED!

@petro I’ve been using the code here marked as the solution and was wondering how I could modify the reading that, for example, says “3 months, 3 weeks, 6 days, etc.” to just cut off everything after “3 weeks” or whichever is the largest measured time frame?

Not sure if this next part is possible, but in the example mentioned above, that time frame is actually closer to 4 months, so would it be possible to “round up”?

Just use relative_time() macro that’s built into the system. Keep in mind that none of these templates (including mine) do months because months require a complex equation to calculate properly where all other time deltas are not. If you stick to 4 weeks in a month, then the calculation is easy but then the result is not accurate.

I was having problems with an old uptime sensor I had and I found your streamlined version and it works fine most of the time. It also has the same issue I was seeing.using my old template. In my entity card or a custom button card some of the time Instead of displaying the uptime it displays the word “running”. Not sure where this is coming from or how to fix it.
Thanks.

Are you sure you don’t have 2 template sensors with the same name in your configuration

I don’t think so, but I’ll check closer later. I added yours with a 1 after the name but maybe there is another one somewhere. Where with the sensor get the word “running” from? I have seen this in the developers tools for the sensor as well.

no-where, that’s why I’m asking about another sensor. Unless you added the word running to the template.

I found it. It was in a custom button template. Thanks.

1 Like