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:
So it knows 7h 25m is the answer, but how can I get it to show that number here:
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.
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.
in the meantime I found solution
value_template: >
{{ (states(âsensor.daylengthchangetomorrowâ) | float * 3600)
| timestamp_custom(â%-H:%-M:%-S ', false) }}
{% 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.
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.
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)
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