History_stats period calculation problem

i created item history_stats:

- platform: history_stats
    name: PowerUp
    entity_id: binary_sensor.power_up
    state: "on"
    type: time
    start: "{{ now().replace(hour=20, minute=0, second=0) }}"
    duration: 12:00

but the timing stops at 00:00
where is my error?
I need to count the period from 20:00 to 8:00
binary_sensor always on

Can’t anyone help?

You have to adjust your start time. now() always returns the current day. So when midnight hits, it’s now set to today at hour 20. So you should subtract 1 day from the start time if the current time is less than the start time.

  - platform: history_stats
    name: PowerUp
    entity_id: binary_sensor.power_up
    state: "on"
    type: time
    start: >
      {% set t = now() %}
      {% set start = t.replace(hour=20, minute=0, second=0) %}
      {% if start > t %}
        {{ start - timedelta(days=1) }}
      {% else %}
        {{ start }}
      {% endif %}
    duration: 12:00

Also, please be patient for responses. Requesting help 12 hours after a post is a little soon. People will respond, it make take a few days.

Thank you very much for your comprehensive answer. From now on I will be more patient.

unfortunately your code does not count the time at all
record 12:00 is 12 minutes
record 12:00:00 12 hours
error in documentation