Avg. Time Calculation help

OK so with plenty of help from the community, I setup two history_stats sensors for my cooling system. I wanted to display on a card the total runtime for the day, and the number of times the system kicked on. I display this on my Dashboard and can monitor it throughout the day. Works great.

Now what I want is to display on a card, the Average Runtime per cycle for the PRIOR day which ultimately, I can then hopefully derive a trend from.

My history_stats sensors reset each day using today_at() and a Duration of 24 hours.

sensor.heat_pump_runtime_today_cooling (unit of measure = Hours)
sensor.heat_pump_times_on_today

So if I trigger an automation at say 23:59 to calculate the average runtime per cycle for the day just about to end, how would I do that?

Example: if at 23:59, the system ran a total of 5 hours (300 minutes) and cycled on 10 times, that’s obviously 30 minutes per cycle. I want to display that on my Dashboard…

//Brew

While you can use an automation setting the value to a helper to accomplish your goal, a trigger-based Template sensor is a more appropriate tool.

template:
  - trigger:
      - trigger: time
        at: "23:59:00"
    sensor:
      - name: Heat Pump Avg Cooling Cycle Length
        unit_of_measurement: minutes
        state: |
          {% set runtime =  states('sensor.heat_pump_runtime_today_cooling') | float(0) %}
          {% set count = states('sensor.heat_pump_times_on_today') | int(0) %}
          {{ 0 if count == 0 else ((runtime * 60) / count) | round(0) }}

Be aware that the advanced functions of template sensors, like triggers, are only available through editing the YAML configuration; they are not available through the Template Sensor Helper.

Exactly what I needed! Thank you so much!