Optimizing use of unused Solar Power to charge a Tesla

I’m experiencing the following (haven’t read it in this discussion):
Whenever Tesla is offline, the start or stop charging commands don’t work. Probably nothing to do with the automation, because of: when you start charging manually via HA (button f.e.), you experience the same thing (I have to click it twice).

In other words: is there a way to wake up the car before sending the “start charge” command?

I’ve got it installed, however I dont seem to have a powerwall_reserve so am getting this error…

2021-12-04 10:35:46 ERROR (SyncWorker_7) [homeassistant.components.python_script.auto_charge_tesla.py] Error executing script: float() argument must be a string or a number, not 'NoneType'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 222, in execute
    exec(compiled.code, restricted_globals)
  File "auto_charge_tesla.py", line 16, in <module>
TypeError: float() argument must be a string or a number, not 'NoneType'

@icepicknz

you need to create two input numbers with below names. Also I made slight changes to my code (in my original code above), so please make sure you got those changes.

target_charge_rate
powerwall_reserve

Thanks,

I got around this by setting the values as static variables for now…

    powerwall_reserve = int('60')
    target_charge_rate = int('32')

I’m seeing log entries but it doesn’t appear to be changing the cars amps…

2021-12-04 11:24:16 ERROR (SyncWorker_2) [homeassistant.components.python_script.auto_charge_tesla.py] vehicle_id:14929324xxxxxxxxpwcl:61 pwr:60 tcl:82 tcr:4:32
2021-12-04 11:27:16 ERROR (SyncWorker_2) [homeassistant.components.python_script.auto_charge_tesla.py] vehicle_id:14929324xxxxxxxxpwcl:61 pwr:60 tcl:82 tcr:6:32

I think I got it working; though the charge rate I think I may need to play with as I have a Tesla model X so the values may differ to model3

@cpuram I wonder if you wouldn’t mind helping me, I’m trying to dumb down your script a little by just setting my cars to 16A when two cars are plugged in, or 32A if only one car is plugged in.

@icepicknz

when you plug in the charger, I don’t think it would change the car at 32A (even if that is what the car is set to). It slowly ramps up in a few seconds. You just have to make sure you automation kicks in just in time to change the AMPS. For that to work I would

  1. first add the charger sensor (binary_sensor.red3_charger_sensor) to trigger entities. So that the the automation gets triggered as soon as you plug in the charger (hopefully before it ramps up all the way up)
  2. in the main section put in the logic to check the status of the other car and set the AMPS to appropriate levels.

I’m guessing the charge rate is the max (32A for single phase), and powerwall_reserve is self explanatory. I dont however see in the script how you are changing the Amps to match solar production and only use what would normally be exported to the grid?

I dont need to do that because of my unique situation (solar prod = powerwall max delivery). Occasionally I change it manually by sliding the target_charge_rate slider. Anyway you can call the service as below. Its part of my start_tesla_charging in my code.

hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id }, 'charging_amps': target_charge_rate}})

OK, I think I’ve made progress - thanks for your tips cpuram. You were right the input number needed to be renamed to target_charge_rate. I missed the change in your python script.

I can now manually set the Amps charge rate via the ‘target_charge_rate’ input number.

I’m a little confused by your statement above, in relation to matching excess solar production to the Amp charge rate: “I dont need to do that because of my unique situation (solar prod = powerwall max delivery).”

Could you elaborate a bit on your situation and what you mean by this? Surely you would still want to optimise the use of excess solar to match your car charge rate?

Either way, I would like to expand this so that the Amp charge rate of the car is matched to the excess solar production. Being a newbie to Hass (and not a python coder) do you have any tips on how I could do this?

I assume I would have to utilise one of the powerwall HA sensors and match grid export power to set the target_charge_rate input number?

Any help appreciated :slight_smile:

@Chacsam
Have you changed anything to your coding or elsewhere since you’ve posted this ? I intend to create the exact same (also including homewizard p1)

Basically no.
As I have an old MCU1 model S, I didn’t get the API update allowing me to tune charging rate, I stuck with on/off :wink:

I have MCU2 and use and will keep on using on/off, for now. I drive enough so charging on the remaining of my solar panels wouldn’t bring me very far (literally :grin:).
Could be that this changes in summer (my max solar panel output on the best summer day is 40 kWh).
Have to say I don’t have a home battery.

Hmm I’d definitely need it to be able to also change the amperage automatically. I have 60kw days in the summer and it would be nice to push all excess on the car without suddenly having high peak if the sun is gone for a bit.

I first need to find a raspberry pi which is difficult given chip shortage, or an odriod n2+ which seems more easy to get (does it really matter?)

Good Morning,
I have just read in the 2022.2 release notes that HomeWizard has become an official integration.
Might be useful to start from that one at once:

I have tried to use (kind of all) options but i am still not there yet. I landed on the @Chacsam one for now which i inputted in the visual editor as putting the code in the YAML did not work so i must have done something wrong there

So now it works when i press ‘run action’, but it does not seem to trigger it on its own? Is there anyone who can help me with this? the below is what i have

alias: TeslaStartCharging
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.p1_meter_<notsureifIshouldshowthisid>_active_power
    below: '0'
    for:
      hours: 0
      minutes: 0
      seconds: 20
condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: Away
  - condition: state
    entity_id: binary_sensor.tesla_model_3_charger_sensor
    state: 'on'
  - condition: state
    entity_id: switch.tesla_model_3_charger_switch
    state: 'off'
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.tesla_model_3_charger_switch
mode: single

alias: TeslaStopCharging
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.p1_meter_<notsureifIshouldshowthisid>_active_power
    above: '1500'
    for:
      hours: 0
      minutes: 0
      seconds: 20
condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: Away
  - condition: numeric_state
    entity_id: sensor.tesla_model_3_charging_rate_sensor
    above: '2'
  - condition: not
    conditions:
      - condition: state
        entity_id: switch.tesla_model_3_maxrange_switch
        state: 'on'
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.tesla_model_3_charger_switch
mode: single

Once this works i would like to see if i can also change the amperage based on excess solar available

Your condition is on “Away”

condition:
  - condition: state
    entity_id: device_tracker.tesla_model_3_location_tracker
    state: Away

That means that the automation only kicks off when the car is not at home.
Could that be the issue? (You can also check the trace of the automation when clicking on the clock)

@Chacsam

I thought I explained that in my post but apparently I did not save that part.

The location always shows ‘away’ so I probably have to adjust some settings in the car itself

So I don’t think that is the issue at this point as it is constantly on ‘away’ in case.

Screenshot_20220205-182650_Home Assistant

I have a feeling I have just forgotten something very stupid here so that it actively checks every X seconds or minutes or so. Like a loop Function if that makes sense

OK, Slice the elephant.
As the condition is not relevant here, remove it from your automation.
The technical state for “Away” is “not_home”, so your condition should match “not_home” anyway.

A test you can do is manually activate the switch from the services tab in the development tools. Does that work?

Ok @Chacsam I sliced the elephant and took out the location condition

Yes?, I was able to run both automations in developer tools - services