Skip to content

Instantly share code, notes, and snippets.

@adampetrovic
Last active December 13, 2022 22:56
Show Gist options
  • Save adampetrovic/b0343809592161bc751f31186d09c42c to your computer and use it in GitHub Desktop.
Save adampetrovic/b0343809592161bc751f31186d09c42c to your computer and use it in GitHub Desktop.
Charge Tesla with Excess Solar
sensor:
- platform: statistics
name: "Solar Export Median - 1 minute (internal)"
unique_id: solar_export_median_internal
entity_id: sensor.solarexport
state_characteristic: median
max_age:
minutes: 1
- platform: statistics
name: "Grid Import Median - 1 minute (internal)"
unique_id: grid_import_median_internal
entity_id: sensor.gridimport
state_characteristic: median
max_age:
minutes: 1
template:
# we need these template sensors to extrapolate to zero if no readings are received for the
# median calculation. This is achieved using the float(0) filter.
- sensor:
- name: "Solar Export Median - 1 minute"
unique_id: solar_export_median
state: "{{ states('sensor.solar_export_median_internal') | float(0) }}"
unit_of_measurement: W
- name: "Grid Import Median - 1 minute"
unique_id: grid_import_median
state: "{{ states('sensor.grid_import_median_internal') | float(0) }}"
unit_of_measurement: W
- trigger:
- platform: state
entity_id: sensor.solar_export_median
- platform: state
entity_id: sensor.grid_import_median
sensor:
- name: "Tesla Max Charge Rate"
unique_id: tesla_max_charge_rate
unit_of_measurement: A
# 3 phase calculation
# Amps = Watts / sqrt(3) * Power Factor (1.0) * Voltage (415v)
# = Watts * (1/717)
# = Watts * 0.0014
# Logic: update max charge rate based off current amperage and add excess amperage from
# export, subtract amperage from import
state: >
{{
[ 0,
(
(states('sensor.tesla_wall_connector_max_amps') | float(0)) +
(states('sensor.solarexport') | float * 0.0014) -
(states('sensor.gridimport') | float * 0.0014)
) | int
] | max
}}
automation:
- id: 'start_tesla_charge_excess_solar'
alias: '[Tesla Solar] Start Charging Tesla with excess solar'
trigger:
# only start charging if we've had 1 amp (720W~) of excess solar for > 30 seconds
- platform: numeric_state
entity_id: sensor.solarexport
above: 720.0
for:
seconds: 30
condition:
- "{{ is_state('binary_sensor.tesla_wall_connector_vehicle_connected', 'on') }}"
- "{{ is_state('binary_sensor.tesla_wall_connector_charge_state', 'off') }}"
- "{{ is_state('input_boolean.tesla_solar_excess_charging', 'on') }}"
action:
- service: script.turn_on
target:
entity_id: script.tesla_start_charger
data:
variables:
reason: "Solar Excess"
notify: false
- id: 'stop_tesla_charge_excess_solar'
alias: '[Tesla Solar] Stop Charging Tesla when insufficient solar'
trigger:
# stop charging if we've had 1 amp (720W~) of import for > 120 seconds
- platform: numeric_state
entity_id: sensor.gridimport
above: 720.0
for:
minutes: 2
condition:
- "{{ is_state('binary_sensor.tesla_wall_connector_vehicle_connected', 'on') }}"
- "{{ is_state('binary_sensor.tesla_wall_connector_charge_state', 'on') }}"
- "{{ is_state('input_boolean.tesla_solar_excess_charging', 'on') }}"
action:
- service: script.turn_on
target:
entity_id: script.tesla_stop_charger
data:
variables:
reason: "Insufficient Solar"
notify: false
- id: 'set_tesla_charge_speed'
alias: '[Tesla Solar] Set charge speed (amps)'
trigger:
- platform: state
entity_id: sensor.tesla_max_charge_rate
condition:
- "{{ is_state('binary_sensor.tesla_wall_connector_vehicle_connected', 'on') }}"
- "{{ is_state('binary_sensor.tesla_wall_connector_charge_state', 'on') }}"
- "{{ is_state('input_boolean.tesla_solar_excess_charging', 'on') }}"
action:
service: script.turn_on
target:
entity_id: script.tesla_set_charge_rate
data:
variables:
rate: "{{ states('sensor.tesla_max_charge_rate') | float(0) }}"
script:
tesla_set_charge_rate:
alias: "[Tesla Solar] set charge rate"
fields:
rate:
description: "Charge rate in Amps"
example: "5"
default: "0"
sequence:
- service: tesla_custom.api
data:
command: CHARGING_AMPS
parameters:
path_vars:
vehicle_id: "{{ state_attr('binary_sensor.venom_online', 'id') }}"
charging_amps: "{{ rate | int }}"
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment