/
template_alarm_clock.yaml
557 lines (503 loc) · 20.8 KB
/
template_alarm_clock.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
###############################################################################
## Templates - Alarm Clock
###############################################################################
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: sensor.alarm_clock_sounds
attribute: file_list
to: ~
sensor:
- name: "Alarm Clock Soundlist"
unique_id: alarm_clock_soundlist
icon: mdi:alarm-bell
state: >
{% set sounds = state_attr(this.entity_id,'soundlist') %}
{{ 0 if sounds == none else sounds|count }}
attributes:
soundlist: >
{% set files = namespace(value=[]) %}
{% set file_list = state_attr('sensor.alarm_clock_sounds','file_list') %}
{% if file_list != none %}
{% for item in file_list %}
{% set files.value = files.value
+ [item|replace('/media/music/Alarm Clock Sounds/','')
|replace('.mp3','')|replace('_',' ')|title] %}
{% endfor %}
{% else %}{% set files.value = ['None'] %}
{% endif %}
{{ files.value }}
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: group.alarm_clock_media_players
attribute: entity_id
to: ~
- platform: state
entity_id: input_text.alarm_clock_media_player_auto
to: ~
select:
- name: "Alarm Clock Media Player Auto"
unique_id: alarm_clock_media_player_auto
state: "{{ states('input_text.alarm_clock_media_player_auto') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_media_player_auto
data:
value: "{{ option }}"
options: &alarm_players >
{% set options = expand(state_attr('group.alarm_clock_media_players','entity_id'))|map(attribute='name')|list|sort %}
{% if options|count > 0 %} {{ options }}
{% else %} none
{% endif %}
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: group.alarm_clock_media_players
attribute: entity_id
to: ~
- platform: state
entity_id: input_text.alarm_clock_media_player_manual
to: ~
select:
- name: "Alarm Clock Media Player Manual"
unique_id: alarm_clock_media_player_manual
state: "{{ states('input_text.alarm_clock_media_player_manual') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_media_player_manual
data:
value: "{{ option }}"
options: *alarm_players
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: group.alarm_clock_media_players
attribute: entity_id
to: ~
- platform: state
entity_id: input_text.alarm_clock_media_player_nap
to: ~
select:
- name: "Alarm Clock Media Player Nap"
unique_id: alarm_clock_media_player_nap
state: "{{ states('input_text.alarm_clock_media_player_nap') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_media_player_nap
data:
value: "{{ option }}"
options: *alarm_players
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: sensor.alarm_clock_soundlist
attribute: soundlist
to: ~
- platform: state
entity_id: input_text.alarm_clock_sound_auto
to: ~
select:
- name: "Alarm Clock Sound Auto"
unique_id: alarm_clock_sound_auto
state: "{{ states('input_text.alarm_clock_sound_auto') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_sound_auto
data:
value: "{{ option }}"
options: &alarm_sounds >
{% set sounds = state_attr('sensor.alarm_clock_soundlist','soundlist') %}
{% set sounds = iif(sounds != none,sounds,[]) %}
{{ ['Media'] + sounds }}
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: sensor.alarm_clock_soundlist
attribute: soundlist
to: ~
- platform: state
entity_id: input_text.alarm_clock_sound_manual
to: ~
select:
- name: "Alarm Clock Sound Manual"
unique_id: alarm_clock_sound_manual
state: "{{ states('input_text.alarm_clock_sound_manual') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_sound_manual
data:
value: "{{ option }}"
options: *alarm_sounds
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: sensor.alarm_clock_soundlist
attribute: soundlist
to: ~
- platform: state
entity_id: input_text.alarm_clock_sound_nap
to: ~
select:
- name: "Alarm Clock Sound Nap"
unique_id: alarm_clock_sound_nap
state: "{{ states('input_text.alarm_clock_sound_nap') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_sound_nap
data:
value: "{{ option }}"
options: *alarm_sounds
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: input_text.alarm_clock_media_auto
to: ~
select:
- name: "Alarm Clock Media Auto"
unique_id: alarm_clock_media_auto
state: "{{ states('input_text.alarm_clock_media_auto') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_media_auto
data:
value: "{{ option }}"
#MASS - need ability to load playlist names into select
options: !include &media_playlist /config/include/mass_playlist_template.yaml
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: input_text.alarm_clock_media_manual
to: ~
select:
- name: "Alarm Clock Media Manual"
unique_id: alarm_clock_media_manual
state: "{{ states('input_text.alarm_clock_media_manual') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_media_manual
data:
value: "{{ option }}"
options: *media_playlist
- trigger:
- platform: homeassistant
event: start
- platform: event
event_type: event_template_reloaded
- platform: state
entity_id: input_text.alarm_clock_media_nap
to: ~
select:
- name: "Alarm Clock Media Nap"
unique_id: alarm_clock_media_nap
state: "{{ states('input_text.alarm_clock_media_nap') }}"
select_option:
- condition: template
value_template: "{{ option != none }}"
- service: input_text.set_value
target:
entity_id: input_text.alarm_clock_media_nap
data:
value: "{{ option }}"
options: *media_playlist
- binary_sensor:
- name: "Alarm Clock"
unique_id: alarm_clock
icon: mdi:alarm-note
state: >
{{ (is_state('switch.alarm_clock_auto','on')
or is_state('switch.alarm_clock_manual','on')
or is_state('switch.alarm_clock_nap','on'))
and is_state('input_boolean.alarm_clock_failed','off') }}
attributes:
alarm_type: >
{% if is_state('switch.alarm_clock_auto','on') %} auto
{% elif is_state('switch.alarm_clock_manual','on') %} manual
{% elif is_state('switch.alarm_clock_nap','on') %} nap
{% endif %}
- name: "Alarm Clock Presence Alert"
unique_id: alarm_clock_presence_alert
icon: mdi:clock-alert
device_class: problem
delay_on: 30 # allow temporary switch on/off
state: >
{{ is_state('input_boolean.alarm_clock_alerts','on')
and is_state('binary_sensor.alarm_clock','on')
and (is_state('binary_sensor.someone_home','off')
or is_state('input_select.occupancy_mode','Away')) }}
- name: "Alarm Clock LED Alert"
unique_id: alarm_clock_led_alert
state: >
{{ is_state('binary_sensor.alarm_clock','on')
or is_state('input_boolean.alarm_clock_failed','on') }}
- sensor:
- name: "Alarm Clock Media Player"
unique_id: alarm_clock_media_player
icon: mdi:animation-play
state: >
{% if is_state('binary_sensor.alarm_clock','on') %}
{% set active = states('input_text.active_alarm_clock_media_player') %}
{% if is_state('switch.alarm_clock_auto','on') %}{% set selected = states('select.alarm_clock_media_player_auto') %}
{% elif is_state('switch.alarm_clock_manual','on') %}{% set selected = states('select.alarm_clock_media_player_manual') %}
{% elif is_state('switch.alarm_clock_nap','on') %}{% set selected = states('select.alarm_clock_media_player_nap') %}
{% endif %}
{% set player = iif(active != 'off',state_attr(active,'friendly_name'),selected) %}
{{ expand('group.alarm_clock_media_players')
|selectattr('name','eq',player)|map(attribute='entity_id')|join('') }}
{% else %} {{ none }}
{% endif %}
attributes:
entity_id: >
{% set player = states(this.entity_id) %}
{% set new_members = namespace(value=[]) %}
{% if has_value(this.entity_id) %}
{% set members = expand(state_attr(player,'group_members')) %}
{% for item in members %}
{% set new_members.value = new_members.value + [item.entity_id ~ '_2'] %}
{% endfor %}
{{ [player] + new_members.value }}
{% else %} []
{% endif %}
- name: "Alarm Clock Auto Next Alarm"
unique_id: alarm_clock_auto_next_alarm
device_class: timestamp
icon: |
{% from 'alarm_clock.jinja' import alarm_clock_icon %}
{{ alarm_clock_icon(this.entity_id) }}
state: >
{% set days = today_at(states('input_datetime.days_waketime')) %}
{% set afts = today_at(states('input_datetime.afternoons_waketime')) %}
{% set wkds = today_at(states('input_datetime.weekend_waketime')) %}
{% set guest = today_at(states('input_datetime.guest_waketime')) %}
{% if is_state('input_select.occupancy_mode','Guest') %}
{% set next_alarm = guest if is_state('input_boolean.alarm_clock_guest','on') else none %}
{% else %}
{% set alarm_today = wkds if is_state('binary_sensor.work_today','off')
else afts if is_state('sensor.work_shift_today','Afternoons') else days %}
{% set alarm_tom = wkds if is_state('binary_sensor.work_tomorrow','off')
else afts if is_state('sensor.work_shift_tomorrow','Afternoons') else days %}
{% set alarm_tom = alarm_tom + timedelta(days=1) %}
{# set next alarm to tomorrows alarm time if now is past alarm time #}
{% set next_alarm = alarm_tom if now() > alarm_today else alarm_today %}
{# set next alarm to null if not boolean enabled #}
{% if (now() <= alarm_today and is_state('input_boolean.alarm_clock_auto_workdays','off')
if is_state('binary_sensor.work_today','on')
else is_state('input_boolean.alarm_clock_auto_weekends','off'))
or (now() > alarm_today and is_state('binary_sensor.work_tomorrow','off')
and is_state('input_boolean.alarm_clock_auto_weekends','off'))
or (now() > alarm_today and is_state('binary_sensor.work_tomorrow','on')
and is_state('input_boolean.alarm_clock_auto_workdays','off'))
or (now() > alarm_today and is_state('binary_sensor.work_tomorrow','off')
and is_state('input_boolean.alarm_clock_auto_weekends','off')) %}
{% set next_alarm = none %}
{% endif %}
{% endif %}
{{ next_alarm }}
attributes:
source: Auto
status: >
{% if is_state('switch.alarm_clock_auto','on') %}
{% if is_state('input_boolean.alarm_clock_failed','on') %} failed
{% elif is_state('switch.alarm_clock_snooze','on') %} snoozed
{% elif is_state('input_boolean.alarm_clock_test_play','on') %} testing
{% elif states(states('sensor.alarm_clock_media_player')) in ['playing','buffering'] %} playing
{% else %} delayed
{% endif %}
{% elif not has_value('sensor.alarm_clock_auto_next_alarm') %} off
{% else %} pending
{% endif %}
12hour: >
{% from 'format.jinja' import twelve_hour %}
{{ twelve_hour(this.entity_id) }}
- name: "Alarm Clock Manual Next Alarm"
unique_id: alarm_clock_manual_next_alarm
device_class: timestamp
icon: |
{% from 'alarm_clock.jinja' import alarm_clock_icon %}
{{ alarm_clock_icon(this.entity_id) }}
state: >
{% if is_state('input_boolean.alarm_clock_manual','on') %}
{% set alarm_time = today_at(states('input_datetime.alarm_clock_manual_time')) %}
{% set alarm_time = alarm_time + timedelta(days=1) if alarm_time < now () else alarm_time %}
{{ alarm_time }}
{% else %} {{ none }}
{% endif %}
attributes:
source: Manual
status: >
{% if is_state('switch.alarm_clock_manual','on') %}
{% if is_state('input_boolean.alarm_clock_failed','on') %} failed
{% elif is_state('switch.alarm_clock_snooze','on') %} snoozed
{% elif is_state('input_boolean.alarm_clock_test_play','on') %} testing
{% elif states(states('sensor.alarm_clock_media_player')) in ['playing','buffering'] %} playing
{% else %} delayed
{% endif %}
{% elif not has_value('sensor.alarm_clock_manual_next_alarm') %} off
{% else %} pending
{% endif %}
12hour: >
{% from 'format.jinja' import twelve_hour %}
{{ twelve_hour(this.entity_id) }}
- name: "Alarm Clock Nap Next Alarm"
unique_id: alarm_clock_nap_next_alarm
device_class: timestamp
icon: |
{% from 'alarm_clock.jinja' import alarm_clock_icon %}
{{ alarm_clock_icon(this.entity_id) }}
state: "{{ state_attr('timer.alarm_clock_nap','finishes_at') if is_state('input_boolean.alarm_clock_nap','on') else none }}"
attributes:
source: Nap
status: >
{% if is_state('switch.alarm_clock_nap','on') %}
{% if is_state('input_boolean.alarm_clock_failed','on') %} failed
{% elif is_state('switch.alarm_clock_snooze','on') %} snoozed
{% elif is_state('input_boolean.alarm_clock_test_play','on') %} testing
{% elif states(states('sensor.alarm_clock_media_player')) in ['playing','buffering'] %} playing
{% else %} delayed
{% endif %}
{% elif not has_value('sensor.alarm_clock_nap_next_alarm') %} off
{% else %} pending
{% endif %}
12hour: >
{% from 'format.jinja' import twelve_hour %}
{{ twelve_hour(this.entity_id) }}
# other alarm clocks set in next 24 hours
- name: "Alarm Clock Other"
unique_id: alarm_clock_other
device_class: timestamp
icon: "{{ iif(has_value('sensor.alarm_clock_other'),'mdi:alarm-check','mdi:alarm-off') }}"
state: >
{% set set_alarms = namespace(value=[]) %}
{% set alarms = state_attr('sensor.alarm_clock_other','alarm_clocks') %}
{% if alarms != none %}
{% for item in alarms %}
{% if has_value('sensor.' ~ item ~ '_next_alarm') %}
{% set next = states('sensor.' ~ item ~ '_next_alarm') %}
{% if next|as_datetime <= now() + timedelta(hours=24) %}
{% set set_alarms.value = set_alarms.value + [next|as_datetime] %}
{% endif %}
{% endif %}
{% endfor %}
{{ (set_alarms.value|min) if set_alarms.value|count > 0 else none }}
{% else %} {{ none }}
{% endif %}
attributes:
alarm_clocks: >
{{ ['jason_phone','dining_room_hub','bedroom_hub',
'bathroom_speaker','office_speaker','living_room_speaker','laundry_room_speaker'] }}
status: "{{ 'off' if state_attr('sensor.alarm_clock_other','source') == none else 'set' }}"
source: >
{% set source = namespace(value=none) %}
{% if has_value('sensor.alarm_clock_other') %}
{% set next = states('sensor.alarm_clock_other') %}
{% set alarms = expand('group.alarm_clock_other_alarms') %}
{% for item in alarms if next == item.state %}
{% set source.value = item.name|replace(' Next Alarm','') %}
{% break %}
{% endfor %}
{% endif %}
{{ source.value }}
12hour: >
{% from 'format.jinja' import twelve_hour %}
{{ twelve_hour(this.entity_id) }}
# earliest of auto, manual, phone, google home alarms in next 24 hours
- name: "Alarm Clock Next Alarm"
unique_id: alarm_clock_next_alarm
device_class: timestamp
icon: |
{% from 'alarm_clock.jinja' import alarm_clock_icon %}
{{ alarm_clock_icon(this.entity_id) }}
state: >
{% set set_alarms = state_attr('sensor.alarm_clock_next_alarm','set_alarms') %}
{{ set_alarms|min|as_datetime if set_alarms != none else none }}
attributes:
set_alarms: >
{% set set_alarms = namespace(value=[]) %}
{% set other_alarms = state_attr('sensor.alarm_clock_other','alarm_clocks')
if state_attr('sensor.alarm_clock_other','alarm_clocks') != none else [] %}
{% set alarm_clocks = ['alarm_clock_auto','alarm_clock_manual','alarm_clock_nap'] + other_alarms %}
{% for item in alarm_clocks %}
{% if has_value('sensor.' ~ item ~ '_next_alarm') %}
{% set alarm = states('sensor.' ~ item ~ '_next_alarm') %}
{% if alarm|as_datetime <= now() + timedelta(hours=24) %}
{% set set_alarms.value = set_alarms.value + [alarm|as_timestamp] %}
{% endif %}
{% endif %}
{% endfor %}
{{ set_alarms.value if set_alarms.value|count > 0 else none }}
status: >
{% if is_state('input_boolean.alarm_clock_failed','on') %} failed
{% elif is_state('switch.alarm_clock_snooze','on') %} snoozed
{% elif is_state('input_boolean.alarm_clock_test_play','on') %} testing
{% elif is_state('binary_sensor.alarm_clock','on') %}
{% if states(states('sensor.alarm_clock_media_player')) in ['playing','buffering'] %} playing
{% else %} delayed
{% endif %}
{% elif state_attr('sensor.alarm_clock_next_alarm','set_alarms') == none %} off
{% else %} {{ state_attr('sensor.alarm_clock_next_alarm','12hour') }}
{% endif %}
source: >
{% set next = states('sensor.alarm_clock_next_alarm') %}
{% if next != none %}
{% if next == states('sensor.alarm_clock_auto_next_alarm') %} Auto
{% elif next == states('sensor.alarm_clock_manual_next_alarm') %} Manual
{% elif next == states('sensor.alarm_clock_nap_next_alarm') %} Nap
{% elif next == states('sensor.alarm_clock_other') %} {{ state_attr('sensor.alarm_clock_other','source') }}
{% else %} {{ none }}
{% endif %}
{% else %} {{ none }}
{% endif %}
12hour: >
{% from 'format.jinja' import twelve_hour %}
{{ twelve_hour(this.entity_id) }}