ESP Home liquid sensor issue

Hi All,

Strange one, I have a liquid sensor in my oil tank that has been working great with the below code for ages. It seems one of the last updates have stopped it calculating the oil volume. Nothing has changed in this code, just new updates to ESP Home and HA. Wonder if anyone can help me find the issue.

The ESP32 Code

sensor:
  - platform: ina219
    address: 0x40
    shunt_resistance: 0.1 ohm
    current:
      name: "INA219 Current"
      id: ina_current
      accuracy_decimals: 5
      filters:
      - multiply: 1000 #convert from Amps to mA
      unit_of_measurement: "mA"
    power:
      name: "INA219 Power"
      accuracy_decimals: 5
    bus_voltage:
      name: "INA219 Bus Voltage"
      accuracy_decimals: 2
    shunt_voltage:
      name: "INA219 Shunt Voltage"
      id: ina219_shunt_voltage
      accuracy_decimals: 5
    max_voltage: 32.0V
    max_current: 400mA
    update_interval: 10s

  - platform: template
    name: "Oil Level"
    id: water_level
    unit_of_measurement: 'm'
    accuracy_decimals: 3
    update_interval: 60s
    lambda: |-
      float shunt_voltage = id(ina219_shunt_voltage).state;
      float full_voltage = 0.00074; // Voltage at full condition
      float empty_voltage = 0.00043; // Voltage at empty condition
      float max_height = 1.250; // Adjust the max height as needed

      // Calculate water level based on calibration
      float slope = (max_height - 0.0) / (full_voltage - empty_voltage);
      float intercept = max_height - slope * full_voltage;
      float water_level = slope * shunt_voltage + intercept;

      // Ensure water level is within bounds
      water_level = water_level < 0.0 ? 0.0 : water_level;
      water_level = water_level > max_height ? max_height : water_level;

      ESP_LOGD("custom", "Shunt Voltage: %.5f V", id(ina219_shunt_voltage).state);
      ESP_LOGD("custom", "Water Level: %.5f m", water_level);
      return water_level;


  - platform: template
    name: "Current Full Percentage"
    id: current_full_percentage
    unit_of_measurement: '%'
    accuracy_decimals: 1
    update_interval: 60s
    lambda: |-
      float max_full_reading = 7.39;  // Adjust the maximum reading at full water level
      float min_empty_reading = 4.303;  // Adjust the minimum reading at empty water level
      float current_reading = id(ina_current).state;
      
      if (current_reading <= min_empty_reading) {
        return 0.0;
      } else if (current_reading >= max_full_reading) {
        return 100.0;
      } else {
        float current_full_percentage = 100 * (current_reading - min_empty_reading) / (max_full_reading - min_empty_reading);
        return current_full_percentage;
      }

  - platform: template
    name: "Tank Volume"
    id: tank_volume
    unit_of_measurement: "litres"
    lambda: |-
      float tank_radius = 0.60;  // Adjust the tank radius as needed
      float max_height = 1.20;   // Adjust the max height as needed
      float total_volume_single_tank = 3.14159265 * tank_radius * tank_radius * max_height * 1000;
      float percentage_full = id(current_full_percentage).state;
      float water_level = max_height * (percentage_full / 100.0);
      float current_volume_single_tank = 3.14159265 * tank_radius * tank_radius * water_level * 1000;
      float total_volume_both_tanks = current_volume_single_tank;
      return total_volume_both_tanks;

  - platform: wifi_signal
    name: "Bothie 4-20 WiFi Signal Sensor"
    update_interval: 60s

Here is the output from the ESP32

