Minutes into hours and minutes

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.
1 Like

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 %}
1 Like

actually I indeed forgot the last bit of the code :blush:
But I also corrected the spacing in the second line.
Now it Works :slight_smile: 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 %}
2 Likes

The state of “sensor.charging_time_left” can be “unavailable”

I used the last template and it Works very fine :slight_smile:

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 :slight_smile: good to know

Sorry, I was away yesterday. It shouldn’t add an hour. Is your timezone +1?