Collect energy returned to grid

Hey,

I have a sensor that reads the power consumption directly from the electricity meter in watts via EDL21 integration. (Entity: sensor.sum_active_instantaneous_power).

When I draw power from the grid, the value is positive. When I feed power into the grid, the value is negative.

I have now built a sensor that shows me the current grid feed-in power in watts.
So if the Entity sensor.sum_active_instantaneous_power is negative, this value will be changed to positive, otherwise the value remains 0.

sensor:
  - platform: template
    sensors:
      instant_power_energy_to_grid:
        friendly_name: "Current Energy to Grid"
        unit_of_measurement: "W"
        value_template: >-
          {% set current_power = states('sensor.sum_active_instantaneous_power')|float %}
          {% if current_power < 0 %}
            {{ (-current_power) }}
          {% else %}
            0
          {% endif %}

Now I wanted to calculate the injected energy in kWh (total without reset) via the utility meter. However, the information is still in watts instead of kWh and also the calculation does not seem to be correct.

Source sensor: instant_power_energy_to_grid
Cycle: none
Offset: 0
net_meter: false
delta_values: false
periodically_resetting: false

What exactly am I doing wrong here?
I would like to add this then also in the energy dashboard.

Thanks!

Okay, i fixed it…
Just used the Integral function instead of utility meter and everything is working now…