Template help to improve on this?

At the risk of getting boring I think we also need to amend

{%- set end = 's' if value > 1 else '' %}

with another | int to be

{%- set end = 's' if value | int > 1 else '' %}

else we get an ‘s’ whenever the value is between 1 and 2

Yep, that spot too!

hell you could just do this:

{%- macro phrase(value, name) %}
{%- set value = value | int %}
{%- set end = 's' if value > 1 else '' %}
{{- '{} {}{}'.format(value, name, end) if value > 0 else '' }}
{%- endmacro %}
1 Like

@klogg , thanks :grin: looking forward to seeing the final product. I used your initial example but it wasn’t working so I amended it to use minutes from the standard uptime counter and that has been working

I think we’re done here.
I use the term ‘we’ loosly, @petro did 99% of the work.

  - platform: uptime

  - platform: template
    sensors:
      ha_uptime:
        friendly_name: HA Uptime
        value_template: >
          {% if states('sensor.uptime') == '0.0' %} 
            Just restarted...
          {% else %}
            {% 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 weeks = (states('sensor.uptime') | int / 7) | int %}
            {% set days = (states('sensor.uptime') | int) - (weeks * 7) %}
            {% set hours = (states('sensor.uptime') | float - states('sensor.uptime') | int) * 24 %}
            {% set minutes = (hours - hours | int) * 60 %}

            {{ [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') }}
          {% endif %}

gives this:

image

I don’t know if I’ll ever see it showing weeks. I don’t seem to be able to leave my config alone for that long. This in itself is evidence of that. I just couldn’t leave it showing a decimal number of days :slight_smile:

1 Like

why is this? I have several other online sensors that start counting immediately and show minutes online? seems a bit silly to have such a magic macro be off for the 14 first minutes…

I think it is worse than that, I think it is actually only right once every 14 minutes.
The sensor counts days to 2 decimal places. 0.01 of a day is 14 minutes.

A bit silly really I agree but there you go. On the other hand it’s not really a big deal once you go over few hours. It’s not exactly a critical sensor :wink:

i see, yes, of course…

03

it only changes per 14 minutes…

I have several:

40
uptime template is your/@petro’s macro, time online the other sensor based on sensor.uptime.

Would be nice if we had one that would be exact and count in minutes

maybe use last_boot or since_last_boot from the system monitor?

34

I think last_boot is different from the sensor.uptime.

last_boot is I think the time the machine was restarted.
the sensor.uptime is (I think) the time since HA was last restarted - to the nearest 14 minutes :slight_smile:

image

last_boot is when it was last booted…
uptime when it was last restarted. Since the latter is hardly ever over a day in my setup, I thought it would be nice to check the macro on last_boot, which as you can see in my setup has a longer time to calculate, 12 days now, since it last hung on history :wink:

I’m not sure what all your sensors are or where you got them from!

I only have two:
uptime - the time in days since HA was restarted (from platform: uptime)
last_boot - returns only the date when the machine was last rebooted (from platform: systemmonitor)

this is what I have:

  since_last_boot_template:
    friendly_name: 'Since last boot'
    value_template: >
      {{states('sensor.since_last_boot').split('.')[0]}}

  last_boot_template:
    friendly_name: 'Up'
    value_template: >
      {{ as_timestamp(states.sensor.last_boot.last_changed) | timestamp_custom( "%X, %d %m") }}

  hassio_online:
    friendly_name: Hassio online
    value_template: >
      {{ relative_time(states.binary_sensor.hassio.last_changed) }}

  uptime_template:
    friendly_name: Uptime template
    value_template: >
      {% macro phrase(value, name) %}
      {%- set end = 's' if value > 1 else '' %}
      {{- '{} {}{}'.format(value | int, name, end) if value | int > 0 else '' }}
      {%- endmacro %}
      {%- set weeks = (states('sensor.uptime') | int / 7) | int %}
      {%- set days = (states('sensor.uptime') | int) - (weeks * 7) %}
      {%- set hours = (states('sensor.uptime') | float - states('sensor.uptime') | int) * 24 %}
      {%- set minutes = (hours - hours | int) * 60 %}
      {{ [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | join(', ') }}

Where do those first three originate (I recognise the last one :slight_smile:)

sensor.since_last_boot
sensor.last_boot.last_changed
binary_sensor.hassio.last_changed

first 2 are based on systemmonitor System monitor - Home Assistant
the binary sensor is a binary_sensor_ping

bw, this is enlightening: Absolute vs. Relative Timestamps: When to Use Which

Thanks, I missed that.
But what is it telling me???

EDIT: got it (20 days, 9 hours and 55 seconds)

I noticed this yesterday when I tried your original code. I thought it wasn’t working (due to it showing zero for quite a while) so I changed the standard uptime counter to minutes and adjusted the template to suit. It seems to be nice that way. Would you consider changing your setup to be based on minutes rather than hours? I’m the same in that I restart HA so often these days to include new code that it rarely goes a week without a restart! Yesterday I must have done about 10 or more to include new automations etc :expressionless:

How did you do that??

Just a quick play and this works but It’s not super elegant and I am not sure what the format of since_last_boot will be for less than a day and I can’t try now, maybe tomorrow…

{% 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 minutes = states('sensor.since_last_boot').split(':')[-2] %}
{% set hours = states('sensor.since_last_boot').split(', ')[1] %}
{% set hours = hours.split(':')[0] %}
{% set days = states('sensor.since_last_boot').split(' ')[0] %}
{% set weeks = (days | int / 7) | int %}
{% set days = (days | int) - (weeks * 7) %}


{{ [ phrase(weeks, 'week'), phrase(days, 'day'), phrase(hours, 'hr'), phrase(minutes, 'min') ] | select('!=','') | list | join(', ') }}

sensor:
  - platform: uptime
    name: 'HassIO uptime'
    unit_of_measurement: minutes

ok, so my templating efforts didn’t quite work correctly!

image