Skip to content

Instantly share code, notes, and snippets.

@bruxy70
Created July 2, 2024 19:20
Show Gist options
  • Save bruxy70/1ca138d3250ea7e5ff77ee16140a0372 to your computer and use it in GitHub Desktop.
Save bruxy70/1ca138d3250ea7e5ff77ee16140a0372 to your computer and use it in GitHub Desktop.
Home Assistant script setting the wallbox and smart switch combination for the target charging power
alias: EMS - EV set wallbox power 🚗
fields:
new_value:
name: Watts
required: true
description: |
Watts (0-1400 = off, 1400-3840 = 1f, 4320-11520+ = 3f)
example: 2400
selector:
number:
min: 0
max: 11520
unit_of_measurement: Watt
step: 10
sequence:
- if:
- condition: state
entity_id: sensor.wallbox_phases
state: "0"
then:
- service: switch.turn_on
data: {}
target:
entity_id:
- switch.wallbox_l1
- delay: "00:00:10"
- alias: Turn off charging if small power
if:
- condition: template
value_template: >-
{{ (new_value < 1600 and
is_state('switch.wallbox_pause_resume','off')) or (new_value < 1400)
}}
alias: Less than minimal power
then:
- if:
- condition: template
value_template: "{{ is_state('switch.wallbox_pause_resume','on') }}"
alias: Wallbox is on
then:
- service: switch.turn_off
data: {}
target:
entity_id:
- switch.wallbox_pause_resume
- stop: ""
- variables:
current_phases: "{{ states('sensor.wallbox_phases') | int(0) }}"
new_phases: |-
{%if is_state('timer.wallbox_phases_changed','idle') %}
{%if (current_phases == 3 and new_value >= 4000) or new_value >= 5000 %}
3
{% elif (current_phases == 2 and new_value >= 2400) or new_value >= 2500
%}
2
{% else %}
1
{% endif %}
{% else %}
{{ current_phases }}
{% endif %}
alias: >-
Define variables current_phases and new_phases (elif second condition
reduced from 3600 to 2500 W)
- if:
- condition: template
value_template: "{{ new_phases != current_phases }}"
then:
- if:
- condition: state
entity_id: switch.wallbox_pause_resume
state: "on"
then:
- service: switch.turn_off
alias: Stop charging
data: {}
target:
entity_id:
- switch.wallbox_pause_resume
- service: number.set_value
target:
entity_id: number.wallbox_maximum_charging_current
data:
value: |-
{{ max(6,min(16,(new_value / 240 / new_phases |
int(1)) | round(0) )) }}
alias: Set the new charging current before turning wallbox off
- delay: "00:00:05"
- service: switch.turn_off
data: {}
target:
entity_id:
- switch.wallbox_l1
- switch.wallbox_l2
- switch.wallbox_l3
- delay: "00:00:05"
- choose:
- conditions:
- condition: template
value_template: "{{ new_phases == 3 }}"
sequence:
- service: switch.turn_on
data: {}
target:
entity_id:
- switch.wallbox_l1
- switch.wallbox_l2
- switch.wallbox_l3
- conditions:
- condition: template
value_template: "{{ new_phases == 2 }}"
sequence:
- service: switch.turn_on
data: {}
target:
entity_id:
- switch.wallbox_l1
- switch.wallbox_l2
default:
- service: switch.turn_on
data: {}
target:
entity_id:
- switch.wallbox_l1
- service: timer.start
data: {}
target:
entity_id:
- timer.wallbox_phases_changed
- delay: "00:00:15"
alias: Switch between 1, 2f and 3f
- variables:
max_current: >-
{{ iif(is_state('sensor.wallbox_phases','3') or
states('input_select.ev_charging_mode') != 'Solar'
,16,iif(is_state('sensor.wallbox_phases','2'),12,10)) }}
charging_current: >-
{{ max(6,min(max_current,(new_value / 240 /
states('sensor.wallbox_phases') | int(1)) | round(0) )) }}
- if:
- condition: template
value_template: >-
{{ states('number.wallbox_maximum_charging_current') | int(0) !=
charging_current }}
alias: New charging current different
then:
- service: number.set_value
target:
entity_id: number.wallbox_maximum_charging_current
data:
value: "{{ charging_current }}"
alias: Set charging current
- if:
- condition: state
entity_id: switch.wallbox_pause_resume
state: "off"
then:
- delay: "00:00:20"
- service: timer.start
data: {}
target:
entity_id:
- timer.ev_charging_triggered
- service: switch.turn_on
alias: Start charging
data: {}
target:
entity_id:
- switch.wallbox_pause_resume
- service: automation.trigger
alias: >-
Set the invertor mode to prevent charging from backup when power == max
and PV output < 11kW
data:
skip_condition: true
target:
entity_id: automation.handle_battery_charging_mode_ev_meteoalarm
enabled: true
mode: queued
icon: mdi:ev-plug-ccs2
max: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment