Skip to content

Instantly share code, notes, and snippets.

@mattiasottosson
Created March 16, 2022 12:13
Show Gist options
  • Save mattiasottosson/7deb5db74d561dcaee7ec331679922e7 to your computer and use it in GitHub Desktop.
Save mattiasottosson/7deb5db74d561dcaee7ec331679922e7 to your computer and use it in GitHub Desktop.
Home Assistant API load sharing
vehicle_connected = hass.states.get('binary_sensor.tesla_wall_connector_contactor_closed').state == 'on'
charger_state_charging = int(hass.states.get('sensor.tesla_wall_connector_state').state) != 9
vehicle_id = hass.states.get('binary_sensor.ocisly_online_sensor').attributes['id']
is_charging = vehicle_connected and charger_state_charging
max_amps = 16
charger_max_amps = 10
l1_max = max_amps - float(hass.states.get('sensor.current_phase_1').state) + float(hass.states.get('sensor.tesla_wall_connector_phase_a_current').state)
l2_max = max_amps - float(hass.states.get('sensor.current_phase_2').state) + float(hass.states.get('sensor.tesla_wall_connector_phase_b_current').state)
l3_max = max_amps - float(hass.states.get('sensor.current_phase_3').state) + float(hass.states.get('sensor.tesla_wall_connector_phase_c_current').state)
current_max_amps = int(sorted([l1_max, l2_max, l3_max])[0])
current_max_amps = charger_max_amps if current_max_amps > charger_max_amps else current_max_amps
if current_max_amps < 0: current_max_amps = 0
last_current_max_amps = int(float(hass.states.get('input_number.wall_charger_amps').state))
if current_max_amps != last_current_max_amps:
hass.services.call('input_number', 'set_value', { 'entity_id': 'input_number.wall_charger_amps' , 'value': current_max_amps })
if is_charging:
try:
hass.services.call('tesla_custom','api', service_data={ 'command': 'CHARGING_AMPS', 'parameters': { 'path_vars': {'vehicle_id': vehicle_id }, 'charging_amps': current_max_amps}})
except:
logger.warning('Could not set charge state')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment