Tracking EV charging consumption only when on grid and not on solar

Hi,

I’m trying to get the cost of charging my EV from grid on a monthly basis.

I have a solar array but during winter I often switch from solar to grid. So I would like to create a sensor that can calculate the total amount of energy used from grid not from solar to charge my EV, knowing that when I am on solar power, the grid energy consumption = 0. I never have both at the same time.

What I have:

  • a sensor from my solar inverter for output power
  • a sensor from my grid output power
  • a sensor from my wallbox charging station added energy
  • a binary sensor from wallbox when EV is charging
  • a sensor for the actual price per kWh.

What I need:

  • a sensor that is doing the following:
    WHEN ev_charge is ON
    IF grid_output_power > 0
    calculate the increasing total on a monthly basis from wallbox_added_energy and multiply by price per kwh.

I am not new to ha and programming but in this particular case, i don’t know where to start, some help would be much appreciated.

You just need the amount of Charing-Power of the Wallbox.

I would write a template sensor like this

sensor:
  - name: "Energy EV-Charge From Grid"
    unit_of_measurement: W
    state_class: measurement
    device_class: power
    state: >
      {% if state_attr('binary_sensor.wallbox_charching', 'on') == true %}
        {% if states('sensor.power_own_use') | float(0) > 0 %}
          {{(states('sensor.wallbox_charing_power'))}}
        {% else %}
          0
        {% endif %}  
      {% endif %}

With this new sensor you can calculate the energy sensor easily like this.

platform: integration
source: sensor.solar_power_net
method: left

After this, you can go further with the helper, and add daily/monthly rotation.

Maybe i forgot something.
Feel free to test.

Cheers
Matt