esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
293 stars 36 forks source link

INA219 component not returning signed power #5732

Open tomlut opened 7 months ago

tomlut commented 7 months ago

The problem

This is a bi-directional device.

A negative current and a positive voltage should produce a negative power (P = I x V).

However the power reported by ESPHome is always positive for the INA219. This makes it impossible to track battery state of charge without a work-around.

Screenshot 2024-04-25 at 00-17-25 History – Home Assistant Screenshot 2024-04-25 at 00-17-52 History – Home Assistant Screenshot 2024-04-25 at 00-17-39 History – Home Assistant

Which version of ESPHome has the issue?

2024.4.1

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

core-2024.4.4

What platform are you using?

ESP32

Board

QuinLED ESP32

Component causing the issue

INA219

Example YAML snippet

sensor:
  - platform: ina219
    address: 0x40
    shunt_resistance: 0.1 ohm
    current:
      name: "Shed Battery Current"
      filters:
        - multiply: -1
        - sliding_window_moving_average:
            window_size: 10
            send_every: 10
            send_first_at: 1
    power:
      name: "Shed Battery Power"
      filters:
        - sliding_window_moving_average:
            window_size: 10
            send_every: 10
            send_first_at: 1
    bus_voltage:
      name: "Shed Battery Voltage"
      filters:
        - sliding_window_moving_average:
            window_size: 10
            send_every: 10
            send_first_at: 1
    shunt_voltage:
      name: "Shed Shunt Voltage"
      filters:
        - sliding_window_moving_average:
            window_size: 10
            send_every: 10
            send_first_at: 1
    max_voltage: 15.0V
    max_current: 3A
    update_interval: 6s

Anything in the logs that might be useful for us?

No response

Additional information

No response

ssieb commented 7 months ago

What value do you get when it should be negative?

tomlut commented 7 months ago

You can see it in the history graphs I posted. It is value|abs. i.e. the positive magnitude.

tomlut commented 7 months ago

Look at the values from midnight to 7am. The power is positive, but the current is negative. Voltage is always positive.

tomlut commented 7 months ago

Currently working around this issue with the following config:


sensor:
  - platform: ina219
    address: 0x40
    shunt_resistance: 0.1 ohm
    current:
      name: "Shed Battery Current"
      id: battery_current
      filters:
        - multiply: -1
        - sliding_window_moving_average:
            window_size: 10
            send_every: 10
            send_first_at: 1
      on_value:
        then:
          - component.update: battery_power
    # power:
    #   name: "Shed Battery Power"
    #   filters:
    #     - sliding_window_moving_average:
    #         window_size: 10
    #         send_every: 10
    #         send_first_at: 1
    bus_voltage:
      name: "Shed Battery Voltage"
      id: battery_voltage
      filters:
        - sliding_window_moving_average:
            window_size: 10
            send_every: 10
            send_first_at: 1
      on_value:
        then:
          - component.update: battery_power
    max_voltage: 15.0V
    max_current: 3A
    update_interval: 6s

  - platform: template
    name: "Shed Battery Power"
    id: battery_power
    unit_of_measurement: 'W'
    state_class: measurement
    device_class: power
    lambda: |-
      if ( (id(battery_current).state) && (id(battery_voltage).state) ){
        return (id(battery_current).state * id(battery_voltage).state);
      } else {
        return NAN;
      }
tomlut commented 6 months ago

And the INA260 does the same thing. Positive power no matter if the current is positive or negative.