History stats is great at collecting statistics, but the output isn’t readable or really usable as-is. A string value of 2.15 doesn’t correlate to 2 hours 15 minutes. It’s actually 2 hours 9 minutes. So I had a goal to fix this. I borrowed some code snippets from a few HA Uptime posts from @petro and various others to use as the base, and modified it to do the math needed to convert the History Stats values. Now, the output for a decimal value says “x hours x minutes”, which is much more readable for humans. I figured I’d share it in case others are interested. Enjoy!
template:
- sensor:
- name: "Daily Furnace Runtime"
state: >-
{% set min = states('sensor.daily_furnace_uptime').split('.')[1] | int %}
{% set hr = states('sensor.daily_furnace_uptime').split('.')[0] | int %}
{% set min1 = min / 100 %}
{% set min2 = min1 * 60 %}
{% set minutes = min2 | int % 60 %}
{% set hours = hr % 24 %}
{% macro phrase(value,name) %}
{%- set value = value %}
{%- set end = 's' if value > 1 else '' %}
{{- '{} {}{}'.format(value,name,end) if value|int > 0 else '{} {}{}'.format(value,name,'s') }}
{%- endmacro %}
{% set text = [phrase(hours,'hour'),
phrase(minutes,'min')]
|select('!=','')|list|join(', ') %}
{% set last_comma = text.rfind(',') %}
{% if last_comma != -1 %}
{% set text = text[:last_comma] + ' and' + text[last_comma + 1:] %}
{% endif %}
{{text}}