# Total Consumed Current

Good day.

I’m kind of hoping someone can assist me with this, as I can’t seem to find anything related to “total consumed current” on any of my searches. (Essentially a battery capacity meter)

ESPHome does pop up with an autocomplete for “total_consumed_current”, but no matter what I tried, I couldn’t resolve issues in the format in order for it to actually save without errors.

What I am planning to do is build a unit with a ina219 to test a 12v car batteries capacity (as an example), so, here in lies my query.

I’ve also thought of just taking the declared amount of the power, and then dividing the sum by the volts again to get back to amperes total, but I don’t think that would give me an accurate consumption of Ah.

The code below is for one of the ina219’s which is for my micro solar setup, and not the capacity meter, but I was messing around in there to see if I could find a solution to my future problem



# Power/Wattage Sensor
- platform: ina219
i2c_id: bus_a
shunt_resistance: 0.010069 ohm
update_interval: 0.2s

bus_voltage:
id: ina219_1b_bus_voltage
name: "INA219 1B Bus Voltage"
unit_of_measurement: "V"
accuracy_decimals: 3
filters:
- throttle_average: 5s

# Would something along these lines work for total current consumed?
# I probably won't require the power and volts,
# but thought I could leave that in for the future

current:
id: ina219_1b_current
name: "INA219 1B Current"
unit_of_measurement: "A"
accuracy_decimals: 2
filters:
- multiply: 10
- throttle_average: 5s
on_value:
then:
- lambda: |-
static long last_time = 0;
if (last_time == 0) {
last_time = millis();
return;
}
long duration = millis() - last_time;
last_time = millis();
float cur = id(ina219_1b_current).state * 1000.0;
float volt = floor(id(ina219_1b_bus_voltage).state * 100) / 100.0;
float power = volt * cur;
if (volt == 0 || fabs(cur) < 2.0) {
return;
}
float ah = cur * duration / 1000.0 / 3600.0;
id(amp_hour) += ah;
id(work_time) += duration;

power:
id: ina219_1b_power
name: "INA219 1B Power"
unit_of_measurement: "W"
accuracy_decimals: 3
filters:
- multiply: 10
- throttle_average: 5s

shunt_voltage:
id: ina219_1b_shunt_voltage
name: "INA219 1B Shunt Voltage"
unit_of_measurement: "mV"
accuracy_decimals: 2
filters:
- multiply: 10000
- throttle_average: 5s

- platform: total_daily_energy
id: ina219_1b_daily_energy
name: "INA219 1B Daily Energy"
power_id: ina219_1b_power
filters:
- multiply: 0.001
unit_of_measurement: "Wh"
device_class: energy

# Alternative to getting current consumed
# (wrong platform most likely) ???
- platform: total_daily_energy
id: ina219_1b_hourly_current
name: "INA219 1B Hourly Current"
power_id: ina219_1b_current
unit_of_measurement: "Ah"
device_class: current

# Or maybe something in this line???
- platform: template
name: "INA219 1b Hourly Current"
id: ina219_1b_hourly_current
lambda: return id(ina219_1b_current).state * duration ;
accuracy_decimals: 2
unit_of_measurement: Ah
icon: "mdi:flash"
update_interval: 0.2s
filters:
- throttle_average: 5s



Also, if possible, some guidance to automate this in ESPHome. Basically, a trigger on current greater than 0.01A and switch off when a battery gets below12v.

I hope this made Any sense to someone out there, but if not, let me know, and I’ll try and clarify.

I’m sure it’s something I’m just overlooking, and probably ridiculously simple, but it evades me at the moment.

Thank you in advance for any insightful tips or hints.

Ps. Apologies for the long winded explanation and also if there are any formatting errors, as I was doing this on my phone during a power outage

I’m also looking for something similar using INA3221, calculate the available battery capacity and the fully charged battery capacity when I have a cutoff voltage of 9.5V. Even ChatGPT didn’t managed to get it…

I’m not entirely sure what you want to extract from the INA3221 but I just ended up using the following to get the amp hours from my INA219


- platform: total_daily_energy
id: ${sen_id}_amp_hours name: "${sen_name} Amp Hours"
power_id: ${sen_id}_current accuracy_decimals: 3 unit_of_measurement: "mAh" icon: mdi:alpha-p-circle-outline device_class: energy filters: - multiply: 1000 - throttle_average:${avg}



I have been running this now for the last 2 days, and when calculating it, it seems spot on, although I still have the idea that it’s the wrong platform to be using.

depending on your battery (12v/16v etc) just use the absolute maximum and the lowest you would like the battery to register that it is empty and add



- platform: template
id: ${sen_id}_volt_percent name: "${sen_name} Volt Percent"
unit_of_measurement: '%'
update_interval: ${update_time} device_class: battery lambda: |- return ((id(${sen_id}_bus_voltage).state - 11.98) /0.770 * 100.00);
filters:
- lambda: |
if (x < 0) return 0;
else if (x > 100) return 100;
else return (x);
- throttle_average: ${avg}  On mine the absolute lowest I want to have the batteries deplete to is 11,98v and the batteries resting voltage is 12,75v(with no load and after being fully charged and left to sit for a day or two) (Old batteries, so their capacity isn’t what it ought to be). With the second bit of code it just gives you a percentage of the battery, so at 11,98v it shows 0%. Also just added this to see if it would yeild the same results.  - platform: integration id: total_consumed_current_ah name: "Total Consumed Current (Ah)" sensor:${sen_id}_current
unit_of_measurement: "Ah"
time_unit: h
accuracy_decimals: 3

- platform: template
id: total_consumed_current_mah
name: "Total Consumed Current (mAh)"
unit_of_measurement: "mAh"
icon: "mdi:current-ac"
lambda: |-
return id(total_consumed_current_ah).state * 1000;
accuracy_decimals: 3



edit. The above code calculates the +/- side of the current, and resets the “Used/Consumed” current only after power has been removed and restored (not on a daily basis at midnight, as is the case with daily energy variant), which is quite useful in it’s own right, as testing a battery may extend over a few days depending on how light the load is.

Managed to squeeze this out of ChatGPT just in case you were wondering about the source

I’m not sure if this is what you were asking, but that’s what I landed up doing on mine, as after a year, I never got any response, so it must be some type of voodoo I was trying to acquire.