To contribute my version: it abuses Jinja’s whitespace control. Easily extendable to Months, Quarters,Years, etc.
edit //
It was breaking on exactly 60 minutes, 1 day, etc. Should be more robust now!
# NOTE:
# - define durations for the various UNITs of time
# * modulus helps to reduce "extras" of the larger time units
#
# - now build the string
#
# for each UNIT of time that's greater than 1:
# if it's not the first loop iteration:
# append ", " to the string
# else:
# 1. convert the DURATION to STR
# 2. split DURATION on "."
# 3. append the left part (the whole number) to the string
# 4. append the UNIT to the string
#
# ...but if that's no UNITs, then we just started so
# set the string to "just now"
#
- platform: template
sensors:
hass_uptime:
value_template: >-
{%- set uptime = states.sensor.hass_uptime_minutes.state | round -%}
{%- set sep = ', ' -%}
{%- set TIME_MAP = {
'week': (uptime / 10080) % 10080,
'day': (uptime / 1440) % 7,
'hour': (uptime / 60) % 24,
'minute': (uptime % 60)
}
-%}
{%- for unit, duration in TIME_MAP.items() if duration >= 1 -%}
{%- if not loop.first -%}
{{ sep }}
{%- endif -%}
{{ (duration | string).split('.')[0] }} {{ unit }}
{%- if duration >= 2 -%}
s
{%- endif -%}
{%- endfor -%}
{%- if uptime < 1 -%}
just now
{%- endif -%}