Need some help with history_stats, get different results

Hi all,

Following configuration are for two daily sensors; one is for the last 24 hours, the second are since 10 pm last night. So, for last night, the actual time in bed was 5,88h, but first sensor says 14,1h.
Why are the first sensor showing wrong value, what have I not understood correct abt the history_stats sensor?

  - platform: history_stats
    name: Seng - Siste døgn
    entity_id: binary_sensor.0x00158d0001956a4f_contact
    state: 'off'
    type: time
    start: '{{ as_timestamp( now().replace(hour=0, minute=0, second=0) ) - 86400 }}'
    end: '{{ now() }}'

  - platform: history_stats
    name: Seng - Siden kl 22 i går kveld
    entity_id: binary_sensor.0x00158d0001956a4f_contact
    state: 'off'
    type: time
    end: '{{ now().replace(hour=22, minute=0, second=0) }}'
    duration:
      hours: 24

The two next shows last 7 and 30 days. Have in mind, it is still not 7 days since I started with this so the values should be identical, but are not. Last 7 days show 45h and last 30 days show 46,33h. Why?

  - platform: history_stats
    name: Seng - Siste 7 dager
    entity_id: binary_sensor.0x00158d0001956a4f_contact
    state: 'off'
    type: time
    end: '{{ now().replace(hour=22, minute=0, second=0) }}'
    duration:
      days: 7

  - platform: history_stats
    name: Seng - Siste 30 dager
    entity_id: binary_sensor.0x00158d0001956a4f_contact
    state: 'off'
    type: time
    end: '{{ now().replace(hour=0, minute=0, second=0) }}'
    duration:
      days: 30

image

Does not matter. The component queries the database for stats.

I cant help with the 24 hour sensor problem but for the 30 days sensor, do you actually store 30 days worth of data in the recorder?

If the duration exceeds the number of days of history stored by the recorder component ( purge_keep_days ), the history statistics sensor will not have all the information it needs to look at the entire duration. For example, if purge_keep_days is set to 7, a history statistics sensor with a duration of 30 days will only report a value based on the last 7 days of history.

Yes I do store 30 days.
Even so, I have only used the sensor since last weekend, so we are talking 5 days so far. This means the 7 and 30 day sensor should show identical per today.

No we’re not. The component queries the database for stats. So it does not matter that you only started this 5 days ago. If you have recorder data that extends back 30 days the hystory_stats sensor will pull that from the database.

@tom_l, I’ll be more clear, sorry :slight_smile:
I added the sensor the same day, so the sensor has only been there for 5 days.

The first two sensors in question 1 show the mismatch also, and those are only for the last 24 hrs.
I must be doing something wrong with the start & end settings…

Ah you mean you added the binary_sensor.0x00158d0001956a4f_contact sensor, right, sorry thought you meant the history_stats sensor.

Correct.

I’ve just used the new Logbook, could easily get the stats.
I added the sensor on June 23rd, but it has only been closed for seconds randomly for testing up until I started using it in the bed June 25th. So I guess you gonna tell me now “Hah, I told you!” :wink: .
I do hope you trust me when I tell you I’ve looked at this throughout the past week and they’ve had mismatch every day after June 25th.

Your first two sensor time ranges are not equivalent.

First:
Start: yesterday at midnight (00:00)
End: now

Second:
Start: 10pm yesterday
End: 10pm today (Not sure what happens if this is in the future).

These ones don’t finish at the same time:

The 7 day sensor:
Start: 10pm 7 days ago
End: 10pm today (Not sure what happens if this is in the future).

The 30 day sensor:
Start: midnight 30 days ago
End: midnight this morning

So, the reason for 10 pm is that if I should be so lame that I go to bed before midnight, it should be part of the calculation. I don’t sleep in the afternoon’s etc.

So, if we do the last 24 hrs; last night I went to bed at 02:30 am and got out of bed at 08:05 am.
Those two sensors should then state the same, or ??

As long as you looked at the sensor after 08:05 and as long as setting the end time in the future for the second sensor doesn’t do weird things, yes theoretically they should be the same.

1 Like

Actually no. The first sensor starts yesterday at midnight. Not 00:00 today. So you get two nights worth of sleep time.

Agree, sadly they aren’t, thus my cry for help …
There is probably something with (perhaps as you say, the future) the configuration.

But as you see from the image I pasted in the original post, it’s almost as the first one includes two days…

'{{ as_timestamp( now().replace(hour=0, minute=0, second=0) ) - 86400 }}'

So, as_timestamp are now, minus 86400 seconds should be last 24 hrs.
Perhaps I misunderstanding this part?
What is 00:00:00 - is it yesterday or today? If it is yesterday and I subtract 1 day it actually gives you two days… Hmmm… Perhaps I will set it to +1 second as at least that is today…

I changed my mind. See my second post above.

The day starts at 00:00. So 00:00 yesterday to after 8am today gets you two nights worth of sleep time.

I suggest you draw out a time line of a few days, each day starts at 00:00 and ends at 23:59:59. Then plot what you actually want to measure on that timeline.

Hi There,
I wanted to know the time how long a sensor is on. Curious, it just shows me when the sensor was on, so starting time and end time but NOT how long. How to solve this? Any hints, thank you.
see my code below - JJ

- platform: history_stats
  name: "TV Wohnen - täglich"
  entity_id: media_player.tv_wohnen
  state: 'on'
  type: time
  start: '{{ now().replace(hour=12).replace(minute=0).replace(second=0) }}'
  end: "{{ now() }}"

- platform: history_stats
  name: "TV Wohnen - 7 Tage"
  entity_id: media_player.tv_wohnen
  state: "on"
  type: count
  start: "{{ now().replace(hour=12, minute=0, second=0) }}"
  duration:
    days: 7