Automation help for 2 Tesla cars; half charge rate when both plugged in

Hey guys, I’m very green and new to HA and trying to get some automation working. We have a Tesla Model X and a Tesla Model 3; both have 32A chargers and our house only has 63A feed into it, so if both cars are charging we are at Max limit and can pop a fuse at the road.

I’m trying to detect when 2 cars are plugged in, to half the charge rate to 16A each, if 1 car is plugged in, then I wish to push 32A to that one car.

I’ve started with this basic, and its working to update the charge rate on the car…

alias: Tesla Charge Rate
description: ''
trigger:
  - type: turned_on
    platform: device
    device_id: d4900302e6362a484f8ce927687d2381
    entity_id: binary_sensor.tezzie_charger_sensor
    domain: binary_sensor
  - type: turned_on
    platform: device
    device_id: 246b4f14158fd3fc67a22bea29d901bb
    entity_id: binary_sensor.eevee_charger_sensor
    domain: binary_sensor
condition: []
action:
  - service: tesla_custom.api
    data:
      command: CHARGING_AMPS
      parameters:
        path_vars:
          vehicle_id: '{{ state_attr(''binary_sensor.tezzie_online_sensor'', ''id'') }}'
        charging_amps: '16'
mode: single

That sounds a bit risky.
You are detecting if two cars are plugged in and then changing the charging value, but then the risky situation have already occured.

we plug them in before 9pm, the cars would have changed to 16A before the charging schedule kicks in at 11pm

I’ve tried o modify one of @cpuram scripts as follows

UPDATE: this is now working

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

tesla_charge_level = ''
tesla_is_awake = True
tesla_is_awake_bex = True


try:
    tesla_charge_level = int(float(hass.states.get('sensor.tezzie_battery_sensor').state))
except:
    logger.warning('tezzie is sleeping')
    tesla_is_awake = False

try:
    tesla_charge_level_bex = int(float(hass.states.get('sensor.eevee_battery_sensor').state))
except:
    logger.warning('eevee is sleeping')
    tesla_is_awake_bex = 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.tezzie_online_sensor').attributes['id']
    vehicle_id_bex = hass.states.get('binary_sensor.eevee_online_sensor').attributes['id']
    tesla_charge_rate = int(float(hass.states.get('sensor.tezzie_charging_rate_sensor').state) * 140/240)  ## convert from km to amps
    tesla_requested_charge_rate = hass.states.get('sensor.tezzie_charging_rate_sensor').attributes['charge_current_request']
    tesla_requested_charge_rate_bex = hass.states.get('sensor.eevee_charging_rate_sensor').attributes['charge_current_request']
#    target_charge_rate = int(float(hass.states.get('input_number.tesla_charge_rate').state))
    target_charge_rate = int('32')
    tesla_charge_switch_entity = 'switch.tezzie_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.tezzie_location_tracker').state == 'home' else False
    tesla_is_home_bex = True if hass.states.get('device_tracker.eevee_location_tracker').state == 'home' else False
    tesla_is_plugged_in = True if hass.states.get('binary_sensor.tezzie_charger_sensor').state == 'on' else False
    tesla_is_plugged_in_bex = True if hass.states.get('binary_sensor.eevee_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
    tesla_online_state = True if hass.states.get('binary_sensor.tezzie_online_sensor').state == 'on' else False
    tesla_online_state_bex = True if hass.states.get('binary_sensor.eevee_online_sensor').state == 'on' else False

    logger.error('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) + ' rcr:' + str(tesla_requested_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})
#            logger.error('Starting to charge')

        if tesla_is_plugged_in and tesla_is_plugged_in_bex:
            if tesla_requested_charge_rate != 16:
                hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id }, 'charging_amps': '16'}})
                logger.error('setting Tezzie to 16A')

        if tesla_is_plugged_in and not tesla_is_plugged_in_bex:
            if tesla_requested_charge_rate != 32:
                hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id }, 'charging_amps': '32'}})
                logger.error('setting Tezzie to 32A')

            if tesla_is_home_bex and tesla_is_awake_bex and tesla_online_state_bex:
                if tesla_requested_charge_rate_bex != 16:
                    hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id_bex }, 'charging_amps': '16'}})
                    logger.error('setting Eevee to 16A as car is home but not plugged in, safety precaution')

#        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}})
#            logger.error('changing charge amps')
            
    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})
            logger.error('stopping charge')


if tesla_is_awake_bex:
    vehicle_id = hass.states.get('binary_sensor.tezzie_online_sensor').attributes['id']
    vehicle_id_bex = hass.states.get('binary_sensor.eevee_online_sensor').attributes['id']
    tesla_charge_rate = int(float(hass.states.get('sensor.tezzie_charging_rate_sensor').state) * 140/240)  ## convert from km to amps
    target_charge_rate = int('32')
    tesla_charge_switch_entity = 'switch.tezzie_charger_switch'
    tesla_requested_charge_rate = hass.states.get('sensor.tezzie_charging_rate_sensor').attributes['charge_current_request']
    tesla_requested_charge_rate_bex = hass.states.get('sensor.eevee_charging_rate_sensor').attributes['charge_current_request']
    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.tezzie_location_tracker').state == 'home' else False
    tesla_is_home_bex = True if hass.states.get('device_tracker.eevee_location_tracker').state == 'home' else False
    tesla_is_plugged_in = True if hass.states.get('binary_sensor.tezzie_charger_sensor').state == 'on' else False
    tesla_is_plugged_in_bex = True if hass.states.get('binary_sensor.eevee_charger_sensor').state == 'on' else False
    tesla_is_not_fully_charged = True if tesla_charge_level != 100 else False
    tesla_online_state = True if hass.states.get('binary_sensor.tezzie_online_sensor').state == 'on' else False
    tesla_online_state_bex = True if hass.states.get('binary_sensor.eevee_online_sensor').state == 'on' else False

    ##### functions ##############
    def start_tesla_charging_bex(tesla_is_charging, tesla_charge_rate, target_charge_rate):
        if tesla_is_plugged_in_bex and tesla_is_plugged_in:
            if tesla_requested_charge_rate_bex != 16:
                hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id_bex }, 'charging_amps': '16'}})
                logger.error('setting Eevee to 16A')

        if tesla_is_plugged_in_bex and not tesla_is_plugged_in:
            if tesla_requested_charge_rate_bex != 32:
                hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id_bex }, 'charging_amps': '32'}})
                logger.error('setting Eeevee to 32A')

            if tesla_is_home and tesla_is_awake and tesla_online_state:
                if tesla_requested_charge_rate != 16:
                    hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id }, 'charging_amps': '16'}})
                    logger.error('setting Tezzie to 16A as car is home but not plugged in, safety precaution')

    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})
            logger.error('stopping charge')
        
    ##### 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)

    if tesla_is_home and tesla_is_plugged_in:
        start_tesla_charging(tesla_is_charging, tesla_charge_rate, target_charge_rate)

    if tesla_is_home_bex and tesla_is_plugged_in_bex:
        start_tesla_charging_bex(tesla_is_charging, tesla_charge_rate, target_charge_rate)

Can’t you just have two triggers (as you do now), and an “and” condition that both are plugged in, and then set both cars to 16 amps? If only one car is plugged in, the automation won’t fire. You could have another one (or a choose block) that sets them back to 32 amps once one is unplugged.

But, better yet, do your two chargers not support this functionality?

Even though this is solved I thought I would also mention with most of the wall chargers ( mine is the 80 amp) they have a set of com ports that communicate between the units. You set the charger to the max current and it will do power sharing if both are plugged in. This allows signaling to the cars to reduce the charge amount is my guess. This way you can have two 80 amps in say a 100 amp panel and you will not blow the breakers. Not sure which units still have the feature but worth looking to see if yours does. Protection at the controller level is a good fall back if HA decides to flake out.

2 Likes

The Gen 3 chargers still allow this and it’s over Wi-Fi instead of wired like it was previously. Documentation is here.

1 Like