Power to Grid if Watt <0 the kWh are counted

Hello all, I would like to count the kwh that I feed into the grid. unfortunately, my meter has a stop therefore this must go through a template sensor and the current power (W). That means if the watt goes into minus the kwh should be counted. My experience is currently very limited maybe someone can help me :slight_smile:

  - name: Power to Grid
        unique_id: power_to_grid
        icon: mdi:transmission-tower-export
        state_class: measurement
        device_class: energy
        unit_of_measurement: kWh
        state: if (sensor.tasmota_haus_power_curr < 0,
           sensor.tasmota_haus_power_curr * -1, 0)

and hereafter should be counted to kWh but there I currently have no idea how that should work because the power is not permanentl recorded :confused:

https://community.home-assistant.io/search?q=power%20negative%20kwh

1 Like

Thank you VDRainer, the first step is made :+1:

*to Grid
*from Grid

      - name: Power to Grid
        unique_id: power_to_grid
        icon: mdi:transmission-tower-export
        state_class: measurement
        device_class: power
        unit_of_measurement: W
        state: >
          {% if states('sensor.tasmota_haus_power_curr') | int > 0 %}
            0
          {% else -%}
            {{ (states('sensor.tasmota_haus_power_curr') | int) | abs }} 
          {% endif %}
          

but how will now the conversion in kWh in a sperate sensor?

Tip: Use method β€˜left’ for energy.