Gah, I must be tired. I went back one last time after posting this and found my mistake.
Have to set the actual values for minutes and hours:
{% set time = states.sensor.esxi_raw_uptime.state | int %}
{% set minutes = ((time % 360000) / 6000) | int%}
{% set hours = ((time % 8640000) / 360000) | int %}
{% set days = (time / 8640000) | int %}
I was using the original code but i wanted to shorten the ouput so i modified the code to output 00d 00h 00m. Much better IMO than the elongated 00 days, 00 hours, 00 minutes.
Heres the code:
value_template: >-
{% set time = (value | int) | int %}
{% set minutes = ((time % 360000) / 6000) | int %}
{% set hours = ((time % 8640000) / 360000) | int %}
{% set days = (time / 8640000) | int %}
{%- if time < 6000 -%}
Less than a minute
{%- else -%}
{%- if days > 0 -%}
{{ days }}d
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ hours }}h
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ minutes }}m
{%- endif -%}
{%- endif -%}
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 -%}
sorry take my long time to try this i was busy i try change the sensor.nut_ups_battery_runtime but still not working my name same as you sensor.nut_ups_battery_runtime it give this error ?
If you’re using the exact same configuration as shown in @LJM9000’s post above: The indentation is wrong; all lines from friendly_name onwards must be indented by 2 more spaces.
And have you changed your user interface configuration? That code does not change the way the original sensor stores and represents its data, it’s creating a new sensor.
In the entities card that currently displays all data about your UPS you will have to change the entity ID from sensor.nut_ups_battery_runtime to sensor.nut_ups_runtime_friendly.
i fully agree, the sensors i’m converting into human-readable are updating once per hour, seconds, even minutes are of little importance.
The thing is, that your formulas for hours days & weeks are wrong.