Optimizing use of unused Solar Power to charge a Tesla

Is there a way to do this dynamically, you limit your self to max 2500w?

In my case I can modulate the heating power from 0 to 10V which is from 0 to 2500W. I use a light swich brightness from 0 to 100% which can modulate the power output accordingly.

Should I create different low pass filter sensors for each specific need?

Sometimes there is enough power for 60min at 250/500W or 30min 1000/1500W or 2500W etc.

2500W is the excess I require to run my heat pump. If that is variable in your case, replace it with a sensor that contains how much power you want to draw.

How should I create a sensor, on the basis of what, using different power output levels?

do you have power outlet measuring power of your heat pump ? I am trying to understand how this works

Is this your heat pump ?

sensor.hot_water_power

So your binary sensor does the following “Solar Power” minus “Hot Water Power” is greater than 2500W
Why do you include the hot water power in your binary sensor?

Example
2000W solar power
-450 W house power consumption (how do you take care of this)
= 1550W excess power

Sorry for asking so many questions, I really appreciate your effort of sharing !

The hot water load is only on for a few hours a day. But is a big load, 1000W when on. So I have to take that into account.

If the inverter power minus the hot water power is greater than 2500W then I have enough power to run my base load (450W) and the heat pump (2000W). And I only run it based on a bunch of other temperature and manual automation settings (input booleans).

It’s pretty much specific to my situation and probably not much use to you.

With the new Tesla firmware 2021.36 we can set the charging speed from the api. And since this week the Tesla component has added the api call as well. So now we can adjust the speed like this:

service: tesla_custom.api
data:
  command: CHARGING_AMPS
  parameters:
    path_vars:
      vehicle_id: 'xxxxxxx'
    charging_amps: '5'

Hope this helps

3 Likes

I am now using the Tesla API to match charging to solar production.

Looks pretty good.

I have the following script setup to run every minute when the car is charging.

service: tesla_custom.api
data:
  command: CHARGING_AMPS
  parameters:
    path_vars:
      vehicle_id: '{{ state_attr(''binary_sensor.<car_name>_online_sensor'', ''id'') }}'
    charging_amps: '{{ ((states(''sensor.solaredge_solar_power''))| float *1.4 )| int }}'
5 Likes

The API can set charging amps to 0 as well, no more need to turn on/off?

By the way, how did you get your car on your energy dashboard?

1 Like

Yes you can just set it to 0 and it stops. You can even set values smaller than 5 to very slowly charge the car. I use my P1 smart meter values to calculate how much power can go to the car.

I think his card is the tesla-style-solar-power-card.

3 Likes

Following this thread heavily, now with the new API, I use the CircuitSetup ESP8266 based Energy Monitor to monitor solar production directly at the breaker which feeds my panel as soon as I get this update pushed to my car I plan to follow what you guys are doing here and variable update how much charge goes into the car based on that energy reading.

can you help me with using this api in python script. below is not working.

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

If you are going to use in python, I think you are better off calling teslajsonpy or TeslaPy directly rather than going back through Hass.

1 Like

I have had mixed results with setting to 0.

Sometimes it stops, sometimes it continues to trickle at 1 Amp.

Using the sun sensor I also switch off at sunset and switch on at sunrise.

Yes I’m using the Tesla style power card, which displays real-time power and energy and is thus a bit more advanced than the energy dashboard.

1 Like

Thanks @markpurcell

actually it worked. the mistake I did was taking the wrong vehicle_id. Looks like we should use the id (not vehicle_id) as vehicle_id.

I just want to say that this is a fantastic find. I’m about to expand my solar system, and I’m anticipating having plenty of surplus generation in the middle of the day. I’m just now switching from SmartThings to HA, and I had plans to devise something like this. Great to see that it’s already being developed. Hopefully, as I learn my way around HA, I’ll be able to contribute to this.

Thanks @markpurcell this is exactly the functionality that I need to take my home energy management system forward - I was so happy to see the amp change functionality appear in the Tesla app and was sure somebody had found out how to make use of it.

I was going to ask for help to get the tesla_custom.api to work, but found out through writing the request that I didn’t have the latest version which introduced the service call!

Can’t wait to get my charging matched up to my excess solar production - my custom energy dashboard below for interest.

1 Like

I have taken a slightly different approach because I have a battery aswell. The Tesla junction box decides when and how much to charge the battery so the amount directed to the car has to take into account what the box decides to do next. This code estimates how much solar can be directed to the car and works out the amount of amps by dividing by 0.26. I’m polling the automation every 30s.

