Energy Grid / Sensor off

Hey folks!

Bit of an odd issue here, really. I have an power system in my home that can -via modbus- poll the current Usage in Watts. It is just a measurement in this very second in time, nothing accumulated.


  - name: e3dc
    type: tcp
    host: 10.2.1.10
    port: 502
    sensors:
      - name: E3DC power consumption
        unit_of_measurement: W
        device_class: power
        address: 40071

This imports, in watt, the current reading, which works:

Now, this obviously lacks statistics and is not in the correct form, So I did this template sensor:

  - sensor:
      - name: "Grid Power In"
        unit_of_measurement: "kWh"
        state_class: measurement
        device_class: energy
        state: >
          {% if states('sensor.e3dc_grid_power') | float(0) >= 0 %}
            {% set gridin = states('sensor.e3dc_grid_power') | float(0) %}
          {% else %}
            {% set gridin = 0 %}
          {% endif %}
          {{ gridin / 1000 | round(3, default=0) }}
        attributes:
          last_reset: '1970-01-01T00:00:00+00:00'

Which results in upscaled kWh instead of Wh:

(The oddly missing spike is the wallbox that I am currently configuring and is missing in one input/output; please ignore.

But the energy dashboard, after waiting a day, is going nuts:

Direction is all wrong, big Candle up is 11kwh usage, the big candle down 10.1kwh usage. The calculated costs are a dream, but lack any sort of reality:

Where exactly am I doing something wrong here? The data points are there, graphs are plotted okay, but the energy dashboard, using the same data is going crazy?

Cheers,
Chris.

You seem to be mixing W and Wh. These are not interchangeable. Watts are power, watt hours are energy.
You can integrate power over time to get energy: Integration - Riemann sum integral - Home Assistant