esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
420 stars 26 forks source link

Support for power monitor INA228/229 #2549

Closed taHC81 closed 6 months ago

taHC81 commented 10 months ago

Describe the problem you have/What new integration you would like

Would it be possible to add INA228 (I2C) and INA229 (SPI) power monitoring chips? There're already some INAs supported, but these 2 are often used for battery management.

Please describe your use case for this integration and alternatives you've tried:

Battery voltage/current/power/SoC monitoring.

Additional context

taHC81 commented 9 months ago

@taHC81 can you please test negative current.

Basically Charge/discharge cycle

P. S. Vbus shall stay almost positive as per datasheet

Yeah, Vbus is the voltage across the battery terminals. I need to manage some test load. Will run test most probably in upcoming days.

taHC81 commented 9 months ago

https://pastebin.com/ujj7pnXz

Here's how it looks like when I've connected charger and current flowed in an opposite direction. Crazy numbers :)

latonita commented 9 months ago

@taHC81 what are expected negative values? Looks like I didn't manage to convert 'Two's compliment' negative numbers to normal format properly 🤦

taHC81 commented 9 months ago

Load (from a battery) is 0.286A, when I turn on power supply, it draws 0.317A, it can be translated to a charging with 0.031A. Maybe a bit more, as the Vbus is a bit higher (15.58V vs. 15.85V).

Let me discharge that battery a bit, to have a bigger difference.

latonita commented 9 months ago

@taHC81 i pushed the fix. please try

taHC81 commented 9 months ago

No funny numbers anymore, but I just realized it shows positive power & current when I connect the load, and it should be opposite - load = minus power, charging = plus power. My smart battery is acting strangely, it doesn't accept any charge at the moment, will try with non-smart battpack on Monday. Sunday = family.

https://pastebin.com/Mp8WA0yR

latonita commented 9 months ago

@taHC81 good to hear. as for +- .... don't shoot the messenger. i'm just getting data from the device :) you might have on_value triggers in particular sensor to switch polarity if you need have a nice sunday. meanwhile i'll remove debug logs and make PR ready for review

taHC81 commented 9 months ago

Had some spare time, so did a try. Counters reset works fine, energy/charge working fine, but charge overflows, see the video and logs - https://pastebin.com/sW96Ww8s

https://www.youtube.com/watch?v=W_YwNdLt76I

latonita commented 9 months ago

@taHC81 try get new ver, shall be proper negative charge

taHC81 commented 9 months ago

Fixed, good! ;) But I'd recommend to swap the polarity of the current as a default.

latonita commented 9 months ago

@taHC81 this is just what i get from the device. i think it might be changed in particular sensor adding

  charge:
     name: xx
     filter:
        multiply : -1.0
latonita commented 9 months ago

and one ugly thing which is coming - is overflowing charge register. i know its a big number (for your settings it is 11.5 kAh) but... funny is that device counts charge as signed value, but at the same time overflow bit doesnt say is it + overflow or -overflow. :))) I will ask TI forum

audito67 commented 9 months ago

Greetings. I also have ina228 for monitoring the battery of a solar station. At the moment I do not use any processing using HA tools; all data is displayed in its original form. There are fluctuations in the shunt voltage and shunt current in the near-zero zone, but the Ah meter increases the readings. After about an hour of operation of the device, the Ah reading is 0.00003181 Ah, while no real current flows through the shunt resistor. charge: name: xx filter: multiply : -1.0 If you use the solution you suggested, the register values will continue to increase even when the battery is low.

taHC81 commented 9 months ago

Greetings. I also have ina228 for monitoring the battery of a solar station. At the moment I do not use any processing using HA tools; all data is displayed in its original form. There are fluctuations in the shunt voltage and shunt current in the near-zero zone, but the Ah meter increases the readings. After about an hour of operation of the device, the Ah reading is 0.00003181 Ah, while no real current flows through the shunt resistor. charge: name: xx filter: multiply : -1.0 If you use the solution you suggested, the register values will continue to increase even when the battery is low.

It's a common noise, you have either live with that or filter out values under f.i. 0.005A and integrate energy by yourself. Even the sub-sized PV plant uses 100+ Ah batteries, so considering 1e-5 Ah values is a bit odd to me.

