Help with history_stats sensor

Hello enveryone.
Im trying to config a sensor that count the number of hours my pool purifier in 24hours.
Its important to me that count the hours since 3:00 am to 2:59 am of the next day.
Not the typical 24 hours from 00:00 to 23:59

The typical case (24 hours from 00:00 to 23:59) its easy and the sensor works with this code:

  - platform: history_stats
    name: Depuradora hoy
    entity_id: switch.calle_piscina_depuradora
    state: 'on'
    type: time
    start: '{{ now ().replace(hour=0, minute=0, second=0) }}'
    end: '{{ now() }}'

im trying to change the start and end, but maybe i dont understand exactly how works and cant obtain this sensor from 3:00 am to 2:59 am

can someone help me?

thanks!!!

Try without the end, use duration

start: "{{ now().replace(hour=3, minute=0, second=0, microsecond=0) }}"
duration:
  hours: 24
1 Like

ok i will test with your information.
i tested with duration in this format " duration: “24:00” " and not works.
i will try

thanks!!!

Not works.
At 0:00 the sensor apears unavailable. :frowning:

That’s because before 3:00, the start time is in the future. You’ll have to subtract a day off of it. Something like this (but I haven’t tested it):

start: >
  {% if now().hour > 3 %} 
    {{ now().replace(hour=3, minute=0, second=0, microsecond=0) }}
  {% else %} 
    {{ now().replace(hour=3, minute=0, second=0, microsecond=0) - timedelta(days=1) }}
  {% endif %}
1 Like

this option seems to work.
i need to try other 24 hours, but seems to work!
thanks!!!

depuradora_2

In your graph the sensor is updating at 04:00 because the first line should instead be
{% if now().hour >= 3 %}

oh, its true!!! thanks!!!

This is my time in bed tracker. It starts midday yesterday and ends midday today.

- platform: history_stats

  name: hours slept 0 night ago

  entity_id: binary_sensor.kilimelis_contact

  state: "off"

  type: time

  start: '{{ today_at("12:00") - timedelta(days=1) }}'

  end: '{{ today_at("12:00") - timedelta(days=0) }}'

in your case: 3 am yesterday till 3 am today

  start: '{{ today_at("3:00") - timedelta(days=1) }}'

  end: '{{ today_at("3:00") - timedelta(days=0) }}'
1 Like

That code still suffers from the same root problem: The re-calculation of the time window occurs at midnight (00:00), not at 03:00. You’d still need an if-then statement.

For example:

  • Jan 2 at 01:00 - your code says window is Jan 1 03:00 to Jan 2 03:00. That is what is desired.
  • Jan 2 at 04:00 - your code says window is Jan 1 03:00 to Jan 2 03:00. That is NOT what is desired.

hello.
before the july update, the if solution works perfect.
But i dont know what happend, but not works? do you have problems with history_stats sensors???

I am running 2023.7.1 and I have the following working. Can you paste the code you are using?

  - platform: history_stats
    name: Test History Sensor
    entity_id: input_boolean.test
    state: 'on'
    type: time
    start: >
      {% if now().hour >= 3 %} 
        {{ today_at("03:00") }}
      {% else %} 
        {{ today_at("03:00") - timedelta(days=1) }}
      {% endif %}
    duration:
      hours: 24

Hello.
Im ussing

start: >
  {% if now().hour >= 3 %} 
    {{ now().replace(hour=3, minute=0, second=0, microsecond=0) }}
  {% else %} 
    {{ now().replace(hour=3, minute=0, second=0, microsecond=0) - timedelta(days=1) }}
  {% endif %}

I test your solution

Thanks!!!

There is nothing wrong with that portion of the code; it will work the same as today_at(). You may have an error in the other part of the definition.

You didn’t specify what was not working; if it’s calculating incorrectly it would be helpful to know what it is doing. If it’s not working at all it would be helpful to know what is in the error logs.

Thanks a lot.
Maybe my problem is the recordé, i put some things in the exclude but i dont know what is exactly the problem. Im trying now deleting the excludes.

My problem this days is that apears some incorrect data in the History_stats and after a reboot, apears correct.

I will try to show you some pictures this night, because Im in travel and its dificult to atach

Thanks a lot

Hello again.
I remove the recorder and setup again, because i think the problem is recorder. But one day after, the problem apears again.

I have 2 History_stats.
This is the config:

One its natural day since 0:00 to 24. And other since 3:00 to 3:00
The pool works 2 times per day: since 0:00 to 3:00 and since 14:00 to 17:00

The max time in both sensor will be 6 hours.
This is the pictures of the pool:

This pictures is taken in the morning of the sensor 3 to 3:
Not works, because reset correctly but keep counting when the pool is off

This other sensor is dayly, and works ok.

This is the resume of the 2 sensors:

And in this point i reboot home assistant. And the sensors changes!!
Now the values ar correct, after tbe reboot

I dont understand :frowning:

I also don’t understand.

I am curious if there is some sort of race condition with the reset happening at the same time as the switch changing from on to off.

What happens if you change your sensor to today_at("03:00:05") ?

    start: >
      {% if now() >= today_at("03:00:05") %} 
        {{ today_at("03:00:05") }}
      {% else %} 
        {{ today_at("03:00:05") - timedelta(days=1) }}
      {% endif %}

Ok, lets go with this changes.

I dont understand what is the problem. But is strange. Not fails all the days.
This is not a crĂ­tica sensor in my setup, its interesting to optimize the use of the pool but not critical.
But i have a lot of curious with situation jejeje

Thanks Man!!!

Hello again.
The problem is not only with the sensor from 3 to 3.
I notice today that the problem its the same with the normal “natural day” sensor.
This is the sensor:

The pool works from 0:00 to 3:00:

This pictures its at morning 10:30 aprox: (before reboot of ha). Bad data

And this picture is after I reboot home assistant. Good data

Have you tried my example above? It works for me.