-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesla-v2.yaml
831 lines (725 loc) · 31.2 KB
/
tesla-v2.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
###############################################################################
# Disclaimer:
#
# Even though this automation has been created with care, the author cannot be
# responsible for any damages caused by this automation. Use at your own risk.
#
###############################################################################
# Objective:
# - To charge Tesla car using excess solar and weather forecast.
#
# History:
# 20/6/2023 Inspiration from Alphaemef's Tesla Solar Charger Blueprint 30/4/2023.
###############################################################################
blueprint:
name: Tesla night charger automation
description: To charge Tesla car using surplus solar electricity and weather forecast.
domain: automation
###############################################################################
# Blueprint inputs
###############################################################################
input:
#####################################
# Main configs
#####################################
sensor_grid_power_net:
name: Grid power net
description: Sensor that express negative power in Watts when exporting to grid and positive power when consuming from grid. This sensor is the feedback loop for adjusting power consumption.
default: sensor.grid_power_net
selector:
entity:
domain: sensor
# https://github.com/home-assistant/frontend/issues/12281
# toggle_charge_from_grid:
# name: Charge from Grid Toggle
# description: Turn on to charge from grid at constant current.
# default: false
# selector:
# boolean: {}
toggle_charge_from_grid:
name: Charge from grid toggle
description: Toggle on to charge from grid at specified current (Settings > Devices & Services > Helpers > Create helper > Toggle). The default is to charge from solar only.
#default: input_boolean.telsa_model3_charge_from_grid
selector:
entity:
domain: input_boolean
grid_target_power_draw:
name: Charger target power draw when charging from grid
description: Specify target power draw from grid in watt
#default: input_boolean.telsa_model3_charge_from_grid
default: 3500
selector:
number:
min: 0
max: 7200
unit_of_measurement: watt
mode: box
toggle_stop_charger:
name: Stop charger toggle
description: Toggle on to stop charger script (Settings > Devices & Services > Helpers > Create helper > Toggle). Delayed by "Wait grid power net update" value.
#default: input_boolean.tesla_model3_stop_charging
selector:
entity:
domain: input_boolean
charger_voltage:
name: Charger voltage
description: Specify charger voltage in Volt. In Australia, single phase is 230V, and three phase is 400V due to 120 degree phase angle between phases. This can differ between countries.
default: 230
selector:
number:
min: 110
max: 750
unit_of_measurement: volt
mode: box
charger_max_current:
name: Charger maximum current
description: Specify charger maximum current in Ampere.
default: 15
selector:
number:
min: 5
max: 100
unit_of_measurement: amp
mode: box
charger_min_current:
name: Charger minimum current
description: Specify the minimum charging current to keep charging the car when there is no excess solar electricity. The default is 0 Ampere.
default: 0
selector:
number:
min: 0
max: 100
unit_of_measurement: amp
mode: box
wait_grid_power_net_update:
name: Wait grid power net update
description: Specify in seconds how often to check the "sensor_grid_power_net". The waiting period should be longer than the sensor update interval.
default: 60
selector:
number:
min: 1
max: 600
unit_of_measurement: second
mode: box
sunrise_offset_start_trigger:
name: Sunrise offset start trigger
description: Specify time to start charging relative to sunrise, eg. set "+00:30:00" without quotes to start 30 minutes after sunrise.
default: "+00:30:00"
sunset_offset_end_trigger:
name: Sunset offset end trigger
description: Specify time to stop charging relative to sunset, eg. set "-00:30:00" without quotes to end 30 minutes before sunset.
default: "-00:30:00"
#####################################
# Car daily charge limit
#####################################
car_charge_limit_monday:
name: Monday
description: Car charge limit for day (-1 = Use Tesla app charge limit).
default: -1
selector:
number:
min: -1
max: 100
unit_of_measurement: percent
mode: slider
car_charge_limit_tuesday:
name: Tuesday
description: Car charge limit for day (-1 = Use Tesla app charge limit).
default: -1
selector:
number:
min: -1
max: 100
unit_of_measurement: percent
mode: slider
car_charge_limit_wednesday:
name: Wednesday
description: Car charge limit for day (-1 = Use Tesla app charge limit).
default: -1
selector:
number:
min: -1
max: 100
unit_of_measurement: percent
mode: slider
car_charge_limit_thursday:
name: Thursday
description: Car charge limit for day (-1 = Use Tesla app charge limit).
default: -1
selector:
number:
min: -1
max: 100
unit_of_measurement: percent
mode: slider
car_charge_limit_friday:
name: Friday
description: Car charge limit for day (-1 = Use Tesla app charge limit).
default: -1
selector:
number:
min: -1
max: 100
unit_of_measurement: percent
mode: slider
car_charge_limit_saturday:
name: Saturday
description: Car charge limit for day (-1 = Use Tesla app charge limit).
default: -1
selector:
number:
min: -1
max: 100
unit_of_measurement: percent
mode: slider
car_charge_limit_sunday:
name: Sunday
description: Car charge limit for day (-1 = Use Tesla app charge limit).
default: -1
selector:
number:
min: -1
max: 100
unit_of_measurement: percent
mode: slider
#####################################
# Weather forecast
#####################################
charge_car_base_on_weather:
name: Adjust daily car charge limit based on weather
description: Automatically adjust to the highest charge limit set within a rainy forecast period. Charge limit and weather provider settings must be configured.
default: false
selector:
boolean:
weather_provider:
name: Weather provider
description: Weather forecast provider, eg. Meteorologisk institutt (Met.no).
default: weather.forecast_home
selector:
entity:
domain: weather
forecast_index:
name: Forecast days
description: Number of days to forecast weather excluding today.
default: 2
selector:
number:
min: 0
max: 6
mode: slider
unit_of_measurement: day
forecast_attribute:
name: Weather condition field name
description: Field name for forecast attribute (default="condition"). See https://www.home-assistant.io/integrations/weather/ for details.
default: "condition"
selector:
text:
car_charge_limit_bad_weather:
name: Car charge limit on bad weather forecast
description: Charge limit to set on or before bad weather period. This will be used unless there is a higher setting within the forecast period.
default: 90
selector:
number:
min: 0
max: 100
unit_of_measurement: percent
mode: slider
#####################################
# Home battery config (placeholder only)
#####################################
sensor_homebattery_power_net:
name: Home battery power net
description: Sensor reporting net battery power in Watts. This works even if no battery is connected, in which case choose any entity with a non-number state.
default: sensor.shift_state
selector:
entity:
domain: sensor
sensor_homebattery_level:
name: Home battery charge level
description: Sensor reporting home battery charge level in percentage. If there is no homebattery_level entity, choose any entity with a non-number state.
default: sensor.shift_state
selector:
entity:
domain: sensor
#####################################
# Tesla config entities
#####################################
car_location_tracker:
name: Tesla location tracker
description: The entity that provides the location of car. Important that home location is defined in Home Assistant.
default: device_tracker.location_tracker
selector:
entity:
domain: device_tracker
car_button_wake_up:
name: Tesla button wake up
description: Button to wake up car.
default: button.wake_up
selector:
entity:
domain: button
car_binary_sensor_charger_pluggedin:
name: Tesla binary sensor charger
description: Sensor to check if car is plugged in.
default: binary_sensor.charger
selector:
entity:
domain: binary_sensor
car_switch_charger:
name: Tesla switch charger
description: Switch to turn charger on or off.
default: switch.charger
selector:
entity:
domain: switch
car_binary_sensor_charging:
name: Tesla binary sensor charging
description: Sensor to check if car is charging.
default: binary_sensor.charging
selector:
entity:
domain: binary_sensor
car_number_charging_amps:
name: Tesla charging amps
description: Entity to control current in Ampere used to charge the car battery. This is the main parameter used to adjust electricity consumption.
default: number.charging_amps
selector:
entity:
domain: number
car_number_charge_limit:
name: Tesla charge limit
description: Entity to control car battery charge limit in percentage (100% = fully charged).
default: number.charge_limit
selector:
entity:
domain: number
car_sensor_battery:
name: Tesla sensor battery
description: Sensor that reports current battery charge level.
default: sensor.battery
selector:
entity:
domain: sensor
car_button_forceupdate_ha:
name: Tesla button force update Home Assistant
description: Button to force update sensors in Home Assistant with values from car.
default: button.force_data_update
selector:
entity:
domain: button
###############################################################################
# Global variables
# https://community.home-assistant.io/t/why-the-heck-dont-we-have-global-variables/219671/86
# https://github.com/rogro82/hass-variables
###############################################################################
variables:
#####################################
# Constants
#####################################
# Tesla UMC charger initial minimum charging current is 5A, after that it can vary between 0A and 15A.
initial_charge_current: 6
#max_allowable_current_delta: 6
# Time to wait in seconds for car to wake up.
wait_car_wakeup: 40
# Time to wait in seconds to turn charger on or off.
# To switch on charger from 2V to 230V and then ram up current, it took 16 seconds for initial_charge_current 15A.
#wait_charger_on: 16
# To switch on charger from 2V to 230V and then ram up current, it took 11 seconds for initial_charge_current 6A.
wait_charger_on: 11
wait_charger_off: 5
# Time to wait in seconds for current to change after sending command.
#wait_current_change: "00:00:0.3"
wait_current_change: 0.3
# Time to wait in seconds for command to execute after sending command.
wait_command_execute: 5
#wait_command_execute_str: "00:00:{{ wait_command_execute }}"
# "Skew_default" defines the right / left skew of the import/export curve. I.e a skew of 0.0 will increase current draw
# as soon as power to grid turns negative, and reduces current when power goes higher than 230W.
# i.e it will aim at power draw in the range [0-230W[. A skew of -1 will aim at a power from grid of [-230;0[
# Should be kept between -1 and 0.
skew_default: -0.3
#####################################
# Inputs
#####################################
charge_from_grid: !input toggle_charge_from_grid
stop_charger: !input toggle_stop_charger
car_charger_voltage: !input charger_voltage
car_charger_max_current: !input charger_max_current
car_charger_min_current: !input charger_min_current
grid_target_power_draw: !input grid_target_power_draw
# Power = Voltage * Current
# The power step in watts to increase or decrease with 1A current change.
one_amp_watt_step: "{{ car_charger_voltage * 1 }}"
# This worked ok.
#tesla_model3_charge_level: "sensor.battery"
#tesla_model3_charge_level: !input car_sensor_battery
car_chargelimit_ent: !input car_number_charge_limit
car_chargelimit_last: '{{ expand(car_chargelimit_ent)[0].last_changed | as_timestamp }}'
car_chargelimit_val: '{{ expand(car_chargelimit_ent)[0].state }}'
car_batterylevel_ent: !input car_sensor_battery
car_batterylevel_last: '{{ expand(car_batterylevel_ent)[0].last_changed | as_timestamp }}'
car_batterylevel_val: '{{ expand(car_batterylevel_ent)[0].state }}'
car_charging_amps: !input car_number_charging_amps
#####################################
# Set car charge limit based on which week day
#####################################
mon: !input car_charge_limit_monday
tue: !input car_charge_limit_tuesday
wed: !input car_charge_limit_wednesday
thu: !input car_charge_limit_thursday
fri: !input car_charge_limit_friday
sat: !input car_charge_limit_saturday
sun: !input car_charge_limit_sunday
# Store as integer list
car_charge_limit_list: '{{ [mon, tue, wed, thu, fri, sat, sun] }}'
todayIndex: '{{ now().weekday() }}'
todayChargeLimit: '{{ car_charge_limit_list[todayIndex] }}'
#####################################
# Set car charge limit based on weather forecast
#####################################
charge_car_base_on_weather: !input charge_car_base_on_weather
weather_provider: !input weather_provider
forecast_index: !input forecast_index
forecast_attribute: !input forecast_attribute
carChargeLimitBadWeather: !input car_charge_limit_bad_weather
# Not all intervals are available for all providers. Possible values: "hourly", "twice_daily", "daily"
forecast_interval: "daily"
# Charge limit list for the next N days excluding today. Also take into account carChargeLimitBadWeather.
periodChargeLimitList: >
{% set data = namespace(numbers=[carChargeLimitBadWeather]) %}
{% for i in range(1, forecast_index + 1, 1) %}
{% set day = (todayIndex + i) % 7 %}
{% set data.numbers = data.numbers + [car_charge_limit_list[day]] %}
{% endfor %}
{{ data.numbers }}
periodMaxChargeLimit: '{{ periodChargeLimitList | max }}'
###############################################################################
# Triggers
# Note:
# - Tesla triggers do not update often due to infrequent polling.
###############################################################################
trigger:
#- platform: numeric_state
# entity_id: !input sensor_grid_power_net
# below: 300
#- platform: numeric_state
# entity_id: !input sensor_grid_power_net
# below: 0
# Trigger action if just plugged in.
- platform: state
entity_id:
- !input car_binary_sensor_charger_pluggedin
to: "on"
# Disabled this trigger to allow off-peak night time charging.
# Trigger action if already charging.
#- platform: state
# entity_id: !input car_binary_sensor_charging
# to: "on"
condition:
- condition: state
entity_id: !input car_location_tracker
state: home
- condition: state
entity_id: !input car_binary_sensor_charger_pluggedin
state: "on"
###############################################################################
# Action
###############################################################################
action:
# Wake up Tesla.
- service: button.press
data: {}
target:
entity_id: !input car_button_wake_up
- delay: "{{ wait_car_wakeup }}"
# Get the latest data from Tesla to update HA.
- service: button.press
data: {}
target:
entity_id: !input car_button_forceupdate_ha
- delay: "{{ wait_command_execute }}"
# Save current Tesla parameters for later restore.
- service: scene.create
data:
scene_id: tesla_solar_charger_script_scene
snapshot_entities:
- !input car_number_charge_limit
- !input car_number_charging_amps
- delay: "{{ wait_command_execute }}"
# Should honour existing charge limit set by app or car, unless charge limit for day is set.
# If charge limit for day is not set, ie. set to -1, charge limit will be set according to the Tesla app.
# If charge limit for day is set and charge car based on weather is disabled, charge limit will be set according to the limit configured for the day.
# If charge limit for day is set and charge car based on weather is enabled, charge limit will be adjusted to the highest limit set within the rainy forecast period taking into account the car charge limit on bad weather setting.
# If charge car based on weather is enabled, charge limit and weather provider settings must be configured.
- choose:
- conditions: >
{{ todayChargeLimit >= 0 and todayChargeLimit <= 100 }}
sequence:
- choose:
- conditions: >
{{ charge_car_base_on_weather }}
sequence:
# Get weather forecast data
- service: weather.get_forecasts
target:
entity_id: !input weather_provider
data:
type: "{{forecast_interval}}"
response_variable: weather_forecast
- variables:
periodForecastList: >
{% set data = namespace(strings=[]) %}
{% for day in range(1, forecast_index + 1, 1) %}
{% set data.strings = data.strings + [weather_forecast[weather_provider]['forecast'][day][forecast_attribute]] %}
{% endfor %}
{{ data.strings }}
# See https://www.home-assistant.io/integrations/weather/
# Some possible values: rainy, cloudy, partlycloudy
finalChargeLimit: >-
{% if 'rainy' in periodForecastList and periodMaxChargeLimit > todayChargeLimit %}
{{ periodMaxChargeLimit }}
{% else %}
{{ todayChargeLimit }}
{% endif %}
# Show current status
- service: system_log.write
data_template:
message: |-
{{now()}}:
finalChargeLimit={{finalChargeLimit}}, todayChargeLimit={{todayChargeLimit}}, carChargeLimitBadWeather={{carChargeLimitBadWeather}}, periodMaxChargeLimit={{periodMaxChargeLimit}},
periodChargeLimitList={{periodChargeLimitList}}, periodForecastList={{periodForecastList}},
todayIndex={{todayIndex}}, car_charge_limit_list={{car_charge_limit_list}}, weather_today={{ weather_forecast[weather_provider]['forecast'][0][forecast_attribute] }}
#level: warning
level: info
- service: number.set_value
data:
value: "{{ finalChargeLimit }}"
target:
entity_id: !input car_number_charge_limit
- delay: "{{ wait_command_execute }}"
default:
- service: number.set_value
data:
value: "{{ todayChargeLimit }}"
target:
entity_id: !input car_number_charge_limit
- delay: "{{ wait_command_execute }}"
#####################################
# Main while loop
#####################################
- repeat:
# Only start charging when following conditions are met, otherwise exit script.
while:
- condition: and
conditions:
# Continue if stop button has not been recently pressed.
# https://community.home-assistant.io/t/repeat-until-input-button-is-pressed/429300
# input_button is a state with timestamp of button-press event.
# https://community.home-assistant.io/t/strptime-vs-as-datetime/381500/6
# Got follow error if button was newly created and was never pressed before. Work-around is just to press button once or set default(0, true).
# Error: In 'template' condition: TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'NoneType'
#- condition: template
# value_template: "{{ now() - (states(stop_charger_button_press) | default(0, true) | as_datetime) > timedelta(seconds=(wait_command_execute * 3 + loop_wait_time)) }}"
# Continue if stop button is toggled off.
- condition: template
value_template: "{{ is_state(stop_charger, 'off') }}"
# Continue if charger is plugged in.
- condition: state
entity_id: !input car_binary_sensor_charger_pluggedin
state: "on"
# Continue if battery is more than 1% below charge limit.
# https://community.home-assistant.io/t/configuring-variables-into-templates-in-a-blueprint/323811/2
# OK to charge 1% less because Tesla can stop charging at 1% less by itself.
#- condition: template
# value_template: "{{ (expand(car_batterylevel_ent)[0].state | int + 1) < (expand(car_chargelimit_ent)[0].state | int) }}"
# Continue if battery is below charge limit.
# https://community.home-assistant.io/t/configuring-variables-into-templates-in-a-blueprint/323811/2
- condition: template
value_template: "{{ (expand(car_batterylevel_ent)[0].state | int) < (expand(car_chargelimit_ent)[0].state | int) }}"
- condition: template
value_template: "{{ is_state(charge_from_grid, 'on') }}"
sequence:
- variables:
# For Enphase, grid_power_net is an integer in watts. Positive value means importing power from grid. Negative value means exporting power to grid.
grid_power_net: !input sensor_grid_power_net
skew: >-
{% if skew_default <= -1 %}
{{ skew_default }}
{% else %}
{{ skew_default }}
{% endif %}
- variables:
# Net power from all sources (default 0 set by float(0) if cannot convert to float).
all_power_net: "{{ states(grid_power_net) | float(0) }}"
# This can be greater than car_charger_max_current.
# +ve => importing amp from grid, -ve => exporting amp to grid
all_current_net: "{{ (all_power_net / one_amp_watt_step) | float }}"
target_current_net: "{{ (grid_target_power_draw / one_amp_watt_step) | float }}"
# Charge current for this sequence run, including sanity check to ensure within limits.
charge_current: >-
{% if states(car_charging_amps) | int < 0 %}
{{ 0 }}
{% elif states(car_charging_amps) | int > car_charger_max_current %}
{{ car_charger_max_current }}
{% else %}
{{ states(car_charging_amps) | int }}
{% endif %}
target_current_charge: "{{ target_current_net - (all_current_net - charge_current) | float }}"
# If importing from grid, will try to decrease charge current by same consumption or min charger current.
# If exporting to grid, will try to increase charge current by same production or max charger current.
#propose_charge_current: >-
new_charge_current: >-
{% if target_current_charge > car_charger_min_current %}
{{ [car_charger_max_current, target_current_charge] | min | round(0) }}
{% else %}
{{ [car_charger_min_current, target_current_charge] | max | round(0) }}
{% endif %}
- choose:
# First time run, turn on charger if not turned on already.
- conditions:
- condition: template
value_template: "{{ repeat.index == 1 }}"
sequence:
- choose:
- conditions:
- condition: state
entity_id: !input car_binary_sensor_charging
state: "off"
sequence:
# Only set initial current if charging from solar, otherwise charge from grid using current set by user enforcing current limits.
- if:
- condition: template
value_template: "{{ is_state(charge_from_grid, 'off') }}"
then:
- service: number.set_value
data:
value: "{{ initial_charge_current }}"
target:
entity_id: !input car_number_charging_amps
else:
- service: number.set_value
data:
value: "{{ charge_current }}"
target:
entity_id: !input car_number_charging_amps
- delay: "{{ wait_command_execute }}"
# Turn on charger. Rate of current increase is handled by Tesla car.
- service: switch.turn_on
data: {}
target:
entity_id: !input car_switch_charger
- delay: "{{ wait_charger_on }}"
# Exit script if charger is turned off, ie. not charging.
- conditions:
- condition: state
entity_id: !input car_binary_sensor_charging
state: "off"
sequence:
# Restore previous settings.
- service: scene.turn_on
target:
entity_id: scene.tesla_solar_charger_script_scene
- delay: "{{ wait_command_execute }}"
# Get the latest data from Tesla to update HA before exiting script.
- service: button.press
data: {}
target:
entity_id: !input car_button_forceupdate_ha
- delay: "{{ wait_command_execute }}"
- stop: "Exiting script due to charger turned off manually or automatically by car when charge limit reached."
# https://www.home-assistant.io/docs/scripts/#stopping-a-script-sequence
# Cannot get response_variable to work. Needs to be key/value pair?
#response_variable: "stop_charging_reason"
# Toggled to charge from grid at current specified in app.
- conditions:
- condition: template
value_template: "{{ is_state(charge_from_grid, 'on') }}"
sequence:
- delay: "00:00:00"
# all_power_net < -69W, ie. need to increase charger current.
- conditions:
- condition: template
value_template: "{{ all_power_net > (0 + skew) * one_amp_watt_step }}"
sequence:
# Gradually increase current with 1A step.
- repeat:
while: "{{ (charge_current + repeat.index) <= new_charge_current }}"
sequence:
- service: number.set_value
data_template:
value: "{{ charge_current + repeat.index }}"
target:
entity_id: !input car_number_charging_amps
- delay: "{{ wait_current_change }}"
# all_power_net >= 161W, ie. need to decrease charger current.
- conditions:
- condition: template
value_template: "{{ all_power_net >= (1 + skew) * one_amp_watt_step }}"
sequence:
# Gradually decrease current with 1A step.
- repeat:
while: "{{ (charge_current - repeat.index) >= new_charge_current }}"
sequence:
- service: number.set_value
data_template:
value: "{{ charge_current - repeat.index }}"
target:
entity_id: !input car_number_charging_amps
- delay: "{{ wait_current_change }}"
# 161 > all_power_net >= -69W, ie. do nothing.
- conditions:
- condition: template
value_template: "{{ (1 + skew) * one_amp_watt_step > all_power_net >= (0 + skew) * one_amp_watt_step }}"
sequence:
- delay: "00:00:00"
# Get the latest data from Tesla to update HA.
- service: button.press
data: {}
target:
entity_id: !input car_button_forceupdate_ha
- delay: "{{ wait_command_execute }}"
# Sleep to ensure HA is updated before the next read.
- delay:
hours: 0
minutes: 0
seconds: !input wait_grid_power_net_update
milliseconds: 0
#####################################
# Exit script
#####################################
- if:
- condition: state
entity_id: !input car_binary_sensor_charging
state: "on"
then:
# Gradually decrease to 0A with 1A step, otherwise Tesla car can decrease current from 15A -> 7A -> 0A in 1 second.
- variables:
charge_current: "{{ states(car_charging_amps) | int }}"
- repeat:
while: "{{ (charge_current - repeat.index) >= 0 }}"
sequence:
- service: number.set_value
data_template:
value: "{{ charge_current - repeat.index }}"
target:
entity_id: !input car_number_charging_amps
- delay: "{{ wait_current_change }}"
- delay: "{{ wait_command_execute }}"
# Turn off charger.
- service: switch.turn_off
data: {}
target:
entity_id: !input car_switch_charger
- delay: "{{ wait_charger_off }}"
# Restore previous settings.
- service: scene.turn_on
target:
entity_id: scene.tesla_solar_charger_script_scene
- delay: "{{ wait_command_execute }}"
# Get the latest data from Tesla to update HA before exiting script.
- service: button.press
data: {}
target:
entity_id: !input car_button_forceupdate_ha
- delay: "{{ wait_command_execute }}"
mode: single