service: tesla_custom.api
data:
  command: CHARGING_AMPS
  parameters:
    path_vars:
      vehicle_id: '{{ state_attr(''binary_sensor.<name>_online_sensor'', ''id'') }}'
    charging_amps: >-
      {% if ((states('sensor.powerwall_solar_now') | float -
      states('sensor.powerwall_load_now') | float -
      states('sensor.powerwall_battery_now') | float) / 0.26) | int  < 0 %}

          {{ 0 }}

      {% elif ((states('sensor.powerwall_solar_now') | float -
      states('sensor.powerwall_load_now') | float -
      states('sensor.powerwall_battery_now') | float) / 0.26) | int  >16 %} 

          {{ 16 }}

      {% else %}

          {{ ((states('sensor.powerwall_solar_now') | float - states('sensor.powerwall_load_now') | float - states('sensor.powerwall_battery_now') | float) / 0.26) | int }}

      {% endif %}

Thanks Frédéric! (pretty sure we’ve met on the Tesla fb group)
I have created the first automation, and a second one to stop charging on “sunset”.
The balancing is not necessary for me (simply because of the fact that my car is more empty than full).

sorry for my stupid question, I’m still new to HA, where do you put this script?
in the automation config? or in the developer tools > service?
Thanks
Andrea

You need to put this in the Scripts section:

Note the 3 phase scaling factor of * 1.4, 1000 W / ( 3 phase * 230 volt), for a single phase system you should use a factor of * 4.2, 1000 W / ( 1 phase * 230 volt)

Or you can automate the phase calculation as follows

service: tesla_custom.api
data:
  command: CHARGING_AMPS
  parameters:
    path_vars:
      vehicle_id: '{{ state_attr(''binary_sensor.<car>_online_sensor'', ''id'') }}'
    charging_amps: >-
      {{ (states('sensor.solaredge_modbus_meter_m1_ac_power')|int /
      states('sensor.ev_phases')|int / states('sensor.ev_voltage')|int)|int}}

If you run the script manually it will set the charging rate for you once.

You can then create an Automation to call this script every minute.

1 Like

I have tesla m3 and powerwall 2 as well. I am using the below python script and it works well so far.
Basically I leave my powerwall with 60% reserve at sunset (which is equal to my home usage during the night) and dump every last watt into my tesla. once my tesla is 100% I charge up my powerwall to 100% and the rest goes back to grid for credit.

####################################################################
             ###### tesla charging automation #####
####################################################################

tesla_charge_level = ''
tesla_is_awake = True

try:
    tesla_charge_level = int(float(hass.states.get('sensor.red3_battery_sensor').state))
except:
    logger.info('tesla is sleeping')
    tesla_is_awake = False

if tesla_is_awake:
    powerwall_charge_level = int(float(hass.states.get('sensor.powerwall_charge').state))
    powerwall_reserve = int(float(hass.states.get('input_number.powerwall_reserve').state))
    vehicle_id = hass.states.get('binary_sensor.red3_online_sensor').attributes['id']
    tesla_charge_rate = int(float(hass.states.get('sensor.red3_charging_rate_sensor').state) * 140/240)  ## convert from km to amps
    target_charge_rate = int(float(hass.states.get('input_number.target_charge_rate').state))
    tesla_charge_switch_entity = 'switch.red3_charger_switch'
    tesla_is_charging = True if hass.states.get(tesla_charge_switch_entity).state == 'on' else False
    tesla_is_home = True if hass.states.get('device_tracker.red3_location_tracker').state == 'home' else False
    tesla_is_plugged_in = True if hass.states.get('binary_sensor.red3_charger_sensor').state == 'on' else False
    tesla_is_not_fully_charged = True if tesla_charge_level != 100 else False
    grid_not_in_use = True if float(hass.states.get('sensor.powerwall_site_now').state) * 1000 < 100 else False
    solar_production_is_active = True if float(hass.states.get('sensor.powerwall_solar_now').state) * 1000 > 2000 else False

    # logger.info('vehicle_id:' + str(vehicle_id) +'pwcl:' + str(powerwall_charge_level) + ' pwr:' + str(powerwall_reserve) \
    #     + ' tcl:' + str(tesla_charge_level) + ' tcr:' + str(tesla_charge_rate) + ':' + str(target_charge_rate))

    ##### functions ##############
    def start_tesla_charging(tesla_is_charging, tesla_charge_rate, target_charge_rate):
        if not tesla_is_charging:
            hass.services.call('switch','turn_on', service_data={ 'entity_id': tesla_charge_switch_entity})
        
        if tesla_is_charging and target_charge_rate != tesla_charge_rate:
            hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id }, 'charging_amps': target_charge_rate}})
            
    def stop_tesla_charging(tesla_is_charging):
        if tesla_is_charging:
            hass.services.call('switch','turn_off', service_data={ 'entity_id': tesla_charge_switch_entity})
        
    ##### main ###########
    if tesla_is_home and tesla_is_plugged_in and tesla_is_not_fully_charged:
        if grid_not_in_use and powerwall_charge_level > powerwall_reserve:
            if solar_production_is_active:
                if powerwall_charge_level >= 80:
                    start_tesla_charging(tesla_is_charging, tesla_charge_rate, target_charge_rate) 
            else:
                start_tesla_charging(tesla_is_charging, tesla_charge_rate, target_charge_rate)
        else:
            stop_tesla_charging(tesla_is_charging)
1 Like