Skip to content

Instantly share code, notes, and snippets.

@amacneil
Last active May 17, 2025 20:23
Show Gist options
  • Save amacneil/3345024ae1ec090e90c93ddaf8515d67 to your computer and use it in GitHub Desktop.
Save amacneil/3345024ae1ec090e90c93ddaf8515d67 to your computer and use it in GitHub Desktop.
alias: Solar Car Charging
description: Carge vehicle with excess solar production
triggers:
- trigger: time_pattern
minutes: /5
hours: "*"
conditions:
- condition: sun
before: sunset
after: sunrise
- condition: state
entity_id: binary_sensor.charge_cable
state: "on"
actions:
- variables:
prev_excess_mwh: >
{% set production =
states('sensor.envoy_202418015164_lifetime_energy_production') | float
%} {% set consumption =
states('sensor.envoy_202418015164_lifetime_energy_consumption') | float
%} {% set discharged =
states('sensor.envoy_202418015164_lifetime_battery_energy_discharged') |
float %} {{ production - consumption - discharged }}
- delay: "00:04:00"
- variables:
delay_mins: 4
min_charge_amps: 5
max_charge_amps: 40
curr_charge_amps: >
{{ (states('number.tesla_model_y_charge_current') | float | round) if
states('switch.tesla_model_y_charge') == 'on' else 0 }}
curr_excess_mwh: >
{% set production =
states('sensor.envoy_202418015164_lifetime_energy_production') | float
%} {% set consumption =
states('sensor.envoy_202418015164_lifetime_energy_consumption') | float
%} {% set discharged =
states('sensor.envoy_202418015164_lifetime_battery_energy_discharged') |
float %} {{ production - consumption - discharged }}
net_excess_wh: "{{ ((curr_excess_mwh - prev_excess_mwh) * 1000000) | round(3) }}"
excess_solar_amps: "{{ (net_excess_wh / (delay_mins / 60) / 240) | round(1) }}"
target_excess_solar_amps: 1
new_charge_amps: "{{ curr_charge_amps + excess_solar_amps - target_excess_solar_amps }}"
clamped_charge_amps: |
{% if new_charge_amps < min_charge_amps %}
0
{% elif new_charge_amps > max_charge_amps %}
{{ max_charge_amps }}
{% else %}
{{ new_charge_amps | int }}
{% endif %}
- choose:
- conditions:
- condition: template
value_template: "{{ clamped_charge_amps == 0 }}"
sequence:
- if:
- condition: state
entity_id: switch.tesla_model_y_charge
state: "on"
then:
- type: turn_off
domain: switch
device_id: fe0dbf51c18955c2b97d525df11ac9ac
entity_id: switch.tesla_model_y_charge
- delay: "00:00:05"
- target:
device_id: fe0dbf51c18955c2b97d525df11ac9ac
entity_id: number.tesla_model_y_charge_current
data:
value: "{{ max_charge_amps }}"
action: number.set_value
- conditions:
- condition: template
value_template: "{{ clamped_charge_amps > 0 }}"
sequence:
- if:
- condition: state
entity_id: switch.tesla_model_y_charge
state: "off"
then:
- type: turn_on
domain: switch
device_id: fe0dbf51c18955c2b97d525df11ac9ac
entity_id: switch.tesla_model_y_charge
- target:
device_id: fe0dbf51c18955c2b97d525df11ac9ac
entity_id: number.tesla_model_y_charge_current
data:
value: "{{ clamped_charge_amps }}"
action: number.set_value
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment