Heating season starting date in template/History Statistics Sensor

I would like to count heater running hours starting every 01 August. Currently I set up the following code for the sensor:

- platform: history_stats
    name: Heater running hours
    entity_id: sensor.input3_state
    state: '1'
    type: time
    start: '{{ now().replace(year=(now().year-1)).replace(month=8).replace(day=1).replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

I understand that it counts now not only this season but also takes last year. I would like to find solution for excluding last year if the current date is in between August-December.

Need your help!

Maybe:

    start: >
      {% set n = now() %}
      {% if n.month < 8 %}
        {% set n = n.replace(year=n.year-1) %}
      {% endif %}
      {{ n.replace(month=8, day=1, hour=0, minute=0, second=0, microsecond=0) }}
1 Like

Thank you very much! I have got your idea and changed the sensor definition. Let’s see…

Would be nice if the negative changes will gone as well.
If you have an idea why there were nagative incriments to the accumulated time… I will pray for you!