History Statistic Sensor reports negative values

Hi,

I’ want to track the time I watched TV per day. As I often use to watch TV longer than midnight I want to track the time between 03:00 am - 03:00 am (next day).
I’ve set it up like this:

- platform: history_stats
  name: TV ON today
  entity_id: switch.remote
  state: 'on'
  type: time
  start: '{{ now().replace(hour=3).replace(minute=0).replace(second=0) }}'
  duration:
      hours: 24

Now, every hour I watch after midnight gets reported as a negative value, and therefor reports a wrong value in total. This is how my graph looks like:

Example:
TV on from 09:00pm - 02:00am is beeing calculated as 3h (from 09:00 til midnight) -2h (from midnight til 02:00) =1h

Any help would be greatly appreciated.

Thanks,
Kraken

I think the problem is that after midnight the start is in the future.
Have you tried it with the “Yesterday” example?

    end: '{{ now().replace(hour=3).replace(minute=0).replace(second=0) }}'
    duration:
      hours: 24

Sounds good. I will give it a try and report back.
Thanks!

It seems to work, no more negative values in my sensor data. :+1:
Thank you!