audito67 commented 9 months ago

I understand perfectly well that this is an insignificant value and can be neglected, the point is that this noise accumulates in the register, which logically should not happen. And filtering can be done using the chip, as mentioned in the datasheet.

latonita commented 9 months ago

I may change integration time and turn on averaging For now it's 50us, changing to even max 4000us should not harm anyone (4 ms) I will take a look later today

audito67 commented 9 months ago

Было немного свободного времени, поэтому попробовал. Сброс счетчиков работает нормально, энергия / заряд работает нормально, но переполнение заряда, см. Видео и журналы - https://pastebin.com/sW96Ww8s

https://www.youtube.com/watch?v=W_YwNdLt76I

The question is a little off topic, sorry. How did you implement resetting data in registers on your HA? If possible, a piece of Yaml code on a third-party resource.

latonita commented 9 months ago

BTW, as per datasheet energy/charge error is 1%

taHC81 commented 9 months ago

The question is a little off topic, sorry. How did you implement resetting data in registers on your HA? If possible, a piece of Yaml code on a third-party resource.

button:
  - platform: template
    name: "Reset counters"
    on_press:
      - lambda: "id(ina228_sensor).reset_energy_counters();"
audito67 commented 9 months ago

I may change integration time and turn on averaging For now it's 50us, changing to even max 4000us should not harm anyone (4 ms) I will take a look later today

The integration time seemed to be 1052 µs, and the averaging was “1”, or am I mistaken? By and large, whether 1ms or 4ms is not a big difference, greater accuracy is achieved (according to the datasheet) by averaging the measurements.

latonita commented 9 months ago

By and large, whether 1ms or 4ms is not a big difference, greater accuracy is achieved (according to the datasheet) by averaging the measurements.

I've made 4096us and 128 samples. which all in all give us ~0.5s for a sample. good enough for australia, montenegro, esphome and hass and home automation, i believe. and it gives 19.7 effective resolution bits

I might make it configurable if required. the longest exposure that can be achieved is ~ 4s for the sample. which gives full 20 effective bit adc

please take a look, whether this brings value. then i might add configuration options

taHC81 commented 9 months ago

Good one ;) will check tomorrow. Thanks for your work!

audito67 commented 9 months ago

Greetings. Everything is just super, it works perfectly. I checked the accuracy of the readings with a calibrated constant voltage source, the readings fluctuated to 4 decimal places. The shunt voltage remains stable in the area of its own noise and also in the 4th decimal place. There are no negative shunt current and voltage values. Wonderful. Thanks for your work!

audito67 commented 9 months ago

I checked it on esp8266, later I will check it on esp32 and with current flow.

taHC81 commented 9 months ago

Finally had an opportunity to check, looks good now, I have approx -0.006A reading with no load, I think it's acceptable on a 300A shunt :)

Thanks for you work, I'd say it's ready to become public. 20240201_182019

audito67 commented 9 months ago

Finally had an opportunity to check, looks good now, I have approx -0.006A reading with no load, I think it's acceptable on a 300A shunt :)

Thanks for you work, I'd say it's ready to become public. 20240201_182019

I confirm that this is practically the only project that allows you to use the functionality of the energy monitor on ina228 with minimal effort.

audito67 commented 9 months ago

I might make it configurable if required. the longest exposure that can be achieved is ~ 4s for the sample. which gives full 20 effective bit adc

In the future, do you plan to make the conversion times and averaging parameters customizable? I think the ability for the user to independently configure these parameters would not be superfluous.

latonita commented 9 months ago

In the future, do you plan to make the conversion times and averaging parameters customizable? @audito67 done.

audito67 commented 9 months ago

Is there a desire to create a project with similar functionality (within the capabilities of the chips) for ina226? The existing integration into esphome uses a minimum of functionality compared to the functionality in your project.

taHC81 commented 9 months ago

Is there a desire to create a project with similar functionality (within the capabilities of the chips) for ina226? The existing integration into esphome uses a minimum of functionality compared to the functionality in your project.

