Charging a Tesla with excess solar production

I have seen a few other attempts at this but have not been satisfied with the solutions I have seen or tried. I wanted to take a stab at making my own here and wanted to share my progress.

I have the Tesla Powerwall (I have Tesla Solar and a Powerwall) and Wall Charger integrations. I also have the HACS Tesla integration, but have not used anything from there (yet)

I have the first half of the automation down regarding the trigger and condition. See below:

  trigger:
    platform: numeric_state
    entity_id: sensor.apf_generation_entity
    above: 0
  condition:
  - type: is_plugged_in
    condition: device
    device_id: <s>OMITTED</s>
    entity_id: binary_sensor.tesla_wall_connector_vehicle_connected
    domain: binary_sensor
  - condition: numeric_state
    entity_id: sensor.tesla_model_3_battery_level
    below: 80

As you can see, the trigger is a simple “is there excess energy” inquiry. I created that in my template.yaml page:

    - name: APF Generation Entity
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: "{{ (states('sensor.powerwall_solar_now')|float(0)*1000)|int(0) }}"

The two conditions I have checked are to see if the wall connector is connected to a Tesla and whether that car’s battery is below 80% charge.

Here is where things get… hard. The trigger portion I have been having issues with and I have based it on a few implimations of other people’s work. It also happens to be where I am losing my mind.

  action:
  - service: switch.turn_on
    entity_id: switch.tesla_wall_connector_switch
    data:
      current: '{% set max_charging_current = states(''input_number.tesla_charging_current'')
        | int %} {% set excess_power = states(''sensor.apf_generation_entity'') |
        float - max_charging_current %} {{ max_charging_current if excess_power <
        0 else states(''sensor.apf_generation_entity'') | float }}

I am having an issue because I’m trying to get the code to dynamically update the amps delivered based on the excess solar being producted. This should take on values between 0 amps (no charging and no excess) and 48 amps (max, balls-to-the-wall charge speed).

The sun has gone down at this stage, and I am tired. I was hoping to hear what other people’s thoughts are. I want to complete an automation and share it with others for their benefit.

It seems that Tesla is planning on updating their app to do just this.

According to a phrase found by Olympus, the “Drive on Sunshine” feature may require a Powerwall to function, although it’s not entirely clear. The phrase “Charge on Solar paused for Powerwall Export” hints at the integration of Tesla’s energy storage system with the new feature.

So I decided not to wait for that! :wink: