#Render entity with device_class duration in human readable format

1 messages ยท Page 1 of 1 (latest)

edgy cypress
#

Anyone know how I would render an entity that represents a duration in a human readable format for my dashboards?

I saw that there's this bug: https://github.com/home-assistant/frontend/issues/23338
But I'm not sure how to work around it. My dashboard previously did work described by that bug

daring heart
#

you could template something in a helper to make something readable you could display on dashboard

edgy cypress
#

(I'm aware of the irony of my proposed solution being even more complicated ๐Ÿ˜…)

daring heart
#
{% set minutes = ((time % 3600) / 60) | int %}
{% set hours = ((time % 86400) / 3600) | int %}
{% set days = (time / 86400) | int %}
{%- if time < 60 -%}
  Less than a minute
{%- else -%}
  {%- if days > 0 -%}
    {%- if days == 1 -%}
      1 day
    {%- else -%}
      {{ days }} days
    {%- endif -%}
  {%- endif -%}
  {%- if hours > 0 -%}
    {%- if days > 0 -%}
      {{ ', ' }}
    {%- endif -%}
    {%- if hours == 1 -%}
      1 hour
    {%- else -%}
      {{ hours }} hours
    {%- endif -%}
  {%- endif -%}
  {%- if minutes > 0 -%}
    {%- if days > 0 or hours > 0 -%}
      {{ ', ' }}
    {%- endif -%}
    {%- if minutes == 1 -%}
      1 minute
    {%- else -%}
      {{ minutes }} minutes
    {%- endif -%}
  {%- endif -%}
{%- endif -%}```
#

stick that into a template sensor obviously chaging the first line to point at the right entity

#
#

its not exactly elegent but will do for now i suspect

edgy cypress
#

sounds good, I'll take a look. thanks

daring heart
#

you got some options anyway ๐Ÿ˜›

jaunty tulip
#

I just skimmed over the thread and was going suggest Easy Time but the OP already mentioned it earlier.
I meant to check it out a couple of months ago but forgot about it. Just loaded it now.
I wish I would have tried this a long time ago. It would have caused so much less hassle.

#

I tried out a couple of examples in Developer Tools. ```javascript
{% from 'easy_time.jinja' import clock, clock_icon, easy_time %}
{{ clock() }}
{{ clock_icon(12) }}
{{ easy_time('input_datetime.automation_nighttime_motion_detection_start') }}

{% import 'easy_time.jinja' as et with context %}
{{ et.clock('24-hr') }}
{{ et.easy_time('input_datetime.automation_nighttime_motion_detection_start') }}
{{ et.easy_time('input_datetime.automation_nighttime_motion_detection_end') }}
{{ et.easy_time(states.sensor.s22_ultra_battery_level.last_updated, short=True)}}
{{ et.big_time(states.sensor.s22_ultra_battery_level.last_updated)}}
which returned
11:02 PM
mdi:clock-time-twelve
23 hours

23:02
23 hours
12 hours
45min
44 minutes and 54 seconds

civic moon
#

Is just changing the unit to hours an option?
I think that's the current officially recommended way to get HH:MM display.

edgy cypress
#

what I ended up with for the record:

    {% from 'easy_time.jinja' import big_time -%}
    {{ big_time(now() + timedelta(seconds=states('sensor.octoprint_print_time_left') | int), short=True) }}```
civic moon
#

it's not since in my case the data just comes out of octoprint as seconds
Whatever the integration provides, as the user you can change it in entity registry settings.

#

But if you're happy with the solution that works too ๐Ÿ‘

edgy cypress