What do you expect? INA226 doesn't provide charge/energy registers. Yes, conversion time and averaging shall be added, question is, if there is a real need for this.

audito67 commented 9 months ago

What do you expect? INA226 doesn't provide charge/energy registers. Yes, conversion time and averaging shall be added, question is, if there is a real need for this.

Yes, I am well aware of the capabilities of the INA226, and I designated it “within the capabilities of the chips.” Recently I encountered an unpleasant moment when switching from the alternative firmware esp8266+ina 226 to integration from esphome. There were significant (1.7a) current readings with no current flowing through the shunt resistor. The values written to the ina226 registers are preserved and there is no way to reset them in the esphome platform. Whether there is a need for the user to change the measurement and averaging settings is a matter of experimentation, but working with ina228 showed positive results.

latonita commented 9 months ago

@audito67 actually I see a bug in INA226 - it doesnt work with negative values, instead you will get huge readings. If you still own it it would be great to test. if it works okay - please create error report and i will link it. https://github.com/esphome/esphome/pull/6172

taHC81 commented 9 months ago

I've got some 226's on the way home, should come this week.

audito67 commented 9 months ago

Good afternoon Don't quite understand what I need to do? Perhaps because I use a translator. I can connect a new one, not used on the alternative firmware ina226, and carry out the necessary tests, but when I add the stack code “adc_time: 1100 us” “adc_averaging: 1024” to the working code generated from the example with esphome, the error “[adc_time] is an invalid” appears option for [sensor.ina226]. Please check the indentation." I assume the external component does not contain these changes and probably needs a link to the external component.

audito67 commented 9 months ago

2024-02-05_12-08-34

latonita commented 9 months ago

@audito67

external_components:
  - source: github://pr#6172
    components: [ ina226 ]
audito67 commented 9 months ago

Should the test be carried out on a completely new one or can it be used previously?

audito67 commented 9 months ago

The test was carried out on the previously used ina226. The readings are displayed correctly when measuring current in both directions; there is no deviation in the current and voltage readings. Everything is great.

audito67 commented 9 months ago

Judging by the log, the voltage and current readings are stable in the fourth decimal place even with a measurement time of 332 µs, the number of measurements before averaging is 1024.

latonita commented 9 months ago

@audito67 can you compare it to old "official" ina226? "есть возможность проверить старую официальную версию ina226, без моих изменений? отрицательные и положительные значения тока?"

audito67 commented 9 months ago

Yes, sure. I'll check tomorrow.

audito67 commented 9 months ago

In both cases, the display is correct, both positive and negative values. Just in case, I am attaching the logs, with the old version marked old and, accordingly, your project marked new. logs_ina226_run_new.txt logs_ina226_logs_old.txt logs_ina226_run_old.txt I cleared the cache in HA, but the identical behavior of the device under test is confusing. If necessary, I will conduct more tests. Suggestion: To understand which version we are working with, you can put 3 decimal places when measuring VBUS in the new version and it will immediately be clear which version without viewing the logs.

audito67 commented 8 months ago

Good afternoon. I discovered two bugs, the first concerns the operation of ina228. The energy meter increases its reading regardless of whether the battery is charging or discharging. The second unpleasant discovery is rebooting the esp8266 and resetting the data accumulated in the ina228 when the connection with the home assistant server is lost. The survey was organized on esp8266 by ina228 and BMS Jikong. The BMS survey is conducted using the syssi project. By and large, no matter what project is used, the contents of the registers should not be reset when the esp is rebooted. Otherwise, these power and energy calculations are useless.

taHC81 commented 8 months ago

Good afternoon. I discovered two bugs, the first concerns the operation of ina228. The energy meter increases its reading regardless of whether the battery is charging or discharging. The second unpleasant discovery is rebooting the esp8266 and resetting the data accumulated in the ina228 when the connection with the home assistant server is lost. The survey was organized on esp8266 by ina228 and BMS Jikong. The BMS survey is conducted using the syssi project. By and large, no matter what project is used, the contents of the registers should not be reset when the esp is rebooted. Otherwise, these power and energy calculations are useless.

