Show hours and minutes not decimal hours in history_stats

Is there any way this code:

  - platform: history_stats
    name: Time at Work
    entity_id: device_tracker.matts_iphone
    state: 'Work'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
    end: '{{ now() }}'

can show X hours and X minutes rather than how it currently works which is to show 44.49 hours which actually means 44 hours and 0.49 of an hour (so nearly 30 minutes)

My current card is:

#Showing time at work this week
    - type: sensor
      entity: sensor.time_at_work
      name: Time at work this week
      graph: line # or can be none
      detail: 60    
      hours_to_show: 189

1 Like

Did you ever find an answer to this? I’ve seen several empty threads with similar questions :frowning:

yep I had to create a temple sensor that created a secondary sensor.

sensor:
  - platform: template
    sensors:
      time_at_work_rounded:
        value_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) }}      

 
1 Like

Reviving an old thread - it’s 2022 now - any way to do this without an additional template sensor??

thanks

I don’t think so but you can simplify and pretty it up. Discovered templates understand a timedelta type so let it do the heavy lifting. Outputs H:MM:SS format, just slice off the last three characters if you want H:MM

sensor:
  - platform: template
    sensors:
      time_at_work_rounded:
        value_template: >
          {{ timedelta(hours=states('sensor.time_at_work') | float(0)) }} 
6 Likes

The history_stat sensor has an attribute “value” that shows the time in the format you want.

With Home-Assistant 2022.05, the "value" attribute is gone. :-(
https://github.com/home-assistant/core/pull/70720 
https://www.home-assistant.io/blog/2022/05/04/release-20225/#breaking-changes

the Duration does not work for start and end time for whatever reason, so time is now rendered as decimal hours, which no human beeing can make sense of. 
Just wondering if anyone came up with a smarter idea to render times properly in the ui, that does not reqire another template sensor?
3 Likes

I would be interested in this as well, i just got bitten by this wondering why my history stats are off.

Thanks for this! Any chance you could show the code to remove the seconds?

EDIT: Found it.

{{ (timedelta(hours=states('sensor.pool_pump_running_today') | float(0)) | string)[:-3] }}
3 Likes

hi all,

i try to make history state entity to calculate duration of my Gas Burner for whole year using HA manual, but i get duration value in UNIX format 1281.65557704541.

how to transfer result in classic HH:MM format?
my configuration.yaml for this entity:

- platform: history_stats
  name: GasniKotao_godisnji
  entity_id: binary_sensor.q_1
  state: "on"
  type: time
  start: "{{ now().replace(now().year, month=1, day=1, hour=0, minute=0, second=0 ) }}"
  end: "{{ now() }}"

and result:
image