How do I enable statistics for a Compensation sensor?

Hello all,

I have a compensation sensor translating voltage to litres for a water tank level sensor. This is working great, but I would like to get long term graphs to display on my dashboards using statistics.

After a bit of searching I found this Github post talking about it : [Compensation integration not supported in statistics graph · Issue #96207 · home-assistant/core · GitHub]

It sounds like it is a feature request to make it work natively, but one of the workarounds mentioned was “The integration isn’t designed to work with it yet. You can add state_class: measurement and a device_class: measurement via customize.yaml to get it to work with statistics.

I don’t quite understand what that means. I am taking that I need to reference the Compensation sensor I have created in my configuration.yaml in the customize.yaml so that it has the device_class and state_class properties of the original voltage sensor? But I am really not sure how to do that.

Anyone else done this and could point me in the right direction? Thank you in advance!

Configuration.yaml, compensation sensor :

compensation:
  water_tank_volume:
    source: sensor.house_water_tank_tank_water_level_voltage
    unique_id: water_tank_level_in_litres2
    unit_of_measurement: litres
    precision: 4
    lower_limit: true
#    upper_limit: true
    data_points:
    - [0, 0]
    - [7.71, 27162]

Voltage Sensor from ESPHome :

  - platform: adc
    pin: GPIO32
    name: "Tank Water Level Voltage"
    update_interval: 1s
    unit_of_measurement: V
    accuracy_decimals: 2
    attenuation: auto
    filters:
      - median:
          window_size: 120
          send_every: 120
          send_first_at: 1
      - lambda: return x*(30000+7500)/7500;

Why are you creating another sensor in Home Assistant, do you need both sensors?

If not, you can apply the compensation directly to the ADC in ESPHome. Use the calibrate linear filter and change the ADC unit to litres.

If you do want both voltage and litres you can add the required state_class: measurement using manual customisation, see the example here: https://www.home-assistant.io/integrations/homeassistant/#manual-customization and note that customize: goes under homeassistant: in your configuration.yaml file.

Or you can create a template sensor in ESPHome.

Hi, thanks for your response.

I am keen to keep both values in Home Assistant. Now that you mention a template sensor, I wonder if that is a simpler better option instead of the customization?

I have been playing with customize.yaml and I sucessfully reloaded HA when I had :

sensor.house_water_tank_tank_water_level_voltage:
    device_class:   measurement
    state_class:    measurement  

But I get an error when I try to add the classes to :

compensation sensor.house_water_tank_tank_water_level_voltage:
    device_class:   measurement
    state_class:    measurement  

Assuming I am supposed to do that to allow the compensation sensor to appear as an entity when added a statistics graph?

homeassistant:
  customize:
    sensor.house_water_tank_volume:
      device_class:   measurement
      state_class:    measurement  

Thank you

    sensor.water_tank_volume:
        device_class:   measurement
        state_class:    measurement

This allowed me to reload successfully and I see what I needed to be referencing from the compensation configuration now. The Compensation area “created” a new sensor, with the name “water_tank_volume”, and that is what I needed to add a device_class and state_class to.

I do not see “sensor.water_tank_volume” as an entity in the statistics graph yet. I am assuming that is because it now needs time to build statistics, since I have just created it?

Sorry, I just copied and pasted what you had. It should be:

    sensor.water_tank_volume:
      device_class: volume
      state_class: measurement

Once you have changed that and restarted, check to see that there are no FIX ISSUE buttons in Developer Tools → Statistics.

No need to be sorry! This help is very much appreciated.

No luck with that either unfortunately, I have done full system restarts to make sure. I am on 2025.2 Core version and everything is up to date.

I will do a bit more playing and looking for samples. This topic seems to be quite light on info on old uncle Google.

Check Settings → System → Logs

sensor.house_water_tank_tank_water_level_voltage state is not numerical

So I tried to set the attribute under the compensation sensor setup and I am a bit lost. I tried the source entity as the attribute “house_water_tank_tank_water_level_voltage” but got the same error.

Compensation sensor, with added attribute line. But really confused as to what value need to be on that line.

compensation:
  water_tank_volume:
    source: sensor.house_water_tank_tank_water_level_voltage
    unique_id: water_tank_level_in_litres2
    unit_of_measurement: litres
    attribute: house_water_tank_tank_water_level_voltage
    precision: 4
    lower_limit: true
#    upper_limit: true
    data_points:
    - [0, 0]
    - [7.71, 27162]

I am definitely learning more working through this.

I have been into Dev Tools - States and looked at the Compensation sensors in there. All show numeric values. So I am still quite confused as to why I am getting this error.

I have tried to set the attribute, as in my above reply, but every time I do that, it breaks the compensation sensor on my dashboards.

State of the compensation sensor:

State of the source voltage sensor:

Not 100% sure of this, but it looks like the properties I am trying to set in the customize.yaml are not applying?

Remove the attribute option from your compensation sensor. it is not needed or correct.

Why do you have two compensations sensors?

Change your manual customisation to use the correct entity id it will be one of the two ids in that screen shot. I have no idea which one.

homeassistant:
  customize:
    sensor.house_water_tank_volume:  # <- change this it is not the correct entity id
      device_class:   measurement
      state_class:    measurement  

Then restart Home Assistant. No need to reboot.

1 Like

That got me sorted I believe, it appears under statistics graph, just waiting for the data to populate.

I have two compensation sensors, one converts the voltage from the sensor to litres in the tank the other converts it to a percentage.

My naming for those sensors are terrible, the by product of me learning as I go with the sensors and building it in ESPHome. I haven’t been game enough to change the names to more logical versions as I do not want to break the tank sensor at the moment. I will do it in the future though as it is way too confusing and I am sure it has cost me time here.

Really appreciate your assistance, I have learnt a lot working through this, it all makes a lot more sense now!