Fabian-Schmidt / esphome-victron_ble

Use official Victron BLE endpoint for fetching data from Victron devices via Bluetooth LE via ESPHome.
GNU General Public License v3.0
158 stars 16 forks source link

New sensor state class: total_increasing for compatibility with Home Assistant energy dashboard #3

Closed snipah closed 1 year ago

snipah commented 1 year ago

I think it would be a great idea to add an additional state for some sensors to add compatibility for the Home Assisstant energy dashboard: New sensor state class: total_increasing

Suitable sensors would be yield_today and battery_power if I'm not mistaken.

Fabian-Schmidt commented 1 year ago

I have updated the default value for yield_today to be total_increasing. The only other field which I consider is consumed_ah. Based on my understanding of the docs (links below) is the current measurement value more important than the total value.

If you disagree you can always overwrite the state_class for each sensor in the yaml for any sensor.

snipah commented 1 year ago

Thanks! I think you are right: consumed_ah and yield_today are the most suitable fields.

Is is possible to measure shunt charge and discharge in two sensors? This way we could integrate battery in Home Assistant. https://www.home-assistant.io/docs/energy/battery/

(EDIT) As I'm not a coder, I looked around and found this solution, which should help, correct? https://github.com/KinDR007/VictronSmartShunt-ESPHOME/issues/24

We have "consumed AH" and "battery voltage", this this should be easy to implement :)

  • platform: template name: " ${devicename} charge " filters:
  • multiply: 0.001 unit_of_measurement: kWh device_class: energy state_class: total_increasing accuracy_decimals: ${accuracy} lambda: |- float i = 264 + id(consumed_amp_hours).state; float e = id(bv).state; float w = 0.0; if (i > 0) { w = i * e; } return {w};
Fabian-Schmidt commented 1 year ago

The SmartShunt is already perfoming the integration. If BATTERY_CURRENT is positiv it is charging and when negativ it is discharging. And the CONSUMED_AH and STATE_OF_CHARGE is the result of the integration. It is the state of the battery. Plus the field TIME_TO_GO contains the expected battery runtime based on current consumption (within the settings of the smart shunt you can configure the time between 1-12 minutes to average). So there is no need that HA is doing any integration as the SmartShunt is already doing it with higher precision.

snipah commented 1 year ago

This sounds great, but does not help to fill the fields "Energy going into the battery" and "Energy going out of the battery", as we need kWh.

This would be CONSUMED_AH * STATE_OF_CHARGE in two sensors, correct? One for charge and one for discharge, or am I thinking too complicated ? :)

Fabian-Schmidt commented 1 year ago

The example takes CONSUMED_AH * BATTERY_VOLTAGE = Wh. That is incorrect for batteries as the voltage fluctuates based on the current load and state of charge. Batteries are always measured in Ah and not Wh. I don't know what HA is expecting here.

Fabian-Schmidt commented 1 year ago

I don't have a HA running. Based on the example it could be like this:

  - platform: victron_ble
    victron_ble_id: MySmartShunt
    name: "Battery voltage"
    type: BATTERY_VOLTAGE
    id: shunt_BATTERY_VOLTAGE
  - platform: victron_ble
    victron_ble_id: MySmartShunt
    name: "Consumed Ah"
    type: CONSUMED_AH
    id: shunt_CONSUMED_AH

  - platform: template
    name: "charge"
    lambda: |-
      const auto my_battery_ah = 95.0f;
      const auto ah = id(shunt_CONSUMED_AH).state;
      const auto voltage = id(shunt_BATTERY_VOLTAGE).state;
      if(std::isnan(ah) || std::isnan(voltage)) {
        return NAN;
      }
      const auto remaining_ah = my_battery_ah + ah;
      return remaining_ah * voltage;
  - platform: template
    name: "discharge"
    lambda: |-
      const auto ah = id(shunt_CONSUMED_AH).state;
      const auto voltage = id(shunt_BATTERY_VOLTAGE).state;
      if(std::isnan(ah) || std::isnan(voltage)) {
        return NAN;
      }
      return ah * voltage;

I consider this wrong but if HA wants it.

snipah commented 1 year ago

I get your point and actually never thought about this... anyway: Thanks a lot ;)

snipah commented 1 year ago

Do I have to add

unit_of_measurement: kWh, device_class: energy & state_class: total_increasing for each sensor?

I guess so ;)

Fabian-Schmidt commented 1 year ago

The values are in Wh. So to get divide them by 1000. The sensor settings can be applied here or in HA.

Alternative way could be to take the nominal (fixed) voltage of the battery instead of the actual voltage. So for AGM 12.4V, LiFePo4 12.6V etc. That could yield a better result.

snipah commented 1 year ago

As I said: I'm no coder, but does your "charge"-code not return the remaining capacity of the battery and the discharge-code returns the current discharge in Watts, both as positive values? This differs from the code I pasted above, which simply hands over the current deltas in kWh - either positive or negativive?

Fabian-Schmidt commented 1 year ago

CONSUMED_AH is always a negative number - best case it is 0.0 (Battery is full). So the only difference is that I return a negative value for discharge. So you might need to change return ah * voltage * -1; to get a positive number. Else the two calculations shout be equal. I don't think there is much coding involved here it is a math / physics question.