Create output from time sensor in hours and minutes instead of 7.43 hours

I have searched I promise, but a lot of the answers I didnt understand.

I have a graph, showing length of time at work.
It shows a time sensor in an hour format with a decimal place, ie it shows 7.43 hours, not 7hrs 25 minutes.

In states it shows the below:

image

So it knows 7h 25m is the answer, but how can I get it to show that number here:

image

You won’t be able to. You either display the time as time and get a bar graph, or you display the time as a base 10 number and get a graph.

You’re trying to mix some of a with some of b. You won’t be able to do this without something custom.

Thanks! How would I go about expressing the time as time though, that’s what is causing the main annoyance and sticking point, I just don’t understand how to take the decimal and make it a time.

You’ll need to make a template sensor that converts hours to hours:minutes

Use this template.

{% set hours = states('sensor.time_at_work') | float %}
{% set minutes = ((hours % 1) * 60) | int %}
{% set hours = (hours - (hours % 1)) | int %}
{{ '%02i:%02i'%(hours, minutes) }}

{% set hours = states('sensor.time_at_work') | float %} - Gets your time at work, converts to float
{% set minutes = ((hours % 1) * 60) | int %} - Gets the remainder of hours / 1, then multiplies by 60 and converts to an int.
{% set hours = (hours - (hours % 1)) | int %} - Subtracts the remainder from hours and converts to an int
{{ '%02i:%02i'%(hours, minutes) }} - formats time so that it will be xx:xx for time (07:25 for 7.43). The minutes will truncate the remainder and 25.8 minutes will be 25.

5 Likes

Amazing thank you! Will give it a go once I work out why I can no longer connect to my Pi :s

With the new Entity card you can now specify “value” as an attribue so you can see proper time instead of decimal time. But it is still not possible with the graph card.

1 Like

how to add seconds to this template

1 Like

in the meantime I found solution
value_template: >
{{ (states(‘sensor.daylengthchangetomorrow’) | float * 3600)
| timestamp_custom(’%-H:%-M:%-S ', false) }}

Thanks

Variation to get hh:mm:ss from minutes:

{% set m = 79.2 %}
{% set s = ((m % 1) * 60) | int %}
{% set h = ( m / 60 ) |int %}
{% set m = (m - (m % 1)) | int %}
{% set m = (m - (h * 60)) | int %}
{{ '%02i:%02i:%02i'%(h, m, s) }}

Examples:

  • 4.2 minutes → 00:04:12
  • 142.5 minutes → 02:22:30

In my use case, m is a computed float that I use to feed a timer duration value with.

{% set m = 79.2%}
{{ '%02i:%02i:%02i'%(m/60,m%60,(60*m)%60) }}

:sunglasses:

1 Like

true that :slight_smile:

The task is simplified by using timestamp_custom.

{{ (79.2 * 60) | timestamp_custom('%H:%M:%S', false) }}
2 Likes

With the likely-unimportant caveat that this is limited to 24 hours…

Yes. I was working within the topic’s original scope (7.43 hours) but if the time exceeds 24 hours, you are correct, this technique will produce an incorrect result.

FWIW, it can be reduced to this:

{{ (79.2 * 60) | timestamp_custom('%T', false) }}
1 Like

So once I use this and created my sensor.time_at_work_converted. How do I use it when I want to display it?

{% set entity_id = 'sensor.tesla_charger_power_2' %}
{% set timeleft = states('sensor.tesla_time_to_full_charge_2_converted') %}
 {%- if is_state(entity_id, '8')  -%}
{{-'\u26A1'}} Noir is charging with {{timeleft}} left.

I want it to say" with 2 hours and 30 minutes left". But the above doesn’t work. It gives out “unknown”. Even though the sensor works fine. (I see it in dev tools)

Thanks

did you ever figure this out? I’m trying to get the same information in a “HH hours and MM minutes left” message. I’m working off of a sensor that has an attribute with minutes but can’t figure out the code to get to the HH hours and MM minutes format

Simple version:

{% set m = state_attr('sensor.your_sensor', 'minutes_attribute')|round(0) %}
{{ "%d hours %d minutes left" % (m//60, m%60) }}

More complex version. only showing hours if needed, with plurals (1 minute, 2 minutes):

{% set m = state_attr('sensor.your_sensor', 'minutes_attribute')|round(0) %}
{% if m >= 60 %}
  {{ "%d hour%s %d minute%s left" % (m//60, ('s','')[m//60==1], m%60, ('s','')[m%60==1]) }}
{% else %}
  {{ "%d minute%s left" % (m, ('s','')[m==1])}}
{% endif %}

The // operator is “divide and ignore any remainder”; the % operator is modulus: “just give me the remainder”.