Yaml code for battery in energy dashboard

Hi all, i have a sensor called : sensor.victron_system_battery_power telling me if battery is charging or discharging. This is only 1 figure. The energy dashboard ask for 2 figures (battery in and battery out). How to create this via a template?
so when sensor shows -1.250W, it means battery is discharging. So i would need the following in my energy dashboard: battery in:0 & battery out: -1.250W.

Can someone help me on this?
Many thanks!

configuration.yaml

template:
  - sensor:  
      - name: Battery Power In
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        state: >
          {% if states('sensor.victron_system_battery_power') | float > 0 %}
            {{ states('sensor.victron_system_battery_power') }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.victron_system_battery_power') }}"

      - name: Battery Power Out
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        state: >
          {% if states('sensor.victron_system_battery_power') | float < 0 %}
            {{ states('sensor.victron_system_battery_power') |float | abs }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.victron_system_battery_power') }}"

Note: To be used in the energy dashboard you will need energy sensors, not power sensors.

So you will have to feed both sensor.battery_power_in and sensor.battery_power_out into the Riemann Sum Integral helper to convert power to energy. Make sure to use method: left to minimise approximation errors.

You can then use these two energy sensors in your energy dashboard.

1 Like

Hi Tom, when I filled in your YAML then I get this error message, can you help me with this?

I only have one sensor for charging and discharging the battery - sensor.victron_mqtt_battery_0_battery_power

BR,
Marcel

I provided the full YAML config with all the options for inclusion into configuration.yaml.

You have put all of that into just the state option using the GUI. The only bit that goes there is this, the part shown under the state: option in my config:

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