Struggling to get accurate measurements for battery in Energy dashboard

So I am trying to get my battery into the Energy Dashboard. It mostly works, except I get these steps in the graph which don’t represent what’s happening with the battery.
Data is being provided by a Victron inverter over modbus (every 10s seconds)

Starts with the battery power going in and out from the Victron"

    - name: 'Victron Battery Power'
      unit_of_measurement: "W"
      slave: 100
      address: 842
      data_type: int16
      scale: 1.0
      precision: 0
      device_class: power
      scan_interval: 5

Because power can go in and out of the battery, it’s split into 2 sensors (grid also here but unrelated):

  - platform: template
    sensors:
      victron_battery_in:
        friendly_name: 'Victron battery in'
        value_template: >
          {% if states('sensor.victron_battery_power')|float >= 0 %}
            {{ states('sensor.victron_battery_power') }} 
          {% else %}
            0
          {% endif %}        
        unit_of_measurement: 'W'
        device_class: power

      victron_battery_out:
        friendly_name: 'Victron battery out'
        value_template: >
          {% if states('sensor.victron_battery_power')|float < 0 %}
            {{ -1 * states('sensor.victron_battery_power')|float }}
          {% else %}
            0
          {% endif %}       
        unit_of_measurement: 'W'
        device_class: power

      victron_grid_usage:
        friendly_name: 'Grid usage'
        value_template: >
          {% if float(states("sensor.victron_grid_power")) > 0 %}
            {{ (states("sensor.victron_grid_power") | float | round (0) ) }}
          {% else %}
            0
          {% endif %}        
        unit_of_measurement: 'W'
        device_class: power

This then has 2 Riemann sum integral integrations to give the Energy Dash something to read.

  - platform: integration
    source: sensor.victron_battery_in
    name: 'Victron battery in energy'
    unit_prefix: k
    round: 2

  - platform: integration
    source: sensor.victron_battery_out
    name: 'Victron battery out energy'
    unit_prefix: k
    round: 2       

The issue normally happens when the battery is going out of a state of a long discharge, to then charging. IE when the sun comes up after discharging all night.

So in this case that battery was empty, but when it started charging there was this huge step that shows that it discharged a lot of power.

The source doesn’t show that huge amount of power going in/out of the battery

All of this results in an inaccurate graph where it shows that the flat battery discharged 2 times its actually capacity in an hour at 8 am.

Any feedback would be appreciated or some working YAML, as I’m struggling to find a solution.

2 Likes

Without going into the math, I’ve seen many people opt for using method: left on their Riemann integrations.

In case you have an appliance which produces spikey consumption (like an on/off electrical boiler) you should opt for the left method to get accurate readings.

1 Like

Late reply. This helped a lot. I wouldn’t say it’s perfect but good enough.