How to create 2 integral sensors from one energy sensor

I have solar panels which charge a battery.
when the battery is charging the sensor show a negative value in Watt. when the battery is delivering energy the same sensor shows a positive value in Watt.
how can i create two integral helpers, one which shows the energy for charging the battery in KWh and the other the the energy in KWh delivered from the battery to my grid?
hope somebody can help me with this as i would like to add this battery to the energy screen of home assistant.

First create two template sensors:

configuration.yaml

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

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

Then create two integral sensors to convert the power sensors sensor.battery_power_in and sensor.battery_power_out to energy.

You probably want to use method: left as solar power can switch levels quickly when clouds cross the sun. This integration can be set up in the UI or by using YAML.

You can then add these two energy sensors to your energy dashboard.

1 Like

thanks a lot!