Convert an bidirectional energy meter to two cumulating ( import / export )

I have an energy meter in the grid path that measures the usual stuf (current, voltage, power) and also integrates the power to an energy meter. By design this meter work bidirectional (net meter).

But the energy dashboard insists of having two separate meters for grid consumption and return.
I did not manage to find an easy way using template sensors to convert this meter to two meter without negative values.

I managed to split the power and then use the HA integration helper to have these two virtual meters calculated. But these integration meter differ ~ 10% from the “built in” energy counter of the meter and sometimes some spikes produce false values that I then have to correct manually in the statistics. So I don’t want to use the integration helper of HA anymore and just split the energy meter values I already accurately receive.

Any solution on this? Because i have the same problem. Net Value is wrong calculated.

Sascha

Yes, but I don’t like it very much.

My input value that goes both ways is e3dcfs_grid_energy

I have created 2 Helper / Input Numbers:

  • e3dcfs_grid_energy_import
  • e3dcfs_grid_energy_export

like this

I have created 2 automations. In yaml:

- id: '1676582846413'
  alias: e3dcfs_grid_energy_goes_higher
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.e3dcfs_grid_energy
  condition:
  - condition: template
    value_template: '{{ states(''sensor.e3dcfs_grid_energy'') not in [''unavailable'',
      ''unknown'', ''none''] }}'
  - condition: template
    value_template: '{{ (trigger.to_state.state | float) > (trigger.from_state.state
      | float) }}'
  action:
  - service: input_number.set_value
    data:
      value: '{{ (states.input_number.e3dcfs_grid_energy_import.state | round(3))
        + (trigger.to_state.state | round(3)) - (trigger.from_state.state | round(3))
        }}'
    target:
      entity_id: input_number.e3dcfs_grid_energy_import
  mode: single
- id: '1676583393733'
  alias: e3dcfs_grid_energy_goes_lower
  description: ''
  trigger:
  - platform: state
    entity_id:
    - sensor.e3dcfs_grid_energy
  condition:
  - condition: template
    value_template: '{{ states(''sensor.e3dcfs_grid_energy'') not in [''unavailable'',
      ''unknown'', ''none''] }}'
  - condition: template
    value_template: '{{ (trigger.to_state.state | float) < (trigger.from_state.state
      | float) }}'
  action:
  - service: input_number.set_value
    data:
      value: '{{ (states.input_number.e3dcfs_grid_energy_export.state | round(3))
        + (trigger.from_state.state | round(3)) - (trigger.to_state.state | round(3))
        }}'
    target:
      entity_id: input_number.e3dcfs_grid_energy_export
  mode: single

I created also 2 utlity meters in configuration yaml:

utility_meter:
  e3dcfs_grid_energy_import_daily:
    unique_id: e3dcfs_grid_energy_import_daily
    net_consumption: true
    source: input_number.e3dcfs_grid_energy_import
    cycle: daily
  e3dcfs_grid_energy_export_daily:
    unique_id: e3dcfs_grid_energy_export_daily
    net_consumption: true
    source: input_number.e3dcfs_grid_energy_export
    cycle: daily

These two are then used in the energy dashboard for grid consumption and return to grid.

Thank you. I’m in the same situation (one grid meter going in 2 directions) and this was driving me crazy.

I’ll will put that in place when I get back from work tomorrow. Thank you for describing it. This is much appreciated !

One question though: What happen when the sensor.e3dcfs_grid_energy goes unuvailable for some time ? Since this sensor is a cumulative sensor (KWh) it shouldn’t matter as this is already an integrator.
The computation doing a trigger.to_state.state - trigger.from_state.state, since the from_state.state is probably ‘unavailable’ and not the state it had before going unavailable, my understanding is that we “loose” the energy consumed while the sensor.e3dcfs_grid_energy was unavailable.

Is that right ?

So. I did set it up like @CamFlyerCH did and I finally got something meaningful on the energy dashboard.
THANK YOU.

As suspected, this is not working fine when the meter goes ‘unavailable’.
When it gets back, the 2 automations fail (logically) like this:

It mean that any delta that happened while the meter was unavailable will not be counted in the export nor in the import. This is a shame because after all the meter in kwh is an integrator and whatever the number of sample are lost, we always have the last value.

Tomorrow, I’ll add yet another intermediary helper that will always be updated with the meter value unless this value is unavailable, unknown or none.
This intermediary helper will then be used by the two integrations which mean that the input sensor will never be unavailable and will always have the last known value from the meter. This way, the difference between the from_state and the to_state will always give the real delta of the real meter and we won’t loose any kWh.

I’ll report here the results of my tests.

1 Like

Hello zorglups. Thanks it helped. It working fine since march. And since e3dcfs_grid_energy itself is an template sensor combining 3 phase counters, it will probably not be unavailable for a long time. I am not sure what would happen.
I have sometimes other wrong values to correct on other energy counters:
image
Of coarse I could correct that then in statistics.