You can simplify this a ton:
{%- macro formatvalue(name, value, sep='') %}
{%- set name = '{}s' if value > 1 else name %}
{{ '{} {}{}'.format(value, name, padding) if value >= 1 else '' }}
{%- endmacro %}
{%- set time = ((as_timestamp(now()) - as_timestamp(states.sensor.pump.last_changed))) %}
{%- set minutes = formatvalue('minute', (time % 3600) // 60, '') %}
{%- set hours = formatvalue('hour', (time % 86400) // 3600, ', ') %}
{%- set days = formatvalue('day', time // 86400, ', ') %}
{{ 'Less than 1 min' if time < 60 else days + hours + minutes }}
Let me break it down:
This is a macro. It does 3 things. It takes any ‘name’, like hour, minute or day and pluralizes it based on the value. It also adds a separator, like a comma and a space. You don’t even need the comma if you don’t want. it.
{%- macro formatvalue(name, value, sep='') %}
{%- set name = '{}s' if value > 1 else name %}
{{ '{} {}{}'.format(value, name, padding) if value >= 1 else '' }}
{%- endmacro %}
breaking down the macro:
# start of the macro, 'formatvalue' can be used many times in your template
# it takes 3 inputs, name, value and sep.
# name is the word you want to pluralize based on value
# value is the value that the pluralization is based on.
# sep is the separator between words.
{%- macro formatvalue(name, value, sep='') %}
# if the value is greater than 1, change name to have an s
{%- set name = '{}s' if value > 1 else name %}
# if value is greater than zero, return a fully formatted value and name.
# otherwise return an empty string.
{{ '{} {}{}'.format(value, name, padding) if value >= 1 else '' }}
# end of macro
{%- endmacro %}
Breaking down the template:
# get the time, we don't care if it's rounded or not.
{%- set time = ((as_timestamp(now()) - as_timestamp(states.sensor.pump.last_changed))) %}
# use the macro to get the minute phrase. Use int division (//) to get an integer back.
# does not add any separator after minutes because it's the last item in the phrase.
{%- set minutes = formatvalue('minute', (time % 3600) // 60, '') %}
# use the macro to get the hour phrase. Use int division (//) to get an integer back.
# also adds a , and space after the number of hours.
{%- set hours = formatvalue('hour', (time % 86400) // 3600, ', ') %}
# use the macro to get the day phrase. Use int division (//) to get an integer back.
# also adds a , and space after the number of days.
{%- set days = formatvalue('day', time // 86400, ', ') %}
# if time is less than a minute, say that, otherwise report the full time
# use ~ to cast each variable to a string and add them together
{{ 'Less than 1 min' if time < 60 else days ~ hours ~ minutes }}