Adding battery to my energy monitoring

I have HASS up and running fine.
I have a Victron Energy system which I have integrated into HA using MQTT. This has enabled me to bring in most of my AC usage and the solar side of things.
I’m however struggling a little to figure out how best to integrate the battery side of things.
The battery and its JK BMS is currently connected to the Victron system via a USB-RS485 and so the Victron system is aware of the battery state, monitors voltage, current, state of charge, etc.
My issue is there’s no directly available MQTT topic that covers battery consumption. My guess is that this can be obtained by using some of the other values available and setting up an integrate function over time.
There is a “Consumedamphours” but that’s only a positive value and I guess reset as it can only track energy used (discharge) but not energy sent to the battery when charging.
There’s also a topic which publishes a string of values:

{
  "value": [
    {
      "active_battery_service": true,
      "bmsstate": 9,
      "current": 0,
      "id": "com.victronenergy.battery.ttyACM0__0",
      "instance": 1,
      "name": "SerialBattery(JKBMS)",
      "power": 0,
      "soc": 98,
      "state": 0,
      "temperature": 19.5,
      "timetogo": 2069052,
      "voltage": 56.22
    }
  ]
}

where I guess the “current” value (currently 0) tracks the current going in and out (positive and negative values).

In the Energy section of HA, it does want separate values for energy in and out - I don’t think it will allow you to use the same topic value twice so we’d need to create the two distinct ones which can then be used?

Here’s what I currently have for yaml in the battery section of my configuration:

   ## BATTERIES

    #SOC
    - state_topic: "victron/N/[My VRM ID]/battery/1/Soc"
      name: "Battery SoC"
      unique_id: 0a614086-d623-40df-bb77-4a031513075f
      icon: mdi:battery
      unit_of_measurement: "%"
      device_class: battery
      value_template: '{{ value_json.value | float(0) | round }}'
      device: {
        identifiers: [
          "Cerbo GX"
        ],
        manufacturer: "Victron Energy",
        model: "Cerbo GX",
        name: "Cerbo GX"}

    #Battery Voltage
    - state_topic: "victron/N/[My VRM ID]/battery/1/Dc/0/Voltage"
      name: "Battery Voltage"
      unique_id: 099449a2-0c57-4bac-9db7-23bb0086db19
      icon: mdi:flash-triangle
      unit_of_measurement: "V"
      device_class: voltage
      value_template: '{{ value_json.value | float(0) | round(1) }}'
      device: {
        identifiers: [
          "Cerbo GX"
        ],
        manufacturer: "Victron Energy",
        model: "Cerbo GX",
        name: "Cerbo GX"}
    
    #Battery Current
    - state_topic: "victron/N/[My VRM ID]/battery/1/Dc/0/Current"
      name: "Battery Current"
      unique_id: 2b2971f1-6cbb-4c7c-aeef-32622fe5d809
      icon: mdi:current-dc
      unit_of_measurement: "A"
      device_class: current
      value_template: '{{ value_json.value | float(0) | round }}'
      device: {
        identifiers: [
          "Cerbo GX"
        ],
        manufacturer: "Victron Energy",
        model: "Cerbo GX",
        name: "Cerbo GX"}

    #Battery Power
    - state_topic: "victron/N/[My VRM ID]/battery/0/Dc/0/Power"
      name: "Battery Power"
      unique_id: 943fcbe8-5600-40a4-bf21-c7d84f042c78
      icon: mdi:generator-stationary
      unit_of_measurement: "W"
      device_class: power
      value_template: '{{ value_json.value | float(0) | round }}'
      device: {
        identifiers: [
          "Cerbo GX"
        ],
        manufacturer: "Victron Energy",
        model: "Cerbo GX",
        name: "Cerbo GX"}

    #Time to go
    - state_topic: "victron/N/[My VRM ID]/battery/1/TimeToGo"
      name: "Time to go"
      unique_id: 1cef3f50-a08c-49e2-afa6-0e83371fb059
      icon: mdi:timer
      value_template: >
        {% if value_json.value is none %}
          For Ever and Ever!
        {% else %}
          {% set total_seconds = value_json.value | int %}
          {% set days = (total_seconds // 86400) %}
          {% set hours = (total_seconds % 86400) // 3600 %}
          {% set minutes = ((total_seconds % 3600) // 60) %}

          {% if days >= 1 %}
            {{ days }} day{{ 's' if days > 1 else '' }} {{ hours }} hour{{ 's' if hours != 1 else '' }}
          {% elif hours >= 1 %}
            {{ hours }} hour{{ 's' if hours != 1 else '' }} {{ minutes }} minute{{ 's' if minutes != 1 else '' }}
          {% else %}
            {{ minutes }} minute{{ 's' if minutes != 1 else '' }}
          {% endif %}
        {% endif %}
      device: {
        identifiers: [
          "Cerbo GX"
        ],
        manufacturer: "Victron Energy",
        model: "Cerbo GX",
        name: "Cerbo GX"}

    #Consumed battery
    - state_topic: "victron/N/[My VRM ID]/system/0/Dc/Battery/ConsumedAmphours"
      name: "Battery Ah consumed"
      unique_id: 2f868034-9283-493a-91da-f50703b470a3
      icon: mdi:battery
      unit_of_measurement: "Ah"
      device_class: energy_storage
      state_class: total_increasing
      device: {
        identifiers: [
          "Cerbo GX"
        ],
        manufacturer: "Victron Energy",
        model: "Cerbo GX",
        name: "Cerbo GX"}

So what would be your suggested way of going about this?

After some further reading and getting my head around, I’ve found out how to do it.
I’ve had to split the positive and negative power levels so that they can then be used in HA which wants two separate values by creating a template:

template:
  - sensor:
    - name: "Victron Positive Battery Power"
      unique_id: 6fcb3db0-8880-4525-a923-bb9ba2d06897
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      state: >
        {% if states('sensor.victron_battery_power')|float >= 0 %}
          {{ states('sensor.victron_battery_power') }}
        {% else %}
          0
        {% endif %}

    - name: "Victron Negative Battery Power"
      unique_id: e0672bf4-2952-4ca4-81c1-5cad5ec8e649
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      state: >
        {% if states('sensor.victron_battery_power')|float < 0 %}
          {{ -1 * states('sensor.victron_battery_power')|float }}
        {% else %}
          0
        {% endif %}

I then created a sensor with the integrate platform to create the energy values over time:

sensor:
  # Battery Integration for battery consuption
  - platform: integration
    source: sensor.victron_positive_battery_power
    name: "Victron Battery In"
    unique_id: 4cb2d0b2-e7dc-4c8f-bb63-b3afdfe8816a
    unit_prefix: k
    round: 2
    max_sub_interval: 1

  - platform: integration
    source: sensor.victron_negative_battery_power
    name: "Victron Battery Out"
    unique_id: 293f6c4b-7451-442d-bd0e-01e537510928
    unit_prefix: k
    round: 2
    max_sub_interval: 1

This has given me both energy values I can then use in an energy dashboard.