[10:22:56][D][sensor:093]: 'INA219 Bus Voltage': Sending state 23.88400 V with 2 decimals of accuracy
[10:22:56][D][sensor:093]: 'INA219 Shunt Voltage': Sending state 0.00075 V with 5 decimals of accuracy
[10:22:56][D][sensor:093]: 'INA219 Current': Sending state 0.00000 mA with 5 decimals of accuracy
[10:22:56][D][sensor:093]: 'INA219 Power': Sending state 0.00000 W with 5 decimals of accuracy
[10:23:06][D][sensor:093]: 'INA219 Bus Voltage': Sending state 23.88000 V with 2 decimals of accuracy
[10:23:06][D][sensor:093]: 'INA219 Shunt Voltage': Sending state 0.00075 V with 5 decimals of accuracy
[10:23:06][D][sensor:093]: 'INA219 Current': Sending state 0.00000 mA with 5 decimals of accuracy
[10:23:06][D][sensor:093]: 'INA219 Power': Sending state 0.00000 W with 5 decimals of accuracy
[10:23:16][D][sensor:093]: 'INA219 Bus Voltage': Sending state 23.88000 V with 2 decimals of accuracy
[10:23:16][D][sensor:093]: 'INA219 Shunt Voltage': Sending state 0.00076 V with 5 decimals of accuracy
[10:23:16][D][sensor:093]: 'INA219 Current': Sending state 0.00000 mA with 5 decimals of accuracy
[10:23:16][D][sensor:093]: 'INA219 Power': Sending state 0.00000 W with 5 decimals of accuracy
[10:23:26][D][sensor:093]: 'INA219 Bus Voltage': Sending state 23.88400 V with 2 decimals of accuracy
[10:23:26][D][sensor:093]: 'INA219 Shunt Voltage': Sending state 0.00071 V with 5 decimals of accuracy
[10:23:26][D][sensor:093]: 'INA219 Current': Sending state 0.00000 mA with 5 decimals of accuracy
[10:23:26][D][sensor:093]: 'INA219 Power': Sending state 0.00000 W with 5 decimals of accuracy
[10:23:29][D][custom:097]: Shunt Voltage: 0.00071 V
[10:23:29][D][custom:098]: Water Level: 1.12903 m
[10:23:29][D][sensor:093]: 'Oil Level': Sending state 1.12903 m with 3 decimals of accuracy
[10:23:32][D][sensor:093]: 'Current Full Percentage': Sending state 0.00000 % with 1 decimals of accuracy
[10:23:36][D][sensor:093]: 'Bothie 4-20 WiFi Signal Sensor': Sending state -72.00000 dBm with 0 decimals of accuracy
[10:23:36][D][sensor:093]: 'INA219 Bus Voltage': Sending state 23.88400 V with 2 decimals of accuracy
[10:23:36][D][sensor:093]: 'INA219 Shunt Voltage': Sending state 0.00071 V with 5 decimals of accuracy
[10:23:36][D][sensor:093]: 'INA219 Current': Sending state 0.00000 mA with 5 decimals of accuracy
[10:23:36][D][sensor:093]: 'INA219 Power': Sending state 0.00000 W with 5 decimals of accuracy

The fuel height is correct at 1.169m so assume is is reading correct.

It seems the problem is the Current Full Percentage is being returned at 0 hence it can’t calculate the volume in liters

More precisely, INA219 Current returns zero…
No idea where the problem is.
But you can do all your calculations as long as you have shunt voltage.

So the code can be changed? Sorry im still new to all this.

I don’t know where the problem is, maybe some bug in your last Esphome update.
In any case, you can rewrite the yaml, you don’t need current to calculate percentage.

Any idea on how to do that? I am new to this and got this from another post

If that water level measurement is correct and full height is 1.25m, it should be as simple as this:

- platform: template
    name: "Current Full Percentage"
    id: current_full_percentage
    unit_of_measurement: '%'
    accuracy_decimals: 1
    update_interval: 60s
    lambda: |-
      float max_height = 1.250; // Adjust the max height as needed
      float measured_height = id(water_level).state; // measured water level
      float current_full_percentage = 100 * measured_height / max_height;
      return current_full_percentage;
      }

Replace your actual current_full_percentage sensor code with this.

Thank you, will try

woohoo it worked

You might want to adjust also the tank volume sensor max_height.