Setting the battery charge and discharge power

In the battery power sensor there are no negative values, only positive values ​​for both charging and discharging.
I would like to take out a sensor that, depending on the state of the battery direction sensor, can take out battery power watts whether they are charging or discharging.

Example:
If batterypower is: 300W and the state of battery direction: discharged I get an entity of power_battery_discharge

configuration.yaml

template:
  - sensor: 
      - name: Charging Power
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        state: >
          {% if is_state('sensor.charging_direction','charging') %}
             {{ states('sensor.battery_power') }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.charging_direction') and has_value('sensor.battery_power') }}"

      - name: Discharging Power
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        state: >
          {% if is_state('sensor.charging_direction','discharging') %}
             {{ states('sensor.battery_power') }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.charging_direction') and has_value('sensor.battery_power') }}"

Change the entity_ids to the ones that you actually have rather than my example.

thank you very much I appreciate it very much

1 Like