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)