Calculating of electrical energy kWh from power sensor kW

Both of the above suggestions are incorrect. The utility meter needs to be fed with energy sensors, not power sensors. The Riemann Sum helper needs to have the power split into consumed and supplied first.

To do this you need to split your power into consumed and supplied energy sensors using template sensors:

configuration.yaml:

template:
  - sensor:
      - name: P2 Consumed
        unit_of_measurement: kW
        device_class: power
        state_class: measurement
        state: >
          {% if states('sensor.p2')|float(0) >= 0 %}
            {{ states('sensor.p2')|float(0) }}
          {% else %}
            0
          {% endif %}

      - name: P2 Supplied
        unit_of_measurement: kW
        device_class: power
        state_class: measurement
        state: >
          {% if states('sensor.p2')|float(0) < 0 %}
            {{ states('sensor.p2')|float(0)|abs }}
          {% else %}
            0
          {% endif %}

Then feed these two sensors to the Riemann Sum Helper to convert your two power sensors to energy sensors. Be sure to use method: left