-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator-ats.yaml
More file actions
554 lines (468 loc) · 12.5 KB
/
generator-ats.yaml
File metadata and controls
554 lines (468 loc) · 12.5 KB
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
esphome:
name: generator-ats
friendly_name: Generator ATS
name_add_mac_suffix: false
comment: "Auto and manual start generator"
project:
name: "ats.generator"
version: 1.0.0
build_path: /config/esphome/build
on_boot:
- priority: 800
then:
- switch.turn_off: starter
- switch.turn_off: kill_sw
- priority: -100
then:
- script.execute: update_led
- priority: -50
then:
- if:
condition:
binary_sensor.is_on: gen_run
then:
- script.execute:
id: set_state
new_state: 2
esp32:
board: esp32-c6-devkitc-1
framework:
type: esp-idf
sdkconfig_options:
COMPILER_OPTIMIZATION_PERF: y
CONFIG_LOG_DEFAULT_LEVEL: "3"
CONFIG_LWIP_IPV6: n
CONFIG_BT_ENABLED: n
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: "${node_name} Fallback"
password: !secret ap_password
power_save_mode: none
captive_portal:
packages:
# You can replace next 2 lines with your common configs for all devices
# common_system: !include includes/common_system.yaml
common_sensors: !include includes/common_sensors.yaml
ota:
platform: esphome
logger:
# id: uart_logger
# baud_rate: 0
level: DEBUG #VERBOSE
logs:
component: ERROR # Fix for issue #4717 "Component xxxxxx took a long time for an operation"
time:
- platform: homeassistant
id: esptime
web_server:
port: 80
local: true
version: 3
auth:
username: !secret web_server_username
password: !secret web_server_password
# ota: false
api:
# ---------------- НАСТРОЮВАНІ ПАРАМЕТРИ ----------------
number:
- platform: template
name: Starter Time
id: starter_time
icon: mdi:timer-outline
min_value: 1
max_value: 10
step: 1
initial_value: 5
optimistic: true
- platform: template
name: Start Attempts
id: max_attempts
icon: mdi:counter
min_value: 1
max_value: 5
step: 1
initial_value: 3
optimistic: true
- platform: template
name: Start Confirm Time
id: start_confirm_time
icon: mdi:timer-check-outline
min_value: 5
max_value: 20
step: 1
initial_value: 10
optimistic: true
- platform: template
name: Auto Stop Delay
id: auto_stop_delay
icon: mdi:timer-stop-outline
min_value: 10
max_value: 180
step: 5
initial_value: 60
optimistic: true
- platform: template
name: Fail Reset Run Time
id: fail_reset_run_time
icon: mdi:timer-refresh-outline
min_value: 10
max_value: 300
step: 10
initial_value: 60
optimistic: true
# ---------------- ГЛОБАЛЬНІ ----------------
globals:
- id: gen_state
type: int
initial_value: '0'
- id: start_attempt
type: int
initial_value: '0'
- id: restart_used
type: bool
initial_value: 'false'
- id: run_start_time
type: unsigned long
initial_value: '0'
- id: gen_run_seconds
type: unsigned long
initial_value: '0'
- id: stop_delay_active
type: bool
initial_value: 'false'
# ---------------- GPIO ----------------
switch:
- platform: gpio
pin: GPIO10
id: starter
icon: mdi:engine
restore_mode: ALWAYS_OFF
- platform: gpio
pin: GPIO11
id: kill_sw
icon: mdi:engine-off
- platform: template
name: AUTO MODE
id: auto_mode
icon: mdi:auto-mode
optimistic: true
on_turn_on:
- script.stop: auto_stop
- lambda: 'id(stop_delay_active) = false;'
- if:
condition:
binary_sensor.is_on: gen_run
then:
- script.execute:
id: set_state
new_state: 2
else:
- script.execute: auto_start
on_turn_off:
- script.stop: start_sequence
- switch.turn_off: starter
- if:
condition:
binary_sensor.is_on: gen_run
then:
- script.execute: auto_stop
else:
- script.execute: stop_now
binary_sensor:
- platform: gpio
pin:
number: GPIO2
mode: INPUT_PULLDOWN
id: gen_run
icon: mdi:engine
device_class: running
filters:
- delayed_on: 200ms
- delayed_off: 100ms
on_press:
- lambda: |-
id(run_start_time) = millis();
id(start_attempt) = 0;
id(stop_delay_active) = false;
- script.execute:
id: set_state
new_state: 2
on_release:
- if:
condition:
lambda: 'return id(gen_state) == 2 && !id(stop_delay_active);'
then:
- script.execute: stop_sequence
else:
- lambda: 'id(stop_delay_active) = false;'
- if:
condition:
lambda: 'return id(gen_state) != 3;'
then:
- script.execute:
id: set_state
new_state: 0
- platform: gpio
pin:
number: GPIO3
mode: INPUT_PULLUP
inverted: true
id: dry_contact
name: Auto Start Contact
icon: mdi:electric-switch
filters:
- delayed_on: 1s
- delayed_off: 1s
on_press:
- script.stop: auto_stop
- script.execute: auto_start
on_release:
- script.stop: start_sequence
- switch.turn_off: starter
- script.execute: auto_stop
# ---------------- СТАН ТЕКСТОМ ----------------
text_sensor:
- platform: template
name: Generator State
id: generator_state_text
icon: mdi:state-machine
update_interval: never
lambda: |-
if (id(gen_state) == 0) return {"WAIT"};
if (id(gen_state) == 1) return {"STARTING"};
if (id(gen_state) == 2) return {"RUN"};
if (id(gen_state) == 3) return {"FAIL"};
if (id(gen_state) == 4) return {"COOLING"};
return {"UNKNOWN"};
- platform: template
name: "Generator Runtime"
icon: "mdi:clock-outline"
lambda: |-
int total_s = id(gen_run_seconds);
int hours = total_s / 3600;
int minutes = (total_s % 3600) / 60;
int seconds = total_s % 60;
char buffer[20];
if (hours > 0) {
sprintf(buffer, "%dh %dm", hours, minutes);
} else if (minutes > 0) {
sprintf(buffer, "%dm %ds", minutes, seconds);
} else {
sprintf(buffer, "%ds", seconds);
}
return (std::string) buffer;
update_interval: 5s
# ---------------- СКРИПТИ ----------------
script:
- id: set_state
parameters:
new_state: int
then:
- lambda: |-
id(gen_state) = new_state;
- component.update: generator_state_text
- script.execute: update_led
- id: start_sequence
mode: restart
then:
- if:
condition:
binary_sensor.is_on: gen_run
then:
- script.execute:
id: set_state
new_state: 2
- script.stop: start_sequence
- lambda: 'id(start_attempt) += 1;'
- script.execute:
id: set_state
new_state: 1
- switch.turn_on: starter
- wait_until:
condition:
binary_sensor.is_on: gen_run
timeout: !lambda "return id(starter_time).state * 1000;"
- switch.turn_off: starter
- delay: !lambda "return id(start_confirm_time).state * 1000;"
- if:
condition:
binary_sensor.is_on: gen_run
then:
- lambda: |-
id(start_attempt) = 0;
id(restart_used) = false;
- script.execute:
id: set_state
new_state: 2
else:
- if:
condition:
lambda: 'return id(start_attempt) < id(max_attempts).state;'
then:
- script.execute: start_sequence
else:
- script.execute:
id: set_state
new_state: 3
- id: auto_start
then:
- if:
condition:
lambda: 'return id(gen_state) == 0;'
then:
- script.execute: start_sequence
- id: stop_now
then:
- lambda: 'id(stop_delay_active) = true;'
- switch.turn_on: kill_sw
- delay: 3s
- switch.turn_off: kill_sw
- if:
condition:
lambda: 'return id(gen_state) != 3;'
then:
- script.execute:
id: set_state
new_state: 0
- lambda: 'id(gen_run_seconds) = 0;'
- id: auto_stop
then:
- lambda: 'id(stop_delay_active) = true;'
- script.execute:
id: set_state
new_state: 4
- delay: !lambda "return id(auto_stop_delay).state * 1000;"
- if:
condition:
lambda: 'return id(stop_delay_active);'
then:
- script.execute: stop_now
- id: stop_sequence
then:
- lambda: |-
if (millis() - id(run_start_time) < id(fail_reset_run_time).state * 1000 && !id(restart_used)) {
id(restart_used) = true;
id(start_attempt) = 0;
id(set_state).execute(0);
id(start_sequence).execute();
} else {
id(set_state).execute(3);
}
- id: update_led
then:
- lambda: |-
if (id(gen_state) == 0) { // WAIT
auto call = id(board_led).turn_on();
call.set_rgb(0,0,0.6);
call.set_effect("WAIT pulse slow");
call.perform();
}
else if (id(gen_state) == 1) { // STARTING
auto call = id(board_led).turn_on();
call.set_effect("START blink");
call.perform();
}
else if (id(gen_state) == 2) { // RUN
auto call = id(board_led).turn_on();
call.set_rgb(0,1,0);
call.set_brightness(0.60);
call.set_effect("None");
call.perform();
}
else if (id(gen_state) == 3) { // FAIL
auto call = id(board_led).turn_on();
call.set_effect("FAIL blink");
call.perform();
}
else if (id(gen_state) == 4) { // COOLING
auto call = id(board_led).turn_on();
call.set_rgb(0.7,0,1);
call.set_brightness(0.7);
call.set_effect("None");
call.perform();
}
# ---------------- КНОПКИ ----------------
button:
- platform: template
name: START
icon: mdi:engine
on_press:
- script.stop: auto_stop
- lambda: |-
id(start_attempt) = 0;
id(restart_used) = false;
id(stop_delay_active) = false;
- script.execute: start_sequence
- platform: template
name: STOP
icon: mdi:engine-off
on_press:
- script.stop: start_sequence # Припинити спроби запуску
- script.stop: stop_sequence
- lambda: |-
id(start_attempt) = 0;
id(stop_delay_active) = false;
- script.execute: stop_now
light:
- platform: esp32_rmt_led_strip
id: board_led
name: "ATS LED"
internal: true
pin: GPIO8
num_leds: 1
chipset: ws2812
rgb_order: GRB
restore_mode: ALWAYS_OFF
gamma_correct: 2.8
effects:
- pulse:
name: WAIT pulse slow
transition_length: 2s
update_interval: 3s
min_brightness: 25%
max_brightness: 60%
- strobe:
name: START blink
colors:
- state: true
brightness: 100%
red: 100%
green: 80%
blue: 0
duration: 150ms
- state: false
duration: 150ms
- strobe:
name: FAIL blink
colors:
- state: true
brightness: 60%
red: 100%
green: 0
blue: 0
duration: 700ms
- state: false
duration: 700ms
# ---------------- МОТОГОДИНИ ----------------
interval:
- interval: 1s
then:
- if:
condition:
binary_sensor.is_on: gen_run
then:
- lambda: 'id(gen_run_seconds) += 1;'
- interval: 5s
then:
- if:
condition:
lambda: |-
return id(gen_state) == 3 &&
id(gen_run).state &&
(millis() - id(run_start_time) > id(fail_reset_run_time).state * 1000);
then:
- lambda: |-
id(set_state).execute(2);
id(restart_used) = false;