Template sensor kWh value jumps unexpectedly high

Hello everyone,

i have recently connected a PV storage system to a Victron Multiplus II 3000 and unfortunately I have problems with the charging and discharging entities that I “calculate” via the HACS Victron integration
The integration provides the sensor “sensor.victron_system_battery_power” which outputs the charging and discharging in watts. Negative value = discharge positive value = charging of the battery.

In the configuration.yaml I separate this entity into a charging and discharging entity:

template:
- sensor:
- name: "Battery power victron input"
unit_of_measurement: "W"
state: >

{% if states('sensor.victron_system_battery_power')|float >= 0 %}

{{ states('sensor.victron_system_battery_power') }}
{% else %}
0
{% endif %}

- name: "Battery power victron output"
unit_of_measurement: "W"
state: >
{% if states('sensor.victron_system_battery_power')|float < 0 %}
{{ -1 * states('sensor.victron_system_battery_power')|float }}
{% else %}
0
{% endif %}

These two sensors also give me a watt value.
These “watt sensors” are in turn converted into kWh with the following code.
sensor:

- platform: integration
source: sensor.battery_power_victron_output
name: Victron Battery Output Riemann
unit_prefix: k
round: 3
- platform: integration
source: sensor.battery_power_victron_input
name: Victron Battery Input Riemann
unit_prefix: k
round: 3

image

image

As you can see on the screens, these sensors make a small “jump” when writing new values to “Input” and a jump of approx. 3kWh when discharging. This completely messes up my statistics.

I may have a consumption of 1500 watts for a short time, but never so much and for so long that the jumps of 3kwh can be justified.

Where have I made a mistake in the config? Does anyone have any other ideas on how I can track the charging and discharging of the battery?

Thank you very much

it’s because your source entities are going unavailable and you’re using a default of 0 with float. When float can’t convert the value it receives from the states() function, it will use the default. When it then recieves a value that can be converted to a float, you end up with a state change that is 0 → large value. And that’s added to your energy readings.

integrate an availability template to avoid this.

availability: "{{ states('sensor.victron_system_battery_power') | is_number }}"
1 Like

Hey, it dosen’t work :frowning:

here new Screens from today:


image

I’ve add your code in my configuration.yaml

      - name: "Battery power victron input"
        unit_of_measurement: "W"
        availability: "{{ states('sensor.victron_system_battery_power') | is_number }}"
        state: >
          {% if states('sensor.victron_system_battery_power')|float >= 0 %}
            {{ states('sensor.victron_system_battery_power') }}
          {% else %}
            {{ 0 }}
          {% endif %}

      - name: "Battery power victron output"
        unit_of_measurement: "W"
        availability: "{{ states('sensor.victron_system_battery_power') | is_number }}"
        state: >
          {% if states('sensor.victron_system_battery_power')|float < 0 %}
            {{ -1 * states('sensor.victron_system_battery_power')|float }}
          {% else %}
            {{ 0 }}
          {% endif %}```

The orginal sensor has no “big jumps”

Uh, yes it does, it has multiple 200 to 400w jumps. And your riemann sum is most likely using the wrong equation which is leading to the increases.

Check out this post for details about riemann sum: