-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.yaml
755 lines (687 loc) · 20.9 KB
/
example.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
# TODO:
# - mp3 audio
# - fallback local audio
substitutions:
name: "alarm-clock"
friendly_name: "Alarm Clock"
blob:
# - id: mp3_nr1
# file: "./audio/abracadabra.mp3"
# - id: billy_jean
# file: "./audio/billy_jean-0.mid"
- id: miles
file: "./audio/I_can_see_for_miles.mid"
# - id: who_are_you
# file: "./audio/who_are_you.mid"
- id: bike_horn
file: "./audio/bike_horn.wav"
- id: arcade
file: "./audio/arcade.mp3"
esphome:
name: ${name}
on_boot:
then:
- script.execute: restore_time_from_rtc_module
- script.execute: start_blink_loop
- globals.set:
id: initialized
value: "true"
esp32:
board: esp32dev
framework:
type: arduino
# More quickly save alarm settings to flash. There's no reason
# to wait the default minute on this type of device.
preferences:
flash_write_interval: 2s
logger:
level: DEBUG
logs:
sensor: INFO
apds9960: INFO
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
power_save_mode: none
manual_ip:
static_ip: !secret net_alarm_clock_ip
gateway: !secret net_gateway
subnet: !secret net_mask
dns1: !secret net_dns
dns2: 9.9.9.9
ota:
password: !secret alarm_clock_ota_password
api:
password: !secret api_password
# Don't reboot the device when no API client is connected. This makes it
# feasible to use the device independent from Home Assistant.
reboot_timeout: 0s
# I have two devices that require I2C communication. These get their own
# dedicated I2C bus, so different frequencies can be used.
i2c:
- id: rtc_module_bus
sda: GPIO16
scl: GPIO17
frequency: 200kHz
- id: gesture_sensor_bus
sda: GPIO21
scl: GPIO22
frequency: 50kHz
globals:
# Whether or not system initialization has completed.
- id: initialized
type: bool
restore_value: false
initial_value: "false"
# Whether or not the alarm is active.
- id: alarm_active
type: bool
restore_value: true
initial_value: "false"
# The time at which the alarm settings were modified.
- id: alarm_set_at
type: time_t
restore_value: true
# The time at which the alarm must trigger.
- id: alarm_hh
type: int
restore_value: true
- id: alarm_mm
type: int
restore_value: true
# The last time at which the alarm was triggered.
- id: alarm_triggered_at
type: time_t
restore_value: true
# Today's time at which to trigger the alarm.
- id: alarm_today_at
type: time_t
restore_value: false
# The time until which to snooze.
- id: snooze_until
type: time_t
restore_value: false
# Helper variable for controlling blinking of display elements.
- id: blink
type: bool
restore_value: false
# Whether or not the rotary button was rotated while pressed.
# When true, then releasing the rotary button will not change
# the active display page.
- id: rotated_during_press
type: bool
restore_value: false
# Whether or not the proximity sensor detected proximity at the
# start of the alarm. When proximity is detected at the start,
# then hovering the hand over the alarm clock is not accepted
# for snoozing, since something might be blocking the sensor.
- id: proximity_off_at_start_alarm
type: bool
restore_value: false
script:
- id: restore_time_from_rtc_module
then:
- ds1307.read_time:
# (Re)starts the blink loop with blink enabled.
- id: start_blink_loop
mode: restart
then:
- lambda: "id(blink) = true;"
- script.execute: blink_loop
# Toggles the blink variable and updates the displays twice per second.
- id: blink_loop
mode: restart
then:
while:
condition:
lambda: "return true;"
then:
- script.execute: update_displays
- delay: 500ms
- lambda: "id(blink) = !id(blink);"
# Sends the UI to the main page when the user idles in a menu.
- id: start_menu_timeout
mode: restart
then:
- delay: 4s
- lambda: 'id(page).make_call().set_option("main").perform();'
- script.execute: update_displays
# Update the last time at which the alarm was configured.
- id: register_alarm_set_at
mode: restart
then:
- globals.set:
id: alarm_set_at
value: !lambda 'return id(rtc_time).now().timestamp;'
# Called every minute to check if the alarm should be triggered.
- id: check_alarm
mode: restart
then:
lambda: |-
#include <ctime>
static uint8_t log_state = 0;
if (!id(initialized)) {
return;
}
// Check if the alarm is active.
if (!id(alarm_active)) { return; }
// Check if the alarm clock knows what time it is.
auto now = id(rtc_time).now();
if (!now.is_valid()) { return; }
// Get the current timestamp.
tm now_tm = now.to_c_tm();
time_t now_time = mktime(&now_tm);
// Compute today's timestamp for the configured alarm time.
tm alarm_tm = now.to_c_tm();
alarm_tm.tm_hour = (int)id(alarm_hh);
alarm_tm.tm_min = (int)id(alarm_mm);
alarm_tm.tm_sec = 0;
time_t alarm_today_at_time = mktime(&alarm_tm);
id(alarm_today_at) = mktime(&alarm_tm);
// If today's alarm time has not yet passed, then wait some more.
if (difftime(id(alarm_today_at), now_time) > 0) {
if (log_state != 1) {
ESP_LOGD("Alarm Clock", "Alarm time not yet passed, wait some more");
ESP_LOGD("Alarm Clock", " alarm time: %s", ctime((time_t*)&id(alarm_today_at)));
ESP_LOGD("Alarm Clock", " now: %s", ctime((time_t*)&now_time));
log_state = 1;
}
return;
}
// If the alarm was set at a time later than today's alarm
// timestamp, then wait till tomorrow.
if (difftime(id(alarm_today_at), id(alarm_set_at)) < 0) {
if (log_state != 2) {
ESP_LOGD("Alarm Clock", "Alarm was set after today's alarm time, wait till tomorrow");
ESP_LOGD("Alarm Clock", " alarm time: %s", ctime((time_t*)&id(alarm_today_at)));
ESP_LOGD("Alarm Clock", " alarm set at: %s", ctime((time_t*)&id(alarm_set_at)));
log_state = 2;
}
return;
}
// If the alarm already triggered at or after today's alarm
// timestamp, then wait for the next alarm time.
if (difftime(id(alarm_today_at), id(alarm_triggered_at)) <= 0) {
if (log_state != 3) {
ESP_LOGD("Alarm Clock", "Alarm already triggered, wait till tomorrow");
ESP_LOGD("Alarm Clock", " alarm time: %s", ctime((time_t*)&id(alarm_today_at)));
ESP_LOGD("Alarm Clock", " triggered at: %s", ctime((time_t*)&id(alarm_triggered_at)));
log_state = 3;
}
return;
}
// All constraints met, trigger the alarm!
id(do_alarm).execute();
# Turn on the alarm.
- id: do_alarm
then:
- lambda: |-
ESP_LOGI("Alarm Clock", "Start alarm sequence");
- script.stop: do_snooze
- globals.set:
id: proximity_off_at_start_alarm
value: !lambda "return !id(proximity_detected).state;"
- globals.set:
id: alarm_triggered_at
value: !lambda |-
tm now_tm = id(rtc_time).now().to_c_tm();
time_t now_time = mktime(&now_tm);
return now_time;
- delay: !lambda "return id(max_alarm_time_in_minutes).state * 60.0 * 1000.0;"
- script.execute: turn_off_alarm
# Turn on snooze mode.
- id: do_snooze
then:
if:
condition:
script.is_running: do_alarm
then:
- lambda: |-
ESP_LOGI("Alarm Clock", "Start snooze sequence");
- script.stop: do_alarm
- lambda: |-
auto now = id(rtc_time).now();
tm snooze_until_tm = now.to_c_tm();
snooze_until_tm.tm_sec += id(snooze_time_in_minutes).state * 60.0;
id(snooze_until) = mktime(&snooze_until_tm);
ESP_LOGD("Alarm Clock", " snooze until: %s", ctime((time_t*)&id(snooze_until)));
- delay: !lambda "return id(snooze_time_in_minutes).state * 60.0 * 1000.0;"
- script.execute: do_alarm
# Turn off the alarm
- id: turn_off_alarm
then:
- script.stop: do_snooze
- script.stop: do_alarm
# Presents the alarm clock's UI on the connected displays.
- id: update_displays
mode: restart
then:
- lambda: |-
if (!id(initialized)) {
return;
}
auto dt = id(time_display);
auto da = id(alarm_display);
auto p = id(page).state;
auto b = id(blink);
auto hh = (int)id(alarm_hh);
auto mm = (int)id(alarm_mm);
if (p == "main") {
if (id(rtc_time).now().is_valid()) {
dt->strftime(b ? "%H.%M" : "%H%M", id(rtc_time).now());
} else {
dt->print("SYNC");
}
if (id(do_alarm).is_running()) {
da->print("BEEP");
} else if (id(do_snooze).is_running()) {
tm now_tm = id(rtc_time).now().to_c_tm();
time_t now_time = mktime(&now_tm);
auto diff = (int)(difftime(id(snooze_until), now_time));
da->printf(b ? "-%02d-" : " %02d ", (diff+59) / 60);
} else if (id(alarm_active)) {
da->printf("%02d.%02d", hh, mm);
} else {
da->print(" ");
}
} else if (p == "alarm_hh") {
dt->print("SET ");
if (b) {
da->printf("%02d.%02d", hh, mm);
} else {
da->printf(" .%02d", mm);
}
} else if (p == "alarm_mm") {
dt->print("SET ");
if (b) {
da->printf("%02d.%02d", hh, mm);
} else {
da->printf("%02d. ", hh);
}
} else if (p == "alarm_active") {
dt->print("ACT ");
da->print(id(alarm_active) ? "on " : "off ");
} else if (p == "radio_channel") {
dt->print("CHAN");
da->print(id(radio_channel).state.c_str());
} else if (p == "display_intensity") {
dt->print("DISP");
da->printf(" %d", (int)id(display_intensity).state + 1);
}
dt->display();
da->display();
time:
# The RTC time is the main time source.
- platform: ds1307
id: rtc_time
i2c_id: rtc_module_bus
timezone: Europe/Amsterdam
update_interval: 1h
on_time:
seconds: 0
minutes: "*"
then:
script.execute: check_alarm
# NTP for syncing the RTC module's time.
- platform: sntp
id: sntp_time
timezone: Europe/Amsterdam
on_time_sync:
then:
- ds1307.write_time:
- logger.log:
level: WARN
format: "Wrote NTP time to local RTC"
select:
# Pages that are used in the alarm clock's UI.
- platform: template
id: page
internal: true
optimistic: true
restore_value: false
options:
- main
- alarm_hh
- alarm_mm
- alarm_active
- radio_channel
- display_intensity
# Radio stations
# NPO1 : https://icecast.omroep.nl/radio1-sb-mp3
# NPO2 : https://icecast.omroep.nl/radio2-sb-mp3
# NPO3 : https://icecast.omroep.nl/3fm-sb-mp3
# NPO4 : https://icecast.omroep.nl/radio4-sb-mp3
# NPO5 : https://icecast.omroep.nl/radio5-sb-mp3
# SOUL : https://icecast.omroep.nl/radio6-sb-mp3
# 538 : http://21223.live.streamtheworld.com/RADIO538.mp3
# BNR : http://stream.bnr.nl/bnr_mp3_128_20
- platform: template
name: "${friendly_name} Radio station"
entity_category: ""
id: radio_channel
optimistic: true
restore_value: true
options:
- "NPO1"
- "NPO2"
- "NPO3"
- "NPO4"
- "NPO5"
- "SOUL"
- "538 "
- "BNR "
on_value:
then:
script.execute: update_displays
number:
- platform: template
id: max_alarm_time_in_minutes
name: "${friendly_name} Max Alarm Time in Minutes"
entity_category: ""
mode: box
initial_value: 5
min_value: 0.1
max_value: 60
step: 1
optimistic: true
restore_value: true
- platform: template
id: snooze_time_in_minutes
name: "${friendly_name} Snooze Time in Minutes"
entity_category: ""
mode: box
initial_value: 9
min_value: 0.1
max_value: 30
step: 1
optimistic: true
restore_value: true
- platform: template
id: alarm_hh_number
name: "${friendly_name} Alarm HH"
entity_category: ""
mode: box
min_value: 0
max_value: 23
step: 1
lambda: "return id(alarm_hh);"
set_action:
- lambda: |-
id(alarm_hh) = x;
id(alarm_hh_number).update();
- script.execute: register_alarm_set_at
on_value:
then:
- script.execute: update_displays
- platform: template
id: alarm_mm_number
name: "${friendly_name} Alarm MM"
entity_category: ""
mode: box
min_value: 0
max_value: 59
step: 5
lambda: "return id(alarm_mm);"
set_action:
- lambda: |-
id(alarm_mm) = x;
id(alarm_mm_number).update();
- script.execute: register_alarm_set_at
on_value:
then:
- script.execute: update_displays
- platform: template
id: display_intensity
name: "${friendly_name} Display Intensity"
initial_value: 3
min_value: 0
max_value: 3
step: 1
optimistic: true
restore_value: true
on_value:
then:
- lambda: |-
id(alarm_display).set_intensity(x);
id(time_display).set_intensity(x);
- script.execute: update_displays
switch:
# A switch component that provides the UI for turning
# on and off the alarm.
- platform: template
id: alarm_active_switch
entity_category: ""
name: ${friendly_name} Alarm Active
lambda: "return id(alarm_active);"
turn_on_action:
- globals.set: { id: alarm_active, value: "true" }
- script.execute: register_alarm_set_at
turn_off_action:
- globals.set: { id: alarm_active, value: "false" }
- script.execute: register_alarm_set_at
on_turn_on:
then:
- script.execute: update_displays
on_turn_off:
then:
- script.execute: update_displays
button:
# A button to fully turn off the alarm.
- platform: template
id: turn_off_alarm_button
name: "${friendly_name} Alarm Off Button"
icon: "mdi:alarm-off"
on_press:
script.execute: turn_off_alarm
# A button to turn on snooze mode.
- platform: template
id: snooze_alarm_button
name: "${friendly_name} Snooze Button"
icon: "mdi:alarm-snooze"
on_press:
script.execute: do_snooze
display:
# The display that is used for displaying the current time.
- platform: tm1637
id: time_display
dio_pin: GPIO13
clk_pin: GPIO14
intensity: 3
update_interval: 1h
# The display that is used for displaying the alarm time.
- platform: tm1637
id: alarm_display
dio_pin: GPIO4
clk_pin: GPIO5
intensity: 3
update_interval: 1h
external_components:
- source:
type: local
path: esphome-vs10xx/components
spi:
clk_pin: GPIO18
mosi_pin: GPIO23
miso_pin: GPIO19
vs10xx:
id: audio_decoder
type: vs1003
reset_pin: GPIO12
xcs_pin: GPIO32
xdcs_pin: GPIO33
dreq_pin: GPIO15
plugins:
- wavfix
- dacmono
#media_player:
# platform: vs10xx
# component_id: audio_decoder
apds9960:
update_interval: 500ms
i2c_id: gesture_sensor_bus
sensor:
- platform: uptime
name: ${friendly_name} Uptime
- platform: apds9960
id: proximity_sensor
name: ${friendly_name} Proximity
type: PROXIMITY
# This sensor represents the rotary function of the rotary encoder.
- platform: rotary_encoder
id: rotary_encoder_pos
entity_category: diagnostic
name: "${friendly_name} Rotary Encoder"
pin_a:
number: GPIO26
mode: INPUT_PULLUP
pin_b:
number: GPIO27
mode: INPUT_PULLUP
resolution: 1
on_clockwise:
then:
- if:
condition:
binary_sensor.is_on: rotary_encoder_key
then:
- select.next: page
- globals.set:
id: rotated_during_press
value: "true"
else:
- if:
condition:
lambda: 'return id(page).state == "alarm_hh";'
then:
- number.increment: { id: alarm_hh_number, cycle: true }
- if:
condition:
lambda: 'return id(page).state == "alarm_mm";'
then:
- number.increment: { id: alarm_mm_number, cycle: true }
- if:
condition:
lambda: 'return id(page).state == "alarm_active";'
then:
- switch.turn_on: alarm_active_switch
- if:
condition:
lambda: 'return id(page).state == "radio_channel";'
then:
select.next:
id: radio_channel
cycle: false
- if:
condition:
lambda: 'return id(page).state == "display_intensity";'
then:
number.increment:
id: display_intensity
cycle: false
- script.execute: start_blink_loop
- script.execute: start_menu_timeout
on_anticlockwise:
then:
- if:
condition:
binary_sensor.is_on: rotary_encoder_key
then:
- select.previous: page
- globals.set:
id: rotated_during_press
value: "true"
else:
- if:
condition:
lambda: 'return id(page).state == "alarm_hh";'
then:
- number.decrement: { id: alarm_hh_number, cycle: true }
- if:
condition:
lambda: 'return id(page).state == "alarm_mm";'
then:
- number.decrement: { id: alarm_mm_number, cycle: true }
- if:
condition:
lambda: 'return id(page).state == "alarm_active";'
then:
switch.turn_off:
id: alarm_active_switch
- if:
condition:
lambda: 'return id(page).state == "radio_channel";'
then:
select.previous:
id: radio_channel
cycle: false
- if:
condition:
lambda: 'return id(page).state == "display_intensity";'
then:
number.decrement:
id: display_intensity
cycle: false
- script.execute: start_blink_loop
- script.execute: start_menu_timeout
- platform: wifi_signal
name: "${friendly_name} WiFi Signal"
update_interval: 30s
binary_sensor:
- platform: status
name: "${friendly_name} Status"
# This binary sensor represents the key function of the rotary encoder.
- platform: gpio
id: rotary_encoder_key
entity_category: diagnostic
name: "${friendly_name} Rotary Encoder Button"
pin:
number: GPIO25
mode: INPUT_PULLUP
inverted: true
filters:
- delayed_on: 10ms
on_press:
then:
- globals.set:
id: rotated_during_press
value: "false"
#- vs10xx.turn_off_output:
- vs10xx.set_volume: 70%
- vs10xx.play:
blob_id: bike_horn
# on_release:
# then:
# if:
# condition:
# or:
# - script.is_running: do_alarm
# - script.is_running: do_snooze
# then:
# - script.execute: turn_off_alarm
# else:
# if:
# condition:
# lambda: 'return !id(rotated_during_press);'
# then:
# - select.next: page
# - script.execute: start_blink_loop
# - script.execute: start_menu_timeout
#
# Proximity detection, used to switch an alarm into snooze mode
# by hovering a hand over the alarm clock.
- platform: analog_threshold
id: proximity_detected
name: "${friendly_name} Proximity Detected"
sensor_id: proximity_sensor
threshold:
upper: 30
lower: 15
on_press:
# The snooze feature will only work when no proximity was
# detected at the start of the alarm. This prevents activating
# snooze mode when something is blocking the proximity sensor.
if:
condition:
lambda: 'return id(proximity_off_at_start_alarm);'
then:
- script.execute: do_snooze