Energy is always positive value (https://e2e.ti.com/support/amplifiers-group/amplifiers/f/amplifiers-forum/1028806/ina228-q1-current-energy-polarity), I'd suggest to calculate discharge=energy-charge.

For the loosing the cumulative data, I'd suggest to create utility meters on a HA side to cover it.

audito67 commented 8 months ago

Thanks for the clarification, I reviewed the datasheet and realized that I was in a hurry to send the message. It turns out there is only one parameter useful for use in a solar station, this is the calculation of Ah at the input/output of the battery. I agree, energy can be calculated using HA. I don’t quite understand how using utility meters will save data when resetting registers in ine228? With the same success, you can use the history of this sensor, but provided that the loss of communication was short-term. If there is no data transmission for a long time, then you will not receive correct data in any case. In my opinion, I could be wrong; you can save the data by disabling the reset function when you start the ESP and do a reset at the user’s request or by turning off the power.

taHC81 commented 8 months ago

Issues on an ESP side are another story. Utility meter always increase value, even if the source sensor resets to zero. See the manual - https://www.home-assistant.io/integrations/utility_meter/

latonita commented 8 months ago

I think what's is doable is to define two template sensors in esphome - one for charging, another for discharging, based on current sign. And create two utility meters in hass for energy consumed from the grid and generated

BR, Anton


From: taHC81 @.> Sent: Tuesday, February 27, 2024 4:48:58 PM To: esphome/feature-requests @.> Cc: Anton Viktorov @.>; Comment @.> Subject: Re: [esphome/feature-requests] Support for power monitor INA228/229 (Issue #2549)

Issues on an ESP side are another story. Utility meter always increase value, even if the source sensor resets to zero. See the manual - https://www.home-assistant.io/integrations/utility_meter/

— Reply to this email directly, view it on GitHubhttps://github.com/esphome/feature-requests/issues/2549#issuecomment-1966864577, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AA6O66XSC2DQV7X2WL2XM2LYVX56VAVCNFSM6AAAAABBPOVKTCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNRWHA3DINJXG4. You are receiving this because you commented.Message ID: @.***>

audito67 commented 8 months ago

I have two DDS238 smart meters calculate how much is consumed from the network or transferred to the load. The amount of energy received from the solar panels is calculated by the inverter, although not entirely accurately, but this suits me, since there is no sale to the network. The main purpose of using INA228 is to calculate the filled/consumed energy from batteries and, as additional information, control battery degradation. Energy calculation is easy to implement by multiplying the battery capacity by its voltage, there is no question here. I initially thought that the INA228 counts and stores data in a register, but after studying the datasheet in detail, I realized that I was wrong. Before using INA228, I determined the amount of energy in the battery using BMS, but depending on the version of the BMS software, the accuracy of the calculated capacity varies greatly. As a consequence, this parameter cannot be used unconditionally. INA228 allows for more accurate counting, but the issue of spontaneous, unauthorized data dumping arises. I completely agree with you that counting can be done using HA, provided that the pause between the data before and after the reset is not long. But if there is a pause in data transmission for, for example, 1 hour and at that time the battery received a charge with a current of 20-30A, then the data on this energy will be lost. In my opinion, there are two options for solving this problem: 1. this is to perform an authorized reset, without being tied to the operation of the ESP firmware. The operating algorithm requires that each time the firmware is loaded, registers are configured and data is reset. If you configure without clearing the Charge register, the data will be saved in it, provided that the supply voltage is available. 2. This is to write data locally into non-volatile memory, but this is a separate energy monitoring project and an independent device similar to Victron Smart Shunt. I apologize for such a lengthy message, I was expressing my thoughts.

BR, Valery

audito67 commented 8 months ago

Over time, I will check the stability of operation together with the ESP32; at the moment, data is collected from an active device installed on the battery. photo_2024-02-28_19-38-09

audito67 commented 8 months ago

How do you like the idea of implementing a project with eeprom? I, in turn, am ready to implement and test in hardware, there are many different eeprom memory chips, if necessary, you can order the required one. If you write once a minute, the maximum error will be a few percent, and the operating time of such memory will be 6 years, provided that you write to the same memory cells and the number of rewrite cycles is 100,000. If you periodically change the memory area for recording data, the resource will increase factor of.