-
Notifications
You must be signed in to change notification settings - Fork 1
/
EHLClock-HA.yaml
1158 lines (1113 loc) · 39.4 KB
/
EHLClock-HA.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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# My github: https://github.com/trip5
substitutions:
name: xy-clock-white
friendly_name: XY-Clock-White
comment: "EHLClock-HA from Trip5"
project_version: "2024.12.09"
project_name: Trip5.EspHomeLedClock-HA
waiting: "EHLC" # shows when waiting for time
# Time zone must be POSIX format: (ie "KST-9" & "PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00" & "AST4ADT,M3.2.0,M11.1.0")
# Most can be viewed here: https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
# or here: https://support.cyberdata.net/portal/en/kb/articles/010d63c0cfce3676151e1f2d5442e311
# If you need to make a custom POSIX format you can look here: https://developer.ibm.com/articles/au-aix-posix/)
# or even better, use this POSIX Generator (courtesy of TopYuan): https://www.topyuan.top/posix
time_zone: "AST4ADT,M3.2.0,M11.1.0"
time_zone_name: "AST"
# Alternate Time Zone here. Must also be POSIX format - it would be good to keep the time zone matched to any language substitution being used (ie. Korean to match Korea Standard Time)
time_zone_alt: "KST-9"
time_zone_alt_name: "KST"
#Note these time zone names will be displayed, so keep it simple and 7-segment-compatible!
# This is the sensor name from your Home Assistant configuration
ha_sensor_data: sensor.led_clock_data
# Pins for I2C RTC are same for Sinilink and 303 Clocks
scl_pin: GPIO12
sda_pin: GPIO13
# TM1637 or similar? Specify clk & dio pins here, delete the above 2 lines (if no RTC) and edit the rest of the file carefully!
# clk_pin: GPIO16
# dio_pin: GPIO17
# Mappings are the order of the LED segments. Using the wrong mapping will result in a garbled display!
segment_mapping: PGFEDCBA # Specific to the Sinilink XY-Clock
# segment_mapping: GBPEDCAF # Specific to 303WIFILC01
# TM1637 units don't actually use segment mapping but the routines that make the colon blink depend on it being specified (PM dot is ignored by the hardware)
# segment_mapping: PABCDEFG # TM1637 / normal TM1650 (untested, may not support colon or decimals)
backward: 'false'
# Upside-down display? Use one of these and change above line to backward: 'true'
# segment_mapping: PDCBGFEA # Upside-down Sinilink XY-Clock
# segment_mapping: GDCABPEF # Upside-down 303WIFILC01
# segment_mapping: PDEFABCG # To use with TM1650 (untested)
# * if there is another segment_mapping to use, it's quite simple to change a regular mapping to an upside-down one...
# * The rule is actually fairly simple. Take 2nd, 3rd, 4th and switch with 5th, 6th, 7th in order (TM1650 pattern is easy: PABCDEFG -> PDEFABCG)
# Sinilink Clock uses these pins for buttons
down_button_pin: GPIO9
up_button_pin: GPIO10
set_button_pin: GPIO16
# 303 Clock uses these pins for buttons
# down_button_pin: GPIO15
# up_button_pin: GPIO4
# set_button_pin: GPIO0
# Sinilink Clock has these additional GPIOs (the blue LED looks better for status)
status_led_pin: GPIO0
other_led_pin: GPIO2
buzzer_pin: GPIO5
key_pin: GPIO14 # Contact 'KEY'
# The 303 only has 1 LED (uncomment next line and delete the above 4 lines)
# status_led_pin: GPIO2
# The following are for the tunes played by the alarm through the passive piezo speaker / buzzer on the Sinilink clock - remove if not needed
# You can check here: https://picaxe.com/rtttl-ringtones-for-tune-command/ or Search for "RTTTL tunes" to change the tunes
tune1_name: "Siren"
tune1: "siren:d=8,o=5,b=100:d,e,d,e,d,e,d,e"
tune2_name: "Scale Up"
tune2: "scale_up:d=32,o=5,b=100:c,c#,d#,e,f#,g#,a#,b"
tune3_name: "Death"
tune3: "death:d=4,o=5,b=100:c.,c,8c,c.,d#,8d,d,8c,c,8c,2c."
esphome:
name: ${name}
comment: "${comment}"
project:
name: "${project_name}"
version: "${project_version}"
min_version: 2023.11.0
on_boot:
- priority: 600
then:
- globals.set:
id: segmentmap
value: !lambda 'return "${segment_mapping}";'
- if:
condition:
- switch.is_on: tz_alt
then:
- switch.turn_on: tz_alt
# The Sinilink uses a DS1307 RTC (supported by ESPHome)
- ds1307.read_time:
# The 303 uses a DS1302 RTC (will also require an external component)
# - ds1302.read_time:
- priority: -100
then:
- wait_until:
condition:
- lambda: 'return (id(my_time).now().is_valid());'
# The Sinilink uses a DS1307 RTC
- ds1307.write_time:
# The 303 uses a DS1302 RTC
# - ds1302.write_time:
esp8266:
board: esp8285
restore_from_flash: True
wifi:
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
reboot_timeout: 15min # Default is 15min
id: wifi_id
ap:
ssid: ${name}
password: !secret ap_password
on_connect:
- logger.log: "Wifi connected"
- light.turn_off: led1
external_components:
# Use Trip5's fork of Buzzer13's TM1650 ESPHome Component
- source:
type: git
url: https://github.com/trip5/esphome-tm1650
ref: main
refresh: 60s
components: [ tm1650 ]
# Prefer a local source (only use one source)
# - source:
# type: local
# path: my-components/tm1650 # e.g. /config/esphome/components
# components: [ tm1650 ]
# refresh: 1s
# The 303 Clock has a DS1302 RTC (uncomment this section to use it), use Trip5's fork of Trombik's DS1302 ESPHome Component
# - source:
# type: git
# url: https://github.com/trip5/esphome-ds1302
# ref: main
# refresh: 60s
# components: [ ds1302 ]
time:
- platform: homeassistant # use Home Assistant as a time source (sntp is also possible)
id: my_time
timezone: "${time_zone}"
update_interval: 24h # This seems like the longest safe amount of time - actual configured update happens in interval section
# The Sinilink Clock uses a DS1307 RTC
- platform: ds1307
id: my_time_ds
timezone: "${time_zone}"
update_interval: never
# The 303 Clock uses a DS1302 RTC (uncomment this section and delete the above DS1307 section if needed)
# - platform: ds1302
# id: my_time_ds
# cs_pin: GPIO5
# dio_pin: GPIO14
# clk_pin: GPIO16
# update_interval: never
# timezone: "${time_zone}"
script:
- id: sync_the_time
mode: restart
then:
- lambda: 'id(my_time)->update();'
# The Sinilink Clock uses a DS1307 RTC
- ds1307.write_time:
# The 303 Clock uses a DS1302 RTC (uncomment this and delete the other if needed)
# - ds1302.write_time:
# The script that actually plays the alarms - if you want to add/subtract alarms or tunes, take a look at the nested 'if' states - copy/paste/edit as needed - remove if not needed
- id: check_alarms
mode: restart
then:
- lambda: |-
std::time_t tz1time = (id(my_time).now().timestamp);
std::tm *tz1time_astm = localtime(&tz1time);
int tzhr = tz1time_astm->tm_hour;
int tzmin = tz1time_astm->tm_min;
if (id(alarms_on).state == true && id(alarm1_hour).state == tzhr && id(alarm1_min).state == tzmin) {
// need to put all alarm tunes that exist nested here
if (id(alarm1_tune).state == 1) { id(tune1).press(); };
if (id(alarm1_tune).state == 2) { id(tune2).press(); };
if (id(alarm1_tune).state == 3) { id(tune3).press(); };
};
// if you add an alarm3, duplicate and edit the entire next 'if' (and edit as needed)
if (id(alarms_on).state == true && id(alarm2_hour).state == tzhr && id(alarm2_min).state == tzmin) {
// need to put all alarm tunes that exist nested here
if (id(alarm2_tune).state == 1) { id(tune1).press(); };
if (id(alarm2_tune).state == 2) { id(tune2).press(); };
if (id(alarm2_tune).state == 3) { id(tune3).press(); };
};
# The next section interacts with Home Assistant - it's possible the device doesn't need Home Assistant, in which case delete this whole section
api:
reboot_timeout: 15min # 15min is default
services:
# The next section is only for the Sinilink Clock. If using the 303 Clock or another with no piezo speaker, delete the next 6 lines
- service: tune
variables:
tune: string
then:
- rtttl.play:
rtttl: !lambda 'return tune;'
# This next section creates the Home Assistant services to allow messages to be displayed
- service: message
variables:
message: string
message_alive_time: int
message_display_time: int
message_clock_time: int
then:
- globals.set:
id: message
value: !lambda 'return message;'
- globals.set:
id: message_alive_time
value: !lambda 'return message_alive_time;'
- globals.set:
id: message_display_time
value: !lambda 'return message_display_time;'
- globals.set:
id: message_clock_time
value: !lambda 'return message_clock_time;'
- globals.set:
id: message_clock_time_count
value: !lambda 'return message_clock_time;'
- globals.set:
id: message_alive_time_count
value: '0'
- logger.log:
format: "Message: %s"
args: [ 'message.c_str()' ]
- logger.log:
format: "Alive Time: %i / Display Time: %i / Clock Time: %i"
args: [ 'message_alive_time', 'message_display_time' , 'message_clock_time' ]
mdns:
disabled: False
logger:
level: NONE
# Logging is disabled to save memory but you can easily enable logging (and stop the various sensors/switches from spamming the log) by deleting the above line and uncommening the lines below
# Note that increasing the level uses up memory (which this device has very little available) and may cause errors and/or crashing - please note it's also disabled in the web_server section
# level: DEBUG
# logs:
# sensor: ERROR
# text_sensor: NONE
# switch: NONE
# light: NONE
# number: NONE
# rtttl: NONE
# Web Server is disabled due to memory constraints. Enable at your own risk (consider removing captive_portal, logging to save memory)
#web_server:
# port: 80
# ota: True
# include_internal: False # True will expose more than needed to the Web UI
# version: 2 # version 3 is much larger but possible if local is False
# local: True # will download www.js and serve it directly from the clock
# log: False # disabling in the WebUI to save memory
ota:
- platform: esphome
password: !secret ota_password
on_begin:
then:
- lambda: |-
id(message) = ("OtA");
id(message_alive_time) = 100;
id(message_display_time) = 100;
id(message_clock_time) = 0;
id(tm_display)->update();
- logger.log: "OTA Update started."
on_progress:
then:
- lambda: |-
id(message) = "=" + std::to_string((int)x) + "==";
if (x > 9) { id(tm_display)->update(); }
- logger.log:
format: "OTA progress %0.1f%%"
args: ["x"]
on_end:
then:
- lambda: |-
id(message) = "done";
id(tm_display)->update();
- logger.log: "OTA end"
on_error:
then:
- lambda: |-
id(message) = "Eror";
id(tm_display)->update();
- logger.log:
format: "OTA update error %d"
args: ["x"]
captive_portal:
text_sensor:
- platform: version
name: "${friendly_name} ESPHome Version"
hide_timestamp: True
entity_category: diagnostic
- platform: wifi_info
ip_address:
id: wifi_ip
name: "${friendly_name} IP Address"
entity_category: diagnostic
ssid:
id: wifi_ssid
name: "${friendly_name} SSID"
entity_category: diagnostic
- platform: template
name: "${friendly_name} ${project_name} Version"
lambda: 'return to_string("${project_version}");'
entity_category: diagnostic
update_interval: 1h
- platform: template
name: "${friendly_name} Time Zone"
lambda: 'return {"${time_zone}"};'
entity_category: diagnostic
update_interval: 1h
# Call text sensors from Home Assistant
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 0
id: ha_sensor_data_0
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 1
id: ha_sensor_data_1
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 2
id: ha_sensor_data_2
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 3
id: ha_sensor_data_3
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 4
id: ha_sensor_data_4
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 5
id: ha_sensor_data_5
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 6
id: ha_sensor_data_6
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 7
id: ha_sensor_data_7
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 8
id: ha_sensor_data_8
- platform: homeassistant
entity_id: '${ha_sensor_data}'
attribute: 9
id: ha_sensor_data_9
output:
- platform: esp8266_pwm
id: statusled
pin:
number: ${status_led_pin}
inverted: True
# The next section is only for the Sinilink Clock. If using the 303 Clock or another with no piezo speaker, delete the next 3 lines
- platform: esp8266_pwm
pin: ${buzzer_pin}
id: rtttl_out
# The next section is only for the Sinilink Clock. If using the 303 Clock or another with no piezo speaker, delete the next 2 lines
rtttl:
output: rtttl_out
light:
- platform: monochromatic
id: led1
name: "LED"
output: statusled
internal: True
# Sinilink has 2 LEDs and this will remap the 2nd LED as a status LED but its blinking can be distracting so I've commented it out / 303 Clock can't use it
#status_led:
# pin:
# number: ${other_led_pin}
# inverted: True
preferences:
flash_write_interval: 15s # 0 does immediate write to memory with no mem buffer (not recommended) (only saves when persistent variables have changed)
i2c:
sda: ${sda_pin}
scl: ${scl_pin}
scan: True
id: i2cbus
# Check this section carefully!
# Also, some info how to handle multi_click options: https://community.home-assistant.io/t/esphome-button-held-double-press/136083/10
binary_sensor:
- platform: status
name: "${friendly_name} Status"
- platform: gpio
pin:
number: ${up_button_pin}
inverted: True # Sinilink true / 303 true
mode: INPUT_PULLUP
name: "${friendly_name} Up button"
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_multi_click:
#Click UP will increase brightness
- timing:
- ON for at most 0.5s
- OFF for at least 0.5s
then:
- number.increment: brightness
#Hold UP will toggle enable or disable alarms / delete this part if Alarms don't exist anywhere else (the 303 Clock)
- timing:
- ON for at least 1s
then:
- switch.toggle: alarms_on
- platform: gpio
pin:
number: ${down_button_pin}
inverted: True # Sinilink true / 303 false
mode: INPUT_PULLUP
name: "${friendly_name} Down button"
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_multi_click:
#Click DOWN will decrease brightness
- timing:
- ON for at most 0.5s
- OFF for at least 0.5s
then:
- number.decrement: brightness
#Hold DOWN will select enable or disable alt time zone
- timing:
- ON for at least 1s
then:
- switch.toggle: tz_alt
- platform: gpio
pin:
number: ${set_button_pin}
inverted: True # Sinilink true / 303 true
name: "${friendly_name} Set button"
filters:
- delayed_on: 10ms
- delayed_off: 10ms
on_multi_click:
#Click SET will toggle 12-hour mode
- timing:
- ON for at most 0.5s
- OFF for at least 0.5s
then:
- switch.toggle: hour12_mode
#Hold SET will toggle HA Sensors Data Stop
- timing:
- ON for at least 1s
then:
- switch.toggle: ha_sensor_data_stop
# This section contains a lot of lambda code... if using the Sinilink or 303, you shouldn't have to edit this at all
# If using something else, you may need to edit the segmentmap sections (conveniently 'if' sections), depending on how the decimals/colons are handled
# If that's true, get back to me on github by opening an issue and help contribute to other displays this YAML can handle!
# If using a TM1637, you should change the platform to tm1637 and check a few lines just below here
display:
- platform: tm1650
id: tm_display
update_interval: 1s
length: 4
backward: ${backward}
segment_map: ${segment_mapping}
i2c_id: i2cbus
# TM1637: remove the above 3 lines and uncomment the next 2 lines
# clk_pin: ${clk_pin}
# dio_pin: ${dio_pin}
# If you are using either the Sinilink or 303, there should be no need to edit anything inside this lambda below!
lambda: |-
// Display the message if all conditions are met
if (id(my_time).now().is_valid()) {
if ((id(message_alive_time) != 0) && (id(message_clock_time_count) >= id(message_clock_time))) {
id(message_display_time_count) += 1;
id(message_alive_time_count) += 1;
auto message_text = id(message);
it.print(0, message_text.c_str());
if (id(message_display_time_count) >= id(message_display_time)) {
id(message_display_time_count) = 0;
id(message_clock_time_count) = 0;
if (id(message_alive_time_count) >= id(message_alive_time)) {
id(message_alive_time_count) = 0;
id(message_alive_time) = 0;
ESP_LOGD("main", "Message Alive Time finished.");
}
}
} else {
// check if we should display sensor data now
if (id(ha_sensor_data_now) == true) {
std::string sensor_data_text = "";
if (id(ha_sensor_data_this) == 0) {
if (id(ha_sensor_data_0).state.empty()) {
id(ha_sensor_data_this) = 1;
} else {
sensor_data_text = id(ha_sensor_data_0).state.c_str();
}
}
if (id(ha_sensor_data_this) == 1) {
if (id(ha_sensor_data_1).state.empty()) {
id(ha_sensor_data_this) = 2;
} else {
sensor_data_text = id(ha_sensor_data_1).state.c_str();
}
}
if (id(ha_sensor_data_this) == 2) {
if (id(ha_sensor_data_2).state.empty()) {
id(ha_sensor_data_this) = 3;
} else {
sensor_data_text = id(ha_sensor_data_2).state.c_str();
}
}
if (id(ha_sensor_data_this) == 3) {
if (id(ha_sensor_data_3).state.empty()) {
id(ha_sensor_data_this) = 4;
} else {
sensor_data_text = id(ha_sensor_data_3).state.c_str();
}
}
if (id(ha_sensor_data_this) == 4) {
if (id(ha_sensor_data_4).state.empty()) {
id(ha_sensor_data_this) = 5;
} else {
sensor_data_text = id(ha_sensor_data_4).state.c_str();
}
}
if (id(ha_sensor_data_this) == 5) {
if (id(ha_sensor_data_5).state.empty()) {
id(ha_sensor_data_this) = 6;
} else {
sensor_data_text = id(ha_sensor_data_5).state.c_str();
}
}
if (id(ha_sensor_data_this) == 6) {
if (id(ha_sensor_data_6).state.empty()) {
id(ha_sensor_data_this) = 7;
} else {
sensor_data_text = id(ha_sensor_data_6).state.c_str();
}
}
if (id(ha_sensor_data_this) == 7) {
if (id(ha_sensor_data_7).state.empty()) {
id(ha_sensor_data_this) = 8;
} else {
sensor_data_text = id(ha_sensor_data_7).state.c_str();
}
}
if (id(ha_sensor_data_this) == 8) {
if (id(ha_sensor_data_8).state.empty()) {
id(ha_sensor_data_this) = 9;
} else {
sensor_data_text = id(ha_sensor_data_8).state.c_str();
}
}
if (id(ha_sensor_data_this) == 9) {
if (id(ha_sensor_data_9).state.empty()) {
if (id(ha_sensor_data_all).state == false) {
id(ha_sensor_data_this) = 0;
sensor_data_text = id(ha_sensor_data_0).state.c_str();
}
} else {
sensor_data_text = id(ha_sensor_data_9).state.c_str();
}
}
if (sensor_data_text != "") {
id(ha_sensor_data_this_time) += 1;
if ((id(ha_sensor_data_this_time) >= (id(ha_sensor_data_time).state)) && (id(ha_sensor_data_all).state == true)) {
id(ha_sensor_data_this_time) = 0;
id(ha_sensor_data_this) += 1;
}
if ((id(ha_sensor_data_this_time) > (id(ha_sensor_data_time).state)) && (id(ha_sensor_data_all).state == false)) { // was just > but... 2 seconds either way?
id(ha_sensor_data_this_time) = 0;
id(ha_sensor_data_this) += 1;
id(ha_sensor_data_now) = false ; // we needed to buy 1 second if this mode is on
} else {
it.print(0, sensor_data_text.c_str());
}
} else {
id(ha_sensor_data_now) = false;
id(ha_sensor_data_this) = 0;
id(ha_sensor_data_this_time) = 0;
}
if ((id(ha_sensor_data_this) == 9) && (id(ha_sensor_data_all).state == true)) {
id(ha_sensor_data_this) = false;
id(ha_sensor_data_now) = false;
}
}
// because there may be no HA sensors, we can't skip display the time/date - just check if it actually did print it
if (id(ha_sensor_data_now) == false) {
if ((id(message_alive_time) != 0) && (id(message_clock_time) != 0)) {
id(message_clock_time_count) += 1;
id(message_alive_time_count) += 1;
}
static int blinking = false;
std::time_t tz1time = (id(my_time).now().timestamp);
std::tm *tz1time_astm = localtime(&tz1time);
// check if the date should be displayed
if ((id(time_display_count) >= (id(time_display_time).state)) && (id(date_display_mode).state != false)) {
id(date_display_count) += 1;
// display date
int tzmon = (tz1time_astm->tm_mon) + 1;
int tzmday = tz1time_astm->tm_mday;
std::string timemon_text = std::to_string(tzmon);
std::string timemday_text = std::to_string(tzmday);
std::string mon_a = (timemon_text.substr(0,1).c_str());
std::string mon_b = (timemon_text.substr(1,1).c_str());
std::string mday_a = (timemday_text.substr(0,1).c_str());
std::string mday_b = (timemday_text.substr(1,1).c_str());
if (tzmon < 10) {
mon_b = mon_a;
mon_a = "0";
}
if (tzmday < 10) {
mday_b = mday_a;
mday_a = "0";
}
if (id(date_display_mode_us).state != false) {
it.print(0, mday_a.c_str());
it.print(1, mday_b.c_str());
it.print(2, mon_a.c_str());
it.print(3, mon_b.c_str());
} else {
it.print(0, mon_a.c_str());
it.print(1, mon_b.c_str());
it.print(2, mday_a.c_str());
it.print(3, mday_b.c_str());
}
} else {
// display time
id(date_display_count) = 0;
id(time_display_count) += 1;
int tzhr = tz1time_astm->tm_hour;
int tzmin = tz1time_astm->tm_min;
std::string timeh_text = std::to_string(tzhr);
std::string timem_text = std::to_string(tzmin);
std::string hour_a = (timeh_text.substr(0,1).c_str());
std::string hour_b = (timeh_text.substr(1,1).c_str());
std::string min_a = (timem_text.substr(0,1).c_str());
std::string min_b = (timem_text.substr(1,1).c_str());
if (tzmin < 10) {
min_b = min_a;
min_a = "0";
}
if (id(hour12_mode).state == true) {
if (tzhr > 12) {
timeh_text = std::to_string((tz1time_astm->tm_hour) - 12);
hour_a = (timeh_text.substr(0,1).c_str());
hour_b = (timeh_text.substr(1,1).c_str());
}
if ((tzhr < 22 && tzhr > 12) || (tzhr < 10)) {
hour_b = hour_a;
hour_a = " ";
}
if (tzhr == 0) {
hour_a = "1";
hour_b = "2";
}
if (tzhr > 11) {
if (id(segmentmap) == "GBPEDCAF") {
min_b.append("."); // the 303 can have a decimal after the 4th digit to indicate PM
}
if (id(segmentmap) == "GDCABPEF") {
hour_a.append("."); // the 303 can have a decimal after the 4th digit to indicate PM (flipped!)
}
if (id(segmentmap) == "PGFEDCBA") {
hour_b.append("."); // the Sinilink can have a decimal after the 2nd digit to indicate PM
}
if (id(segmentmap) == "PDCBGFEA") {
min_a.append("."); // the Sinilink can have a decimal after the 2nd digit to indicate PM (flipped!)
}
}
} else {
if (tzhr < 10) {
hour_b = hour_a;
hour_a = "0";
}
}
if (((++blinking & 1) && (id(blink_option) == 1)) || (id(blink_option) == 2)) {
if (id(segmentmap) == "GBPEDCAF") {
hour_b.append("."); // the 303 requires a period between the 2nd and 3rd digit to activate the colon
}
if (id(segmentmap) == "GDCABPEF") {
min_a.append("."); // the 303 requires a period between the 2nd and 3rd digit to activate the colon (flipped!)
}
if (id(segmentmap) == "PGFEDCBA") {
min_a.append(".");
min_b.append("."); // the Sinilink requires periods after the 3rd and 4th digits to activate the colon
}
if (id(segmentmap) == "PDCBGFEA") {
hour_a.append(".");
hour_b.append("."); // the Sinilink requires periods after the 3rd and 4th digits to activate the colon (flipped!)
}
if (id(segmentmap) == "PABCDEFG") {
hour_b.append("."); // the TM1637 requires a period between the 2nd and 3rd digit to activate the colon (flipped can't do it)
}
}
it.print(0, hour_a.c_str());
it.print(1, hour_b.c_str());
it.print(2, min_a.c_str());
it.print(3, min_b.c_str());
}
if (((id(date_display_count) >= id(date_display_time).state) && (id(date_display_mode).state != false)) || ((id(time_display_count) >= id(time_display_time).state) && (id(date_display_mode).state == false))) {
id(time_display_count) = 0;
// check if Sensor Data Stop is active
if (id(ha_sensor_data_stop).state == false) {
id(ha_sensor_data_now) = true;
}
}
}
}
} else {
it.print("${waiting}");
}
it.set_intensity(id(brightness).state);
interval:
- interval: 1min # check if the time should be synced, and if so, do it
then:
- lambda: |-
id(sync_time_count) += 1;
if (id(sync_time_count) >= (id(sync_time).state * 60)) {
id(sync_time_count) = 0;
id(sync_the_time).execute();
}
globals:
- id: message
type: std::string
restore_value: False
initial_value: ''
- id: message_display_time
type: int
restore_value: False
initial_value: '0'
- id: message_clock_time
type: int
restore_value: False
initial_value: '0'
- id: message_alive_time
type: int
restore_value: False
initial_value: '0'
- id: message_display_time_count
type: int
restore_value: False
initial_value: '0'
- id: message_clock_time_count
type: int
restore_value: False
initial_value: '0'
- id: message_alive_time_count
type: int
restore_value: False
initial_value: '0'
- id: segmentmap
type: std::string
restore_value: False
initial_value: ''
- id: IPAddressA
type: int
restore_value: False
initial_value: '0'
- id: IPAddressB
type: int
restore_value: False
initial_value: '0'
- id: IPAddressC
type: int
restore_value: False
initial_value: '0'
- id: IPAddressD
type: int
restore_value: False
initial_value: '0'
- id: blink_option
type: int
restore_value: True
initial_value: '1'
- id: time_display_count
type: int
restore_value: False
initial_value: '0'
- id: date_display_count
type: int
restore_value: False
initial_value: '0'
- id: sync_time_count
type: int
restore_value: False
initial_value: '0'
# HA Sensors variables
- id: ha_sensor_data_this
type: int
restore_value: False
initial_value: '0'
- id: ha_sensor_data_this_time
type: int
restore_value: False
initial_value: '0'
- id: ha_sensor_data_now
type: bool
restore_value: False
initial_value: 'false'
button:
- platform: restart
id: restart_button
name: "${friendly_name} Restart"
entity_category: diagnostic
# Tune Buttons (edit as needed, verify the selector below matches the number available) / the next section is only for the Sinilink Clock. If using the 303 Clock or another with no piezo speaker, delete the next 21 lines
- platform: template
id: tune1
name: "Tune 1: ${tune1_name}"
internal: False
on_press:
- logger.log: "Playing ${tune1_name}"
- rtttl.play: "${tune1}"
- platform: template
id: tune2
name: "Tune 2: ${tune2_name}"
internal: False
on_press:
- logger.log: "Playing ${tune2_name}"
- rtttl.play: "${tune2}"
- platform: template
id: tune3
name: "Tune 3: ${tune3_name}"
internal: False
on_press:
- logger.log: "Playing ${tune3_name}"
- rtttl.play: "${tune3}"
switch:
- platform: template
name: "${friendly_name} 12 Hour Mode"
id: hour12_mode
restore_mode: RESTORE_DEFAULT_OFF
optimistic: True
turn_on_action:
- lambda: |-
ESP_LOGD("main", "12 Hour Mode On");
turn_off_action:
- lambda: |-
ESP_LOGD("main", "12 Hour Mode Off");
- platform: template
name: "${friendly_name} Date Display US Mode (DDMM)"
id: date_display_mode_us
restore_mode: RESTORE_DEFAULT_OFF
optimistic: True
turn_on_action:
- lambda: |-
ESP_LOGD("main", "Date Display US Mode On");
turn_off_action:
- lambda: |-
ESP_LOGD("main", "Date Display US Mode Off");
- platform: template
name: "${friendly_name} Date Display Mode"
id: date_display_mode
restore_mode: RESTORE_DEFAULT_OFF
optimistic: True
turn_on_action:
- logger.log: "Date Display Mode On"
- lambda: |-
id(message) = ("dton");
id(message_alive_time) = 1;
id(message_display_time) = 1;
id(message_clock_time) = 0;
turn_off_action:
- logger.log: "Date Display Mode Off"
- lambda: |-
id(message) = ("doff");
id(message_alive_time) = 1;
id(message_display_time) = 1;
id(message_clock_time) = 0;
- platform: template
name: "${friendly_name} HA Sensors Data All"
id: ha_sensor_data_all
restore_mode: RESTORE_DEFAULT_OFF
optimistic: True
turn_on_action:
- lambda: |-
ESP_LOGD("main", "HA Sensors Data All On");
turn_off_action:
- lambda: |-
ESP_LOGD("main", "HA Sensors Data All Off");
- platform: template
name: "${friendly_name} HA Sensors Data Stop"
id: ha_sensor_data_stop
restore_mode: RESTORE_DEFAULT_OFF
optimistic: True
turn_on_action:
- lambda: |-
ESP_LOGD("main", "HA Sensors Data Stop On");
turn_off_action:
- lambda: |-
ESP_LOGD("main", "HA Sensors Data Stop Off");
# Alt Time Zone selector / activator
- platform: template
name: "${friendly_name} Alternate Time (${time_zone_alt_name})"
id: tz_alt
restore_mode: RESTORE_DEFAULT_OFF
optimistic: True
turn_on_action:
- delay: 1s
- logger.log: "Alt Time Zone on: ${time_zone_alt_name}"
- lambda: |-
id(message) = ("${time_zone_alt_name}");
id(message_alive_time) = 1;
id(message_display_time) = 1;
id(message_clock_time) = 0;
id(my_time)->set_timezone("${time_zone_alt}");
id(my_time_ds)->set_timezone("${time_zone_alt}");
id(my_time)->call_setup();
turn_off_action:
- delay: 1s
- logger.log: "Alt Time Zone off: ${time_zone_name}"
- lambda: |-
id(message) = ("${time_zone_name}");
id(message_alive_time) = 1;
id(message_display_time) = 1;
id(message_clock_time) = 0;
id(my_time)->set_timezone("${time_zone}");
id(my_time_ds)->set_timezone("${time_zone}");
id(my_time)->call_setup();
# Alarms On/Off Selector: in theory, you could edit this and duplicate it to turn individual alarms on and off (you'd have to edit the on_time section and add global variables)
# If using the 303 Clock or another with no piezo speaker, delete the next 19 lines
- platform: template
name: "${friendly_name} Alarms On"
id: alarms_on
restore_mode: RESTORE_DEFAULT_OFF
optimistic: True
turn_on_action:
- logger.log: "Alarms On"
- lambda: |-
id(message_alive_time) = 1;
id(message_display_time) = 1;
id(message_clock_time) = 0;
id(message) = ("ALon");
turn_off_action:
- logger.log: "Alarms Off"
- lambda: |-
id(message_alive_time) = 1;
id(message_display_time) = 1;
id(message_clock_time) = 0;
id(message) = ("Aoff");
select:
- platform: template
name: "${friendly_name} Colon Blink (0=Off / 1=On / 2=Always On)"
id: blink_option_select
restore_value: True
optimistic: True
options:
- '0'
- '1'
- '2'
initial_option: '1'
on_value:
then:
- lambda: |-
id(blink_option) = atoi(x.substr(0,2).c_str());
ESP_LOGD("main", "Colon Blink: %d", id(blink_option));
number:
- platform: template
name: "${friendly_name} Brightness"
id: brightness
restore_value: True
optimistic: True
step: 1
min_value: 0
max_value: 8
initial_value: 1
mode: slider
set_action:
then: