How to display ONE Line of a Template Sensor - Hours and Minutes

Chaps

I’m tracking the hours and minutes my kids watch Twitch Streamers. Originally I just had a “COUNTER” that was active when they were watching which have me a NUMBER total.

I followed another thread and used someone’s templating knowledge (far above mine) to create a template that converts it into Hours and Minutes. This is great but if I try and display this in a card (or Dev Tools) it gives the following output (With all the words included in front of the result)

  • platform: template
    sensors:
    minutes_to_hours:
    value_template: >
    0 minutes

Is there anyway JUST to display the lines with Hours and Minutes

Here is the template code

  • platform: template
    sensors:
    minutes_to_hours:
    value_template: >
    {% set ct = states(‘sensor.template_sensor_twitch_streamers_active’) | int %}
    {% if ct > 60 %}{{ ct // 60 }}:{{ ‘{:0>2d}’.format(ct%60) }} hours{% else %}{{ct}} minutes{% endif %}

Please post your code as preformatted text (</> in the cogwheel menu). This makes it easier for people to copy your code so that they can try it out.

1 Like

Hey man, sorry I didn’t know you could do that
So this is the Template that converts hours to minutes

- platform: template
  sensors:
    minutes_to_hours:
      value_template: >
        {% set ct = states('sensor.template_sensor_twitch_streamers_active') | int %}
        {% if ct > 60 %}{{ ct // 60 }}:{{ '{:0>2d}'.format(ct%60) }} hours{% else %}{{ct}} minutes{% endif %}

And then I display the result in an entities card

card:
  type: entities
  entities:
    - entity: sensor.template_sensor_twitch_streamers_active_hours_minutes
      name: Time Streaming
  title: Twitch Streamers Last Active / Total Hours
  state_color: true

For completeness. This is the output I receive. Would be nice if it was just the minutes and hours results.

Are you placing the template code in the card yaml?
That is not how you should do it.

Either in configuration.yaml or easier, do it in settings → devices & Services → Helpers → and add a template sensor.
in the code field just past the last two lines.

1 Like

Mate
Your a LEGEND
Yes I already had it in a Template Sensor (Setting/Helpers/Sensor)
But yes I had all the text. I didn’t realise I just needed the last two lines

For anyone else that stumbles across this and wants the solution.

Original Code Template Sensor Code

- platform: template
  sensors:
    minutes_to_hours:
      value_template: >
        {% set ct = states('sensor.template_sensor_twitch_streamers_active') | int %}
        {% if ct > 60 %}{{ ct // 60 }}:{{ '{:0>2d}'.format(ct%60) }} hours{% else %}{{ct}} minutes{% endif %}

New Template Sensor Code

{% set ct = states('sensor.template_sensor_twitch_streamers_active') | int %}
        {% if ct > 60 %}{{ ct // 60 }}:{{ '{:0>2d}'.format(ct%60) }} hours{% else %}{{ct}} minutes{% endif %}