Statistics sensor with condition

I would like to track the average AC delta when the HVAC system is actively heating or cooling. I currently have a sensor with the “heating” when the system is heating and “cooling” when the system is cooling. I also have a ESP handling the delta calculation. I am struggling with how I can create a sensor to take the average of the delta ONLY when the system is “heating” or “cooling”. Any help would be amazing

Probably something like this:


  - sensor:
      - name: Active AC Delta
        device_class: temperature
        state_class: measurement
        unit_of_measurement: "°C" # or F
        state: "{{ states('sensor.your_ac_delta_sensor_here') }}"
        availability: "{{ states('sensor.your_ac_heating_or_cooling_sensor_here') in ['heating', 'cooling'] }}"

This sensor will only have the ac delta value when your AC is heating or cooling. Otherwise it will be unknown.

You can then feed this sensor to the statistics sensor integration to get the average.

You probably want the average_linear state characteristic as this takes into account how long the value has been in a state, where as mean does not. e.g. with mean the value could be 1 all day then in the last five minutes change to 11 and the mean would be 6. Where as average linear would report something like 1.04.