-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchessclock.yaml
652 lines (631 loc) · 18.2 KB
/
chessclock.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
esphome:
name: chessclock
platform: ESP8266
board: esp_wroom_02
esp8266_restore_from_flash: yes
on_boot:
then:
- lambda: |-
id(alarmhourtmp) = id(alarmhour);
id(alarmminutetmp) = id(alarmminute);
logger:
api:
ota:
wifi:
use_address: chessclock.local
reboot_timeout: 0s # disable reboot if no wifi connection is established
power_save_mode: HIGH
networks:
- ssid: !secret wifi_j_id
password: !secret wifi_j_pw
- ssid: !secret wifi_b_id
password: !secret wifi_b_pw
- ssid: !secret wifi_m_id
password: !secret wifi_m_pw
- ssid: !secret wifi_id
password: !secret wifi_pw
binary_sensor:
- platform: gpio
pin:
number: GPIO5
mode: INPUT_PULLUP
inverted: yes
internal: true
name: "Chess Toggle"
id: p_switch
on_press:
then:
- script.stop: dimmdown
- lambda: |-
if (!id(settings).state) {
if (id(playing).state) {
id(player).toggle();
} else {
if (!id(started)) id(player).toggle();
id(bright) = 10;
id(indicator) = true;
}
} else {
if (!id(alarmsetting).state) {
int _val = id(p1);
if (_val > 36000) {
_val = 0;
}
_val += id(increment);
id(p1) = _val;
id(p2) = _val;
}
}
id(ring).turn_on();
if (id(alarmsetting).state) {
id(alarminc).turn_on();
}
- component.update: segments
- delay: !lambda "if (id(playing).state) return 0; else return 1000;"
- lambda: |-
if (!id(playing).state) {
id(bright) = 3;
id(indicator) = false;
}
- if:
condition:
- switch.is_off: playing
then:
- script.execute: dimmdown
- if:
condition:
rtttl.is_playing
then:
- rtttl.stop
on_release:
then:
- lambda: |-
id(alarminc).turn_off();
on_double_click:
then:
- lambda: |-
if (!id(settings).state) {
if (!id(started)) {
id(p1) = id(preset);
id(p2) = id(preset);
}
if (id(playing).state) {
id(countdown) = false;
id(playing).turn_off();
id(bright) = 3;
} else {
id(countdown) = true;
id(started) = true;
id(ginprogress).publish_state(1);
id(bright) = 10;
id(indicator) = true;
}
} else {
if (!id(alarmsetting).state) {
id(p1) -= id(increment) * 2;
id(p2) -= id(increment) * 2;
}
id(alarmsetting).toggle();
}
on_click:
- min_length: 1s
max_length: 4s
then:
- if:
condition:
- lambda: 'return id(started);'
then:
- script.execute: resetgame
else:
- lambda: |-
if (id(settings).state) {
id(preset) = id(p1) - id(increment);
}
if (!id(alarmsetting).state) {
id(p1) = id(preset);
id(p2) = id(preset);
id(settings).toggle();
}
- lambda: 'id(player).toggle();'
- min_length: 4s
max_length: 7s
then:
- if:
condition:
lambda: |-
return !id(alarmsetting).state;
then:
- switch.toggle: alarm
switch:
- platform: gpio
pin: GPIO12
name: "Button light"
id: ring
icon: "mdi:radiobox-marked"
restore_mode: ALWAYS_ON
- platform: template
name: "Alarm"
id: "alarm"
icon: "mdi:alarm"
restore_state: yes
optimistic: true
- platform: template
name: "Settings"
id: "settings"
icon: "mdi:cog-outline"
restore_state: no
optimistic: true
turn_on_action:
- lambda: |-
id(p1) = id(preset);
id(p2) = id(preset);
turn_off_action:
- lambda: |-
id(preset) = id(p1);
- switch.turn_off: alarmsetting
- platform: template
name: "Setting Alarm"
id: "alarmsetting"
icon: "mdi:cog-clockwise"
restore_state: no
optimistic: true
turn_on_action:
- switch.turn_on: settings
- platform: template
name: "Playing"
id: "playing"
icon: "mdi:chess-bishop"
restore_state: no
optimistic: true
turn_on_action:
- lambda: |-
if(!id(started)) {
id(p1) = id(preset);
id(p2) = id(preset);
id(started) = true;
id(ginprogress).publish_state(1);
id(indicator) = true;
}
id(bright) = 10;
turn_off_action:
- lambda: |-
if(id(started)) {
id(countdown) = false;
id(bright) = 3;
}
- platform: template
name: "Active player"
id: "player"
icon: "mdi:swap-horizontal"
restore_state: no
optimistic: true
turn_on_action:
- script.execute: showactiveplayer
- sensor.template.publish:
id: p1sens
state: !lambda 'return (id(p1)/600);'
turn_off_action:
- script.execute: showactiveplayer
- sensor.template.publish:
id: p2sens
state: !lambda 'return (id(p2)/600);'
- platform: template
name: "Alarm increase time"
id: "alarminc"
icon: "mdi:alarm-plus"
restore_state: no
optimistic: true
- platform: template
name: "Game increase time"
restore_state: no
icon: "mdi:checkerboard-plus"
turn_on_action:
- lambda: |-
if (id(settings).state) {
if (!id(alarmsetting).state) {
int _val = id(p1);
if (_val > 108000) {
_val = 0;
}
_val += id(increment);
id(p1) = _val;
id(p2) = _val;
}
}
- component.update: segments
- platform: template
name: "Game End"
icon: "mdi:checkerboard-remove"
restore_state: no
turn_on_action:
- script.execute: resetgame
sensor:
- platform: template
name: "Game in progress"
id: "ginprogress"
lambda: 'return id(started);'
update_interval: 1h
icon: "mdi:checkerboard"
accuracy_decimals: 0
- platform: template
name: "Time Player 1"
id: "p1sens"
lambda: 'return (id(p1)/600);'
update_interval: 1h
icon: "mdi:chevron-left-circle"
unit_of_measurement: "m"
accuracy_decimals: 0
- platform: template
name: "Time Player 2"
id: "p2sens"
lambda: 'return (id(p2)/600);'
update_interval: 1h
icon: "mdi:chevron-right-circle"
unit_of_measurement: "m"
accuracy_decimals: 0
- platform: template
name: "Time Game"
lambda: 'return (id(gtimer)/60);'
update_interval: 60s
icon: "mdi:camera-timer"
unit_of_measurement: "m"
accuracy_decimals: 0
- platform: template
name: "Alarm time"
lambda: 'return id(alarmhour) + (id(alarmminute) * 0.01);'
id: alarmtime
update_interval: 1h
icon: "mdi:timeline-clock"
unit_of_measurement: "h"
accuracy_decimals: 2
rtttl:
- output: buzzer
id: sound
output:
- platform: esp8266_pwm
pin: GPIO4
id: buzzer
globals:
- id: started # game in progress (playing or paused)
type: bool
restore_value: no
initial_value: "false"
- id: bright # Display brightness 0-15
type: int
restore_value: no
initial_value: '3'
- id: p1 # Left player time left
type: int
restore_value: no
initial_value: "0"
- id: p2 # Right player time left
type: int
restore_value: no
initial_value: "0"
- id: preset # Initial game time
type: int
restore_value: yes
initial_value: "3000" # in 100ms -> 3600 * 10 = 1h
- id: gtimer # Total game time
type: int
restore_value: no
initial_value: "0"
- id: indicator # Display "-" indicator true/false
type: bool
restore_value: no
initial_value: "false"
- id: startup # Display "-" if time not valid yet
type: int
restore_value: no
initial_value: "1"
- id: increment # Game time increment
type: int
restore_value: no
initial_value: "3000"
- id: countdown # Countdown visible true/false
type: bool
restore_value: no
initial_value: "false"
- id: cdvalue # Countdown value
type: int
restore_value: no
initial_value: "4"
- id: ended # Game has ended true/false
type: bool
restore_value: no
initial_value: "false"
- id: songplayed # Game has ended song played true/false
type: bool
restore_value: no
initial_value: "false"
- id: alarmhour
type: int
restore_value: yes
initial_value: "6"
- id: alarmminute
type: int
restore_value: yes
initial_value: "0"
- id: alarmhourtmp
type: int
restore_value: no
initial_value: "6"
- id: alarmminutetmp
type: int
restore_value: no
initial_value: "0"
- id: alarmplayed # Alarm song played true/false
type: bool
restore_value: no
initial_value: "false"
spi:
clk_pin: GPIO14
mosi_pin: GPIO13
time:
- platform: sntp
id: sntp_time
timezone: Europe/Berlin
interval:
- interval: 1s # Alarmclock Check & Alert
then:
- if:
condition:
lambda: |-
if (id(alarm).state && id(sntp_time).now().is_valid()) {
if (id(sntp_time).now().hour == id(alarmhour)) {
if (id(sntp_time).now().minute == id(alarmminute)) {
if (!id(alarmplayed)) {
id(alarmplayed) = true;
return true;
}
} else id(alarmplayed) = false;
}
}
return false;
then:
- if:
condition:
- lambda: |-
return (id(sntp_time).now().month == 12 && id(sntp_time).now().day_of_month == 24);
then:
- rtttl.play: 'christmas:d=4,o=5,b=200:d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,2g,d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,1g,d,g,g,g,2f#,f#,g,f#,e,2d,a,b,8a,8a,8g,8g,d6,d,d,e,a,f#,2g'
else:
- if:
condition:
- lambda: |-
return (id(sntp_time).now().month == 9 && id(sntp_time).now().day_of_month == 8);
then:
- rtttl.play: 'birthday:d=4,o=5,b=125:8d,8d,e,d,g,2f#,8d,8d,e,d,a,2g,8d,8d,d6,b,g,2f#,e,8c6,8c6,b,g,a,g,p'
else:
- if:
condition:
- lambda: |-
return (id(sntp_time).now().month == 5 && id(sntp_time).now().day_of_month == 4);
then:
- rtttl.play: 'starwars:d=4,o=5,b=180:8f,8f,8f,2a#.,2f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8d#6,2c6,p,8f,8f,8f,2a#.,2f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8d#6,2c6'
else:
- rtttl.play: "Bond:d=4,o=6,b=112:16c.5,32d.5,32d.5,16d.5,8d.5,16c.5,16c.5,16c.5,16c.5,32d#.5,32d#.5,16d#.5,8d#.5,16d.5,16d.5,16d.5,16c.5,32d.5,32d.5,16d.5,8d.5,16c.5,16c.5,16c.5,16c.5,32d#.5,32d#.5,16d#.5,8d#.5,16d.5,16d.5,16d.5,16c.5,32d.5,32d.5,16d.5,8d.5,16c.5,16c.5,16c.5,16c.5,32d#.5,32d#.5,16d#.5,8d#.5,16d.5,16d.5,16c.5,16b.5,2a#5,16g.5,16f.5,2g.5"
#- rtttl.play: "alarm:d=10,o=7,b=90:c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,c,p,c,pc,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c,p,c"
- interval: 100ms # Player Timers
then:
- lambda: |-
if (id(playing).state) {
int _p1, _p2;
_p1 = id(p1);
_p2 = id(p2);
if (_p1 > 0 && _p2 > 0) {
if (id(player).state) {
id(p2) -= 1;
} else {
id(p1) -= 1;
}
} else {
id(playing).turn_off();
id(indicator) = true;
id(bright) = 15;
id(ended) = true;
}
if (_p1 < 6000 || _p2 < 6000) {
id(segments).update();
}
}
- interval: 50ms
then:
- if:
condition:
switch.is_on: alarminc
then:
- lambda: |-
if (id(alarmminutetmp) < 59) {
id(alarmminutetmp) += 1;
} else {
id(alarmminutetmp) = 0;
if (id(alarmhourtmp) < 23) {
id(alarmhourtmp) += 1;
} else {
id(alarmhourtmp) = 0;
}
}
- component.update: segments
else:
- if:
condition:
lambda: |-
if (id(settings).state || (id(alarmhourtmp) == id(alarmhour) && id(alarmminutetmp) == id(alarmminute))) {
return false;
} else return true;
then:
- lambda: |-
id(alarmminute) = id(alarmminutetmp);
id(alarmhour) = id(alarmhourtmp);
ESP_LOGD("settings", "Updated alarm settings");
id(alarmtime).publish_state(id(alarmhourtmp) + (id(alarmminutetmp) * 0.01));
- interval: 1s # Game Timer, Countdown & End of Game Alert
then:
- lambda: |-
if (id(playing).state) {
id(gtimer) += 1;
} else {
if (id(started)) id(indicator) = !id(indicator);
}
if (id(countdown)) {
id(bright) = 10;
if (id(cdvalue) > 1) {
id(cdvalue) -= 1;
}
else {
id(cdvalue) = 4;
id(countdown) = false;
id(playing).turn_on();
}
}
if (id(started) && id(playing).state) id(indicator) = true;
if (id(ended)) {
if (id(indicator)) {
id(bright) = 15;
} else {
id(bright) = 1;
}
}
- if:
condition:
and:
- lambda: 'return id(ended);'
- lambda: 'return !id(songplayed);'
then:
- if:
condition:
- lambda: |-
return (id(sntp_time).now().month == 12 && id(sntp_time).now().day_of_month == 24);
then:
- rtttl.play: 'christmas:d=4,o=5,b=200:d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,2g,d,g,8g,8a,8g,8f#,e,e,e,a,8a,8b,8a,8g,f#,d,d,b,8b,8c6,8b,8a,g,e,d,e,a,f#,1g,d,g,g,g,2f#,f#,g,f#,e,2d,a,b,8a,8a,8g,8g,d6,d,d,e,a,f#,2g'
else:
- if:
condition:
- lambda: |-
return (id(sntp_time).now().month == 9 && id(sntp_time).now().day_of_month == 8);
then:
- rtttl.play: 'birthday:d=4,o=5,b=125:8d,8d,e,d,g,2f#,8d,8d,e,d,a,2g,8d,8d,d6,b,g,2f#,e,8c6,8c6,b,g,a,g,p'
else:
- if:
condition:
- lambda: |-
return (id(sntp_time).now().month == 5 && id(sntp_time).now().day_of_month == 4);
then:
- rtttl.play: 'starwars:d=4,o=5,b=180:8f,8f,8f,2a#.,2f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8d#6,2c6,p,8f,8f,8f,2a#.,2f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8c6,2a#.6,f.6,8d#6,8d6,8d#6,2c6'
else:
- rtttl.play: "MadeForL:d=4,o=6,b=140:a5,b5,c,8d,e,8e,d,c,b5,a5,b5,c,8d,e,8e,2d,p,a5,b5,c,8d,e,8e,d,c,b5,a5,b5,c,8d,e,8e,2d,p,8e,8d,8e,d,8c,8d,8e,8g,e.,2p,8e,8d,8e,d,8c,8d,8e,8g,e.,p,8e,e,e.,8d,d.,p,8d,e,d.,8c,c"
#- rtttl.play: "smbdeath:d=4,o=5,b=90:32c6,32c6,32c6,8p,16b,16f6,16p,16f6,16f.6,16e.6,16d6,16c6,16p,16e,16p,16c"
- lambda: 'id(songplayed) = true;'
script:
- id: dimmdown # Dimm Display every minute until brightness = 1
mode: restart
then:
- while:
condition:
lambda: |-
return id(bright) > 1;
then:
- delay: 1min
- lambda: |-
id(bright) -= 1;
if (id(bright) == 1) {
id(ring).turn_off();
}
- id: showactiveplayer
mode: single
then:
- lambda: |-
if (!id(settings).state) {
if (!id(started)) id(player).toggle();
id(bright) = 10;
id(indicator) = true;
}
- component.update: segments
- delay: !lambda "if (id(playing).state) return 0; else return 1000;"
- lambda: |-
if (!id(playing).state) {
id(bright) = 3;
id(indicator) = false;
}
- id: resetgame
mode: single
then:
- lambda: |-
if (id(started)) {
id(playing).turn_off();
id(started) = false;
id(ginprogress).publish_state(0);
id(indicator) = false;
id(ended) = false;
id(p1) = id(preset);
id(p2) = id(preset);
id(gtimer) = 0;
id(bright) = 3;
id(songplayed) = false;
}
display:
- platform: max7219
id: segments
cs_pin: GPIO15
num_chips: 2
lambda: |-
it.set_intensity(id(bright));
if (id(bright) != 0) {
if (id(indicator)) {
int _pos = id(player).state * 7;
it.print(_pos, "-");
}
if (id(alarm).state) it.print(7, ".");
if (id(started) || (id(settings).state)) {
int _s1, _p1s, _p1m, _s2, _p2s, _p2m;
_s1 = id(p1);
_p1m = _s1/600;
_p1s = _s1/10-(_p1m*60);
_s2 = id(p2);
_p2m = _s2/600;
_p2s = _s2/10-(_p2m*60);
if (!id(alarmsetting).state) {
if (_p1m < 10) {
int _p1c = _s1-(_p1m*60+_p1s)*10;
it.printf(8, "%1d.%02d.%1d", _p1m, _p1s, _p1c);
} else if (_p1m > 99) {
it.printf(8, "%03d", _p1m);
} else it.printf(8, "%02d.%02d", _p1m, _p1s);
if (_p2m < 10) {
int _p2c = _s2-(_p2m*60+_p2s)*10;
it.printf(12, "%1d.%02d.%1d", _p2m, _p2s, _p2c);
} else if (_p2m > 99) {
it.printf(12, "%03d", _p2m);
} else it.printf(12, "%02d.%02d", _p2m, _p2s);
}
if (!id(settings).state && !id(countdown)) {
int _g, _gs, _gm, _gh;
_g = id(gtimer);
_gh = _g/3600;
_gm = (_g-(_gh*3600))/60;
_gs = _g-(_gh*3600)-(_gm*60);
it.printf(1,"%02d.%02d.%02d", _gh, _gm, _gs);
} else if (id(countdown)) {
int _pos = 1 + id(player).state * 5;
it.printf(_pos,"%1d", id(cdvalue));
} else {
if (!id(alarmsetting).state) {
it.printf(1, "SCHACH");
} else {
it.print(1, "uhr");
it.printf(10, "%02d.%02d", id(alarmhourtmp), id(alarmminutetmp));
}
}
} else {
if (id(sntp_time).now().is_valid()) {
it.strftime(1,"%H.%M.%S", id(sntp_time).now());
it.strftime(8,"%d.%m.%Y", id(sntp_time).now());
} else {
it.print(id(startup),"-");
it.print(id(startup)+8,"-");
if (id(startup) < 6) id(startup)+=1;
else id(startup) = 1;
}
}
}