Statistics: How to exclude zero values from statistics?

Hi there,
I could a question, which actually sound easy, but confuses me.
I’m trying to use Home Assistant statistics for a signal like this, which show a heat pump’s COP with a stop of the machine at 14:00:

From this time on, the value of the COP is actually not available and therefor drops down to Zero (blue line).
In another test, I’ve changed sensor to not update, if the machine goes to off state and therefore the COP’s value stays at last value (purple line).

Both seem to result in wrong statistics by Home Assistant’s statistics diagram, because the first sensor includes zero values in mean value and the second sensor is including last value for mean value calculation.

Can somebody help, how to properly set up the sensor or explain how to exclude zero values from mean calculation of statistics?

My sensor for reference:

template:
- sensor:
  - name: "Altherma ESP COP Average Raumheizung"
    unique_id: altherma_esp_cop_average_raumheizung
    state: >
      {% if (is_state("sensor.altherma_esp_betriebsart","Raumheizung")) %}
      {{ ( states('sensor.altherma_esp_cop_filter')|float()|round(1) ) }}
      {% else %}
      0
      {% endif %}
      unit_of_measurement: ''
      state_class: measurement

Thanks,
Heiko

When a device is unavailable, the state should not be 0 but litterally unavailable. If this is an integration the developer should fix it. If this is a template sensor, you can fix it yourself, but we need to know more about it.

If the integration developer does not fix it, you could make a template sensor with the right behavior, assuming the value cannot actually be 0 that you need to know about.

I’ve posted my sensor. Maybe you can assist changing it according to your reply?

I cannot test myself, so forgive me if I made a mistake somewhere. Changes I made:

  • The if is gone, that was what gave the problems
  • an availability template was added, to tell HA when the sensor is available/unavailable
  • float shoud have a default these days so I changed it to float(0). That basically does the same as the if you had, but with the availability template, it should never happen.
  • Indentation of last two lines fixed, probably paste error.
- name: "Altherma ESP COP Average Raumheizung"
  unique_id: altherma_esp_cop_average_raumheizung
  availability: "{{ is_number(states('sensor.altherma_esp_cop_filter')) }}"
  state: "{{ ( states('sensor.altherma_esp_cop_filter')|float(0)|round(1) ) }}"
  unit_of_measurement: ''
  state_class: measurement

P.S. This won’t fix earlier 0’s, just avoids them from now on.

This was a great help! “availability” was the right keyword!
Slightly changed availability to the following:

      - name: "Altherma ESP COP Average Raumheizung"
        unique_id: altherma_esp_cop_average_raumheizung_statistik
        availability: "{{ is_state('sensor.altherma_esp_betriebsart','Raumheizung') }}"
        state: "{{ ( states('sensor.altherma_esp_cop_filter')|float(0)|round(1) ) }}"
        unit_of_measurement: ''
        state_class: measurement

This makes sensor available depending on the fact, if certain operation of the heat pump is on.

Greetings,
Heiko

1 Like