History Stats time

So like almost everybody else I am using history_stats too.
To calculate the time my bed is occupied.

    ### daily usage of sleep time @ my side
  - platform: history_stats
    name: sleep time yesterday for makis
    entity_id: binary_sensor.master_bed_occupied
    state: 'on'
    type: time
    end: '{{ now().replace(hour=11, minute=0, second=0) }}'
    duration:
        hours: 15

It does work, but when I look at the state it gives me something that is not time! (7.82)
The hour has 60 minutes, so how the earth it shows 82 ???

What am doing wrong? Can I correct it please ?

Indeed, so 7h and 49.2m
What else would you expect here, figures need to be there for further calculation and 07:49:20 would not be time as well

thank you for your quck reply! You are right because the UI card it gives me exactly this result

εικόνα

What I don’t understand is
a) why it is different ?
b) do I have to convert it to time at my template ? e.g. (82x60/100)

I miss something here and I definetelly can’t see it …

No one answer… it depends on what you want to do with it, for me it serves as the time how much my son spends behind his gaming console and how long certain (often pointless) lights are on. I personally would not like h:mm:ss as it more resembles the clock time…prefer it to be 7h49m… that is just ‘me’ of course

Depending my on what you want to do, the answers would vary, as said above, but one thing I’d do is to set the device class to duration and also a unit of measurement. This will automatically treat the number as a timedelta object which will allow a number of things to work automatically. One thing I’ve observed though, if you plot it (with the built-in cards), it won’t use a nicely formatted time on the y axis, but the decimal value instead.

How to set device class and unit of measurement to History stats ??
It is not supported

Not directly on the sensor definition, no, but you can set it in customize.yaml or via the UI.

1 Like