-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharging.yaml
543 lines (524 loc) · 18.3 KB
/
charging.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
#
# Don't allow the charging amps to go to zero
#
homeassistant:
customize:
number.jess_charging_amps:
min: 1
#
# [Switch] enable/disable smart charging
#
input_boolean:
smart_charge:
name: "[Tesla Smart Charge] Smart Charge"
initial: on
icon: mdi:battery-charging-60
#
# [Number] The amount of time to wait between triggers
#
input_number:
smart_charge_delay:
name: "[Tesla Smart Charge] Smart Charge Delay"
unit_of_measurement: s
initial: 30
min: 10
max: 600
step: 1
icon: mdi:car-clock
mode: box
#
# [Number] The amount of overrun allowed before disabling charge
#
smart_charge_buffer:
name: "[Tesla Smart Charge] Charge Overrun Buffer"
icon: mdi:battery-charging-60
unit_of_measurement: w
min: 0
max: 500
step: 50
initial: 100
#
# [Timer] Delay stopping charge
#
# When an automation calls to turn the charger off, we delay it by 2 minutes.
# This stops the charger's relay from clicking on and off constantly if the available power is bouncing around
# as the timer is cancelled when power becomes available again within the 2 minutes.
#
timer:
turn_off_car_charger:
name: "[Tesla Smart Charge] Timer to turn off car charger in 3 minutes"
duration: "00:03:02"
icon: mdi:timer-sand
#
# [Automation] Triggered when the timer to turn off the charger ends.
#
automation turn_off_car_charger_automation:
id: "8379465214871"
alias: "[Tesla Smart Charge] Turning off car charger"
description: ''
mode: single
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.turn_off_car_charger
action:
- service: switch.turn_off
target:
entity_id: switch.jess_charger
data: {}
#
# [Automation] Turn charger off immediately after plugging in
#
automation charging_off_when_plugged_in:
id: "6485463543436"
alias: "[Tesla Smart Charge] Turn car charger off after being plugged in"
description: ""
mode: single
trigger:
- platform: state
entity_id: binary_sensor.jess_charger
to: "on"
from: "off"
condition:
# Car must be at home
- condition: state
entity_id: device_tracker.jess_location_tracker
state: home
action:
- service: timer.start
target:
entity_id: timer.turn_off_car_charger
data:
duration: "00:01:00"
#
# Smart Charging
#
automation charging:
id: "8379465214376"
alias: "[Tesla Smart Charge] Charge on solar power"
description: ""
mode: single
trace:
stored_traces: 30
trigger:
- platform: state
entity_id: sensor.power_in_or_out_reversed
condition:
# The sun must be out
- condition: state
entity_id: sun.sun
state: above_horizon
# Wait at least smart_charge_delay(10) seconds
- condition: template
value_template: >
{% set last = this.attributes.last_triggered %}
{% set delay = states('input_number.smart_charge_delay') | int(10) %}
{{ last is none or now() > last + timedelta(seconds = delay) }}
# Smart charge must be enabled
- condition: state
entity_id: input_boolean.smart_charge
state: "on"
# Car must be plugged in
- condition: state
entity_id: binary_sensor.jess_charger
state: "on"
# Car must be at home
- condition: state
entity_id: device_tracker.jess_location_tracker
state: home
action:
# Setup some variables
- variables:
min_charging_amps: 1
minimum_start_amps: 2
max_charging_amps: "{{ state_attr('number.jess_charging_amps', 'max') | int }}"
current_charging_amps: "{{ states('number.jess_charging_amps') | int }}"
spare_energy: "{{ states('sensor.power_in_or_out_reversed') | float(0) }}"
grid_voltage: "{{ 240 | float * 3 }}"
spare_amps: "{{ (spare_energy / grid_voltage) | round(0, 'floor') | int }}"
new_charge_amps: "{{ (current_charging_amps + spare_amps) | int }}"
charge_limit: "{{ states('number.jess_charge_limit') | int }}"
battery_level: "{{ states('sensor.jess_battery') | int }}"
overrun_buffer: "{{ states('input_number.smart_charge_buffer') | float(0) * -1 }}"
- choose:
- conditions:
#
# Condition: Car is not currently charging and we have enough spare amps
# Car is also under the max charge level
# Car is asleep
#
- condition: template
value_template: "{{ spare_amps >= minimum_start_amps }}"
- condition: state
entity_id: switch.jess_charger
state: 'off'
- condition: template
value_template: "{{ charge_limit > battery_level }}"
- condition: state
entity_id: binary_sensor.jess_asleep
state: "on"
alias: Can start charge but car is asleep
sequence:
#
# Do:
# Log to Logbook
# Send Wake Up command to car
#
- service: logbook.log
data:
name: "[Tesla Smart Charge]"
message: "Car is asleep, attempting to wake."
- service: button.press
target:
entity_id: button.jess_wake_up
- conditions:
#
# Condition: Car is not currently charging and we have enough spare amps
# Car is also under the max charge level
#
- condition: template
value_template: "{{ spare_amps >= minimum_start_amps }}"
- condition: state
entity_id: switch.jess_charger
state: 'off'
- condition: template
value_template: "{{ charge_limit > battery_level }}"
alias: Ready to start charging
sequence:
#
# Do:
# Log to Logbook
# Cancel any 'charger turn off' timer, but only if it is running
# Set number of amps to use
# Turn car charger on
#
- service: logbook.log
data:
name: "[Tesla Smart Charge]"
message: "Charger is off and we have {{ spare_amps }} spare amps. Attempting to start charge"
# Check that the timer is running, otherwise it will trigger a finish
- choose:
- conditions:
- condition: state
entity_id: timer.turn_off_car_charger
state: active
sequence:
- service: timer.cancel
target:
entity_id: timer.turn_off_car_charger
- service: number.set_value
target:
entity_id: number.jess_charging_amps
data:
value: "{{ spare_amps }}"
- service: switch.turn_on
target:
entity_id: switch.jess_charger
- conditions:
#
# Condition: Car is charging, we have spare energy and we aren't at the maximum charge amps yet
#
- condition: state
entity_id: switch.jess_charger
state: 'on'
- condition: template
value_template: "{{ spare_amps > 0 }}"
- condition: template
value_template: "{{ current_charging_amps < max_charging_amps }}"
# - condition: template
# value_template: "{{ 1 < 0 }}" ##### TODO: REMOVE
alias: Charging and can increase charge rate
sequence:
#
# Do:
# Log to Logbook
# Cancel any 'charger turn off' timer, but only if it is running
# Set the new charge amps
#
- service: logbook.log
data:
name: "[Tesla Smart Charge]"
message: "Charger is on and we have {{ spare_amps }} spare amps. Already at {{ current_charging_amps }} amps. Attempting to increase charge rate to {{ (current_charging_amps + spare_amps) | int }} amps"
# Check that the timer is running, otherwise it will trigger a finish
- choose:
- conditions:
- condition: state
entity_id: timer.turn_off_car_charger
state: active
sequence:
- service: timer.cancel
target:
entity_id: timer.turn_off_car_charger
- service: number.set_value
target:
entity_id: number.jess_charging_amps
data:
value: "{{ [max_charging_amps, new_charge_amps] | min }}"
- conditions:
#
# TURN OFF?
#
# Condition: Car is charging and we don't even have enough juice to charge even at min_charging_amps
# We also make sure that we are above our overrun_buffer
#
- alias: Charger is on
condition: state
entity_id: switch.jess_charger
state: 'on'
- alias: new_charge_amps < min_charging_amps
condition: template
value_template: "{{ new_charge_amps < min_charging_amps }}"
alias: Charging and need to stop!
sequence:
#
# Do:
# Log to Logbook
# Reduce amps to min_charging_amps if not already at min_charging_amps
# Run timer that delays for 3 minutes then turns off the charger, but only if it isn't already running
#
- service: logbook.log
data:
name: "[Tesla Smart Charge]"
message: "Charger is on and we don't have enough juice to even charge at {{ min_charging_amps }}A. Turning off charger in 2 minutes. (new_charge:{{ new_charge_amps }}A < min_charging:{{ min_charging_amps }}A) (current_charging:{{ current_charging_amps }}A + spare:{{ spare_amps }}A) (power out:{{ spare_energy }})"
# Only set charge rate if we need to
- alias: Set minimum charge rate if not already
if:
- condition: template
value_template: "{{ current_charging_amps != min_charging_amps }}"
then:
- service: number.set_value
target:
entity_id: number.jess_charging_amps
data:
value: "{{ min_charging_amps }}"
- alias: Start or Cancel the timer that switches off the charger
if:
- alias: If spare_energy < overrun_buffer
condition: template
value_template: "{{ spare_energy < overrun_buffer }}"
then:
# Check that the timer isn't already running, otherwise it will restart
- alias: Start the timer
choose:
- alias: Make sure the stop timer isn't already running
conditions:
- condition: or
conditions:
- condition: state
entity_id: timer.turn_off_car_charger
state: idle
- condition: state
entity_id: timer.turn_off_car_charger
state: paused
sequence:
- service: timer.start
target:
entity_id: timer.turn_off_car_charger
data:
duration: "00:03:02"
else:
# Disable time if running
- alias: Stop the timer
choose:
- alias: Check if the timer is running
conditions:
- condition: state
entity_id: timer.turn_off_car_charger
state: active
sequence:
- service: timer.cancel
target:
entity_id: timer.turn_off_car_charger
- conditions:
#
# Condition: Car is charging and we are using too much energy
#
- condition: state
entity_id: switch.jess_charger
state: 'on'
- condition: template
value_template: "{{ spare_amps < 0 }}"
alias: Charging but need to lower charge rate
sequence:
#
# Do:
# Log to Logbook
# Cancel any 'charger turn off' timer, but only if it is running
# Reduce the number of amps
#
- service: logbook.log
data:
name: "[Tesla Smart Charge]"
message: "Charger is on and we are using {{ spare_amps }} too many amps. Attempting to decrease charge rate to:{{ [min_charging_amps, new_charge_amps] | max }}"
- choose:
- conditions:
- condition: state
entity_id: timer.turn_off_car_charger
state: active
sequence:
- service: timer.cancel
target:
entity_id: timer.turn_off_car_charger
- service: number.set_value
target:
entity_id: number.jess_charging_amps
data:
# spare_amps will be negative
value: "{{ [min_charging_amps, new_charge_amps] | max }}"
default:
#
# Do:
# Disable the shut off timer (We could be in a state where we are charging at 1A, only have the 1A available, but the timer was previously triggered)
#
- choose:
- conditions:
- condition: state
entity_id: timer.turn_off_car_charger
state: active
alias: Cancel timer that disables charger
sequence:
- service: timer.cancel
target:
entity_id: timer.turn_off_car_charger
# - service: logbook.log
# data:
# name: "[Tesla Smart Charge]"
# message: "We have nothing to do. Charger is {{ states.switch.jess_charger.state }} - current_charging_amps:{{ current_charging_amps }} - spare_energy:{{ spare_energy }} - spare_amps:{{ spare_amps }} - minimum_start_amps:{{ minimum_start_amps }}"
#
# Turn Off at Sunset
#
automation stop_charging:
id: "8379365214876"
alias: "[Tesla Smart Charge] Turn charger off at sunset"
description: "If smart charging is enabled, charging will be switched off at sunset"
mode: single
trigger:
# Perform at sunset
- platform: sun
event: sunset
condition:
# Smart charge must be enabled
- condition: state
entity_id: input_boolean.smart_charge
state: "on"
# Car must be plugged in
- condition: state
entity_id: binary_sensor.jess_charger
state: "on"
# Car must be at home
- condition: state
entity_id: device_tracker.jess_location_tracker
state: home
action:
- service: logbook.log
data:
name: "[Tesla] Smart Charging"
message: "It is sunset and the car charger is currently on, so attempting to turn it off"
- service: switch.turn_off
target:
entity_id: switch.jess_charger
#
# Notification when car is plugged in
#
automation notification_on_plugged_in:
id: "8379465214876"
alias: "[Tesla Smart Charge] Ask user for smart charging when charge port connector connects"
description: ""
mode: single
trigger:
# When the car is plugged in
- platform: state
entity_id: binary_sensor.jess_charger
to: "on"
from: "off"
condition:
# Car must be at home
- condition: state
entity_id: device_tracker.jess_location_tracker
state: home
# The sun must be out
- condition: state
entity_id: sun.sun
state: above_horizon
# Smart charge must be disabled
- condition: state
entity_id: input_boolean.smart_charge
state: "off"
action:
- service: notify.notify
data:
message: Enable smart charging?
data:
actions:
- action: enable_smart_charging
title: Enable
- action: disable_smart_charging
title: Disable
#
# Notification actions
#
automation notification_actions:
id: "8379465214877"
alias: "[Tesla Smart Charge] Smart charging iOS actions"
description: ""
trigger:
- platform: event
event_type: ios.notification_action_fired
event_data:
actionName: enable_smart_charging
- platform: event
event_type: ios.notification_action_fired
event_data:
actionName: disable_smart_charging
action:
- choose:
- conditions:
- condition: template
value_template: "{{ trigger.event.data.actionName == 'enable_smart_charging' }}"
sequence:
- service: input_boolean.turn_on
target:
entity_id: input_boolean.smart_charge
data: {}
- conditions:
- condition: template
value_template: "{{ trigger.event.data.actionName == 'disable_smart_charging' }}"
sequence:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.smart_charge
data: {}
#
# Notification when car isn't charging but we have lots of excess power
#
automation notification_solar_surplus:
id: "3846592348321"
alias: "[Tesla Smart Charge] iOS notify upon solar power production surplus"
description: ""
trigger:
- platform: numeric_state
entity_id: sensor.power_in_or_out_reversed
above: 750
for:
minutes: 5
condition:
# Smart charging is disabled
- condition: state
entity_id: input_boolean.smart_charge
state: "off"
# Car must be at home
- condition: state
entity_id: device_tracker.jess_location_tracker
state: home
- condition: sun
before: sunset
after: sunrise
action:
- service: notify.notify
data:
message: Excessive solar power production, enable smart charging?
data:
actions:
- action: enable_smart_charging
title: Enable