I have a sensor value displayed in minutes but it would much more readable if displayed in hours and minutes.
Is this conversion somehow possible?
Not sure if this is the most elegant solution but you can achieve this with templates.
{% set my_test_json = {
"travel_time": 152,
"unit": "minutes"
} %}
The travel time is {% if my_test_json.travel_time > 60 %}{{ my_test_json.travel_time // 60 }}:{{ '{:0>2d}'.format(my_test_json.travel_time%60) }} hours{% else %}{{my_test_json.travel_time}} minutes{% endif %}.
Result:
The travel time is 2:32 hours.
you can create a template sensor that reads the first one. Then just hide the first one using a customize section.
sensor:
- platform: template
sensors:
convert_to_hours:
value_template: >
{% set minutes = ( states('sensor.minute_sensor') | int * 60 ) | timestamp_local %}
The travel time is {{ strptime(minutes, '%Y-%m-%d %H:%M:%S').strftime('%H:%M') }} hours.
I tried to integrate your suggestion into a template but it returns an error.
- platform: template
sensors:
minutes_to_hours:
value_template: >
{% if sensor.charging_time_left > 60 %}{{ sensor.charging_time_left // 60 }}:{{ '{:0>2d}'.format(sensor.charging_time_left%60) }} hours{% else %}{{sensor.charging_time_left}} minutes{% endif %}
-It seems that this template adds one hour to the correct time.
-When no data is available this template still returns â01:00â
-Is it possible to delete the pre 0 when hours returns less than â10â?
.
Whatâs your error?
2018-07-05 22:32:44 ERROR (MainThread) [homeassistant.components.sensor.template] Could not render template minutes_to_hours: UndefinedError: âsensorâ is undefined
Oh, I see. Try this:
- platform: template
sensors:
minutes_to_hours:
value_template: >
{% set ct = states('sensor.charging_time_left') | int %}
{% if ct > 60 %}{{ ct // 60 }}:{{ '{:0>2d}'.format(ct%60) }} hours{% else %}{{ct}} minutes{% endif %}
I get another error now
2018-07-05 23:08:28 ERROR (MainThread) [homeassistant.components] Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: tag name expected) for dictionary value @ data[âsensorsâ]['minutes_to_hours][âvalue_templateâ]. Got â{% set ct = states(âsensor.charging_time_leftâ) | int %} {% if ct > 60 %}{{ ct // 60 }}:{{ â{:0>2d}â.format(ct%60) }} hours{% else %}{{ct}} minutes{% \nâ. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
Are you sure that you correctly copied that piece of code?
I think the spacing of the second line is wrong. sensors
should line up with platform
:
- platform: template
sensors:
minutes_to_hours:
value_template: >
{% set ct = states('sensor.charging_time_left') | int %}
{% if ct > 60 %}{{ ct // 60 }}:{{ '{:0>2d}'.format(ct%60) }} hours{% else %}{{ct}} minutes{% endif %}
actually I indeed forgot the last bit of the code
But I also corrected the spacing in the second line.
Now it Works but when no data is available it still returns â0 minâ. Is it possible to show âUnavailableâ instead?
Depends on what you mean by âwhen no data is availableâ. How exactly is that determined? Is that when states('sensor.charging_time_left')
is 'unknown'
? If so, then how about this:
- platform: template
sensors:
minutes_to_hours:
value_template: >
{% set ct = states('sensor.charging_time_left') %}
{% if ct == 'unknown' %}
Unavailable
{% else %}
{% set ct = ct | int %}
{% if ct > 60 %}
{{ ct // 60 }}:{{ '{:0>2d}'.format(ct%60) }} hours
{% else %}
{{ ct }} minutes
{% endif %}
{% endif %}
Or maybe just this:
- platform: template
sensors:
minutes_to_hours:
value_template: >
{% set ct = states('sensor.charging_time_left') | int %}
{% if ct == 0 %}
Unavailable
{% elif ct > 60 %}
{{ ct // 60 }}:{{ '{:0>2d}'.format(ct%60) }} hours
{% else %}
{{ ct }} minutes
{% endif %}
The state of âsensor.charging_time_leftâ can be âunavailableâ
I used the last template and it Works very fine
Not beeing very good at this templating I Wonder what âctâ is for. Is it a kind of internal flag? Why can´t âsensor.charging_time_leftâ be used all through the template?
I Wonder if it would be possible to use this time to show the actual time of day when charging is complete.
I guess maybe I could add the charging time to the actual time?
ct
is a local variable to cache the value/state of sensor.charging_time_left
.
{% set ct = states('sensor.charging_time_left') %}
This way you donât have to query the state over and over, which (very) theoretically might end in varying states throughout the template.
So it is to ensure that the same value is used within the entire template and not change maybe halfways through the cycle. (if it calculates the template in a cycle)
Correct, but again, this is not very likely to happen. It is simply more efficient to only query the state once and to use that value throughout the template.
okay thanks good to know
Sorry, I was away yesterday. It shouldnât add an hour. Is your timezone +1?