-
Notifications
You must be signed in to change notification settings - Fork 1
/
automations.yaml
1089 lines (1015 loc) · 41.2 KB
/
automations.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
# HA STARTUP - PUSH VARIOUS STATES TO PLATES
- id: openhasp_sonos_ha_startup_update_objects
alias: openHASP Sonos HA startup update objects
mode: restart
trigger:
- platform: state
entity_id:
- group.hasp_sonos_devices
attribute: entity_id
for:
seconds: 10
- platform: homeassistant
event: start
variables:
volume: >
{{ ((states("sensor.sonos_volume") | int * 2) / (1 + (states("sensor.sonos_volume") | int / 100))) | int }}
action:
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b20.val
payload_template: "{{ volume }}"
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b20.value_str
payload_template: "{{ volume }}"
- service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload_template: >
{"page":2,"id":89,"text":"{{ states('sensor.sonos_master_friendly') }}"}
- service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload_template: >
{"page":2,"id":93,"options":{% set ns=namespace(counter="")
%}{% for sources in state_attr("group.sonos_all_speakers", "entity_id")[1:]
%}{% set friendly = "#008000 " + state_attr((sources), "friendly_name")
+ "#" if sources in state_attr("group.sonos_all", "entity_id") else
"#FF0000 " + state_attr((sources), "friendly_name") + "#" %}{% set ns.counter
= ns.counter + '\"'+ friendly +'"' %}{% if not loop.last %}{% set ns.counter
= ns.counter + "," + '"\\n"' + "," %}{% endif %}{% endfor %}[{{ns.counter}}]}
- service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload_template: >
{% if states("sensor.hasp_sonos_master_state") == "paused" %}
{"page":2,"id":17,"val":0,"text":"\ue40a"}
{% elif states("sensor.hasp_sonos_master_state") == "playing" %}
{"page":2,"id":17,"val":1,"text":"#FFAC00 \ue3e4#"}
{% else %}
{"page":2,"id":17,"val":0,"text":"\ue4db"}
{% endif %}
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b98.options
payload: "{{ states('sensor.hasp_sonos_playlist_options') }}"
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b99.options
payload: "{{ states('sensor.hasp_sonos_source_options') }}"
# Update and populate group.hasp_sonos_devices dynamically with all available openHASP devices
# Updates upon changes and HA restart
- id: openhasp_sonos_change_available_openhasp_devices_group
alias: openHASP Sonos change available openHASP devices group
mode: restart
trigger:
- platform: state
entity_id: sensor.hasp_all_available_devices
- platform: homeassistant
event: start
action:
- service: group.set
data_template:
object_id: hasp_sonos_devices
entities: >
{{ states("sensor.hasp_all_available_devices") }}
# Update group.sonos_all upon HA start and change in Sonos speaker group
- id: openhasp_sonos_change_sonos_active_group_speakers
alias: openHASP Sonos change active group speakers
mode: restart
trigger:
- platform: state
entity_id: sensor.sonos_active_group_speakers
- platform: homeassistant
event: start
action:
- service: group.set
data_template:
object_id: sonos_all
entities: "{{ state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'group_members') | join(',') }}"
# Populate group.sonos_all_speakers upon HA start and change in available speakers
# entity_id[0] copied from group.sonos_all as master speaker. Remaining entities from dynamic speaker sensor
- id: openhasp_sonos_change_available_slave_speakers_group
alias: openHASP Sonos change available slave speakers group
trigger:
- platform: state
entity_id: sensor.sonos_available_slave_speakers
- platform: homeassistant
event: start
action:
- service: group.set
data_template:
object_id: sonos_all_speakers
entities: "{{ state_attr('group.sonos_all', 'entity_id')[0] }},{{ states('sensor.sonos_available_slave_speakers') }}"
# Using group commands to push updates to ALL plates !
# Remember plates have to share same object mapping (page/object number)
# Push changes on media_title- or sonos_active_speakers sensors to plate objects
- id: openhasp_sonos_push_artist_title
alias: openHASP Sonos push artist title
mode: queued
trigger:
platform: state
entity_id:
- sensor.media_title
- sensor.sonos_active_speakers
action:
- service: mqtt.publish
data_template:
payload: '{{ states(trigger.entity_id) }}'
topic: >
{% if (trigger.entity_id) == 'sensor.media_title' %}
hasp/plates/command/p2b14.text
{% else %}
hasp/plates/command/p2b26.text
{% endif %}
# Push media_artist/media_album_name to plate
# If media_title if not equal to media_album_name and media_album_name exists then add album name to artist string
- id: openhasp_push_media_artist_media_album_name_to_plate
alias: openHASP push media artist/media album name to plate
mode: restart
trigger:
platform: state
entity_id:
- sensor.media_artist
- sensor.media_album_name
action:
- service: mqtt.publish
data_template:
topic: hasp/plates/command/p2b15.text
payload: >
{% if is_state('sensor.media_album_name', '--- No album name ---') or states('sensor.media_title') == states('sensor.media_album_name') %}
{{ states('sensor.media_artist') }}
{% else %}
{{ states('sensor.media_artist') }} {{"•"|e}} {{ states('sensor.media_album_name') }}
{% endif %}
# Upon change, push media source + state to plate(s) in separate pop-up window for 5 seconds
# Mandatory: support sensor sensor.hasp_sonos_master_state
# and group.hasp_sonos_devices
# Revised 20230216. Completely dynamic now and support simultanious, mixed plates with varying display width
- id: openhasp_sonos_push_source_media
alias: openHASP Sonos push source media
mode: restart
trigger:
platform: state
entity_id: sensor.hasp_sonos_master_state
to:
- playing
- paused
condition:
condition: and
conditions:
- condition: template
value_template: "{{ states('sensor.media_source') != 'unavailable' }}"
variables:
source_type: >
{% if states('sensor.media_source') != 'no source' %}
{{ 'TuneIn' if states("binary_sensor.sonos_tunein_is_source") == 'on' else 'Source' }}
{% elif state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'media_playlist') != None %}
{{ 'Spotify playlist' if state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'media_content_id') is search('spotify', ignorecase=True) else 'Unknown playlist' }}
{% else %}
{{ 'Spotify song' if state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'media_content_id') is search('spotify', ignorecase=True) else 'Unknown song' }}
{% endif %}
source_name_status: >
{% if source_type == 'TuneIn' or source_type == "Source" %}
\n{{ state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'media_channel') }}\n#FFAC00 {{ states('sensor.hasp_sonos_master_state') }}
{% elif source_type is not search('song', ignorecase=True) %}
\n{{ state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'media_playlist')}}\n#FFAC00 {{ states('sensor.hasp_sonos_master_state') }}
{% else %}
\n\n#FFAC00 {{ states('sensor.hasp_sonos_master_state') }}
{% endif %}
action:
- repeat:
for_each: >
{{ state_attr("group.hasp_sonos_devices", "entity_id") | list }}
sequence:
- variables:
w: "{{ state_attr((repeat.item), 'tftWidth') }}"
- service: mqtt.publish
data:
topic: "hasp/{{ state_attr((repeat.item), 'node') }}/command/jsonl"
payload: >
{% if w == 800 %}
{"page":2,"id":90,"hidden":0,"x":40,"y":153,"w":275,"text_font":32,"text":"#FFAC00 {{ source_type }}{{ source_name_status }}"}
{% elif w == 480 %}
{"page":2,"id":90,"hidden":0,"x":45,"y":160,"w":220,"text_font":32,"text":"#FFAC00 {{ source_type }}{{ source_name_status }}"}
{% else %}
{"page":2,"id":90,"hidden":0,"x":10,"y":157,"w":188,"text_font":28,"text":"#FFAC00 {{ source_type }}{{ source_name_status }}"}
{% endif %}
- delay:
seconds: 5
- service: mqtt.publish
data:
topic: hasp/plates/command
payload: >-
{"page":2,"id":90,"hidden":1}
- id: openhasp_push_sonos_volume_muted
alias: openHASP push Sonos volume muted
mode: single
trigger:
- platform: state
entity_id: binary_sensor.sonos_volume_muted
- platform: homeassistant
event: start
- platform: state
entity_id:
- group.hasp_sonos_devices
attribute: entity_id
for:
seconds: 10
action:
- service: mqtt.publish
data_template:
topic: hasp/plates/command/jsonl
payload: >
{% if is_state('binary_sensor.sonos_volume_muted', 'on') %}
{"page":2,"id":24,"val":1,"text":"\ue75f"}
{% else %}
{"page":2,"id":24,"val":0,"text":"\ue57e"}
{% endif %}
## 250223 New automation
## Moved from CC event part p2b24
- id: openhasp_sonos_change_volume_muted
alias: openHASP Sonos change volume muted
mode: queued
trigger:
platform: mqtt
topic: hasp/+/state/p2b24
condition:
- condition: template
value_template: "{{ trigger.payload_json.event == 'up' }}"
action:
- service: media_player.volume_mute
data:
entity_id: group.sonos_all
is_volume_muted: "{{ false if state_attr((state_attr('group.sonos_all', 'entity_id')[0]),'is_volume_muted') else true }}"
## 250223 New automation
## Moved from CC event part p2b25
- id: openhasp_sonos_change_shuffle
alias: openHASP Sonos change shuffle
mode: queued
trigger:
platform: mqtt
topic: hasp/+/state/p2b25
condition:
- condition: template
value_template: "{{ trigger.payload_json.event == 'up' }}"
action:
- service: media_player.shuffle_set
data:
entity_id: group.sonos_all
shuffle: "{{ false if state_attr((state_attr('group.sonos_all', 'entity_id')[0]),'shuffle') else true }}"
- id: openhasp_push_sonos_shuffle
alias: openHASP push Sonos shuffle
mode: single
trigger:
- platform: state
entity_id: binary_sensor.sonos_shuffle
- platform: homeassistant
event: start
- platform: state
entity_id:
- group.hasp_sonos_devices
attribute: entity_id
for:
seconds: 10
action:
- service: mqtt.publish
data_template:
topic: hasp/plates/command/jsonl
payload: >
{% if is_state('binary_sensor.sonos_shuffle', 'on') %}
{"page":2,"id":25,"val":1,"text":"\ue49d"}
{% else %}
{"page":2,"id":25,"val":0,"text":"\ue49e"}
{% endif %}
## 250223 New automation
## Moved from CC event part p2b27
- id: openhasp_sonos_change_repeat
alias: openHASP Sonos change repeat
mode: queued
trigger:
platform: mqtt
topic: hasp/+/state/p2b27
condition:
- condition: template
value_template: "{{ trigger.payload_json.event == 'up' or trigger.payload_json.event == 'release' }}"
action:
- service: media_player.repeat_set
data:
entity_id: group.sonos_all
repeat: >
{% if is_state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'repeat', 'one') %}
all
{% elif is_state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'repeat', 'all') %}
off
{% elif is_state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'repeat', 'off') %}
one
{% endif %}
- id: openhasp_push_sonos_repeat
alias: openHASP push Sonos repeat
mode: single
trigger:
- platform: state
entity_id: sensor.sonos_repeat
- platform: homeassistant
event: start
- platform: state
entity_id:
- group.hasp_sonos_devices
attribute: entity_id
for:
seconds: 10
action:
- service: mqtt.publish
data_template:
topic: hasp/plates/command/jsonl
payload: >
{% if is_state('sensor.sonos_repeat', 'one') %}
{"page":2,"id":27,"text_color":"#FFAC00","text":"\ue458"}
{% elif is_state('sensor.sonos_repeat', 'all') %}
{"page":2,"id":27,"text_color":"#FFAC00","text":"\ue456"}
{% else %}
{"page":2,"id":27,"text_color":"#C7BAA7","text":"\ue457"}
{% endif %}
# Hide/show progress bar & enable/disable shuffle & repeat buttons when shifting from/to playlist as source
# Wait one second before proceeding (HA's media entities can 'flicker' a bit upon change)
# Only trigger on state changes to/from 0 on entity's media_duration. This is always 0 when non playlists are selected.
- id: openhasp_sonos_hide_progress_bar
alias: openHASP Sonos hide progress bar
mode: restart
trigger:
- platform: state
entity_id: sensor.hasp_sonos_media_duration
to: '0'
for:
seconds: 1
- platform: state
entity_id: sensor.hasp_sonos_media_duration
from: '0'
action:
- service: mqtt.publish
data_template:
topic: hasp/plates/command/jsonl
payload_template: >
{% if trigger.to_state.state == '0' %}
{'page':2,'id':41,'hidden':1}{'page':2,'id':25,'enabled':0}{'page':2,'id':27,'enabled':0}
{% else %}
{'page':2,'id':41,'hidden':0}{'page':2,'id':25,'enabled':1}{'page':2,'id':27,'enabled':1}
{% endif %}
# 260223 New automation
# Moved from CC properties part p2b41
- id: openhasp_sonos_push_max_duration_to_plates
alias: openHASP Sonos push max duration to plates
mode: restart
trigger:
- platform: state
entity_id: sensor.hasp_sonos_media_duration
action:
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b41.max
payload_template: "{{ states('sensor.hasp_sonos_media_duration') | int }}"
# 260223 New automation
# Moved from CC properties part p2b41
- id: openhasp_sonos_push_media_position_to_plates
alias: openHASP Sonos push media position to plates
mode: restart
trigger:
- platform: state
entity_id: sensor.hasp_sonos_media_position
action:
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b41.val
payload_template: "{{ states('sensor.hasp_sonos_media_position') | int }}"
# Listen for changes from ALL plates (+ in topic) !
# Remember plates have to share same object mapping (page/object number)
# Push volume value string setting immediately to all plates
# Push volume setting from hasp to media_player_group
# Plates are using a Bezier algorithm in order to adjust volume at low levels much easier that if using a linear slider
# https://community.home-assistant.io/t/smooth-media-player-volume-fade-with-multiple-curve-functions/349950
- id: openhasp_sonos_volume_receive_from_plates
alias: openHASP Sonos volume - receive from plates
mode: restart
trigger:
platform: mqtt
topic: hasp/+/state/p2b20
action:
- service: media_player.volume_set
data_template:
entity_id:
- group.sonos_all
volume_level: '{{ trigger.payload_json.val | float / 100(1) / (1 + (1 - trigger.payload_json.val | float(1) / 100 )) }}'
# Push Sonos volume to plates as 'converted' values. Both val and value_str are pushed
# Plates are using a Bezier algorithm in order to adjust volume at low levels much easier that if using a linear slider
# https://community.home-assistant.io/t/smooth-media-player-volume-fade-with-multiple-curve-functions/349950
- id: openhasp_sonos_volume_push_to_plates
alias: openHASP Sonos volume - push to plates
mode: restart
trigger:
platform: state
entity_id: sensor.sonos_volume
condition:
condition: template
value_template: "{{ now().timestamp() - as_timestamp(states.sensor.hasp_volume.last_changed) > 1 }}"
# Set offset for volume setting within the volume slider
# Specific values needed, depending on slider width
# 320 pixel width devices: -98 & 98
# 480/800 pixel width devices: -170 & 170
# sensor.sonos_volume = 60 equals value 75 on volume slider
# Here we push sensor.sonos_volume (trigger.to_state_state)
# Revised 20230213: Dynamic, individual value_ofs_x pushed to each plate
action:
- repeat:
for_each: >
{{ state_attr("group.hasp_sonos_devices", "entity_id") | list }}
sequence:
- variables:
w: "{{ state_attr((repeat.item), 'tftWidth') }}"
- service: mqtt.publish
data:
topic: "hasp/{{ state_attr((repeat.item), 'node') }}/command/p2b20.value_ofs_x"
payload: >
{% if trigger.to_state.state | int > 60 and (w == 480 or w == 800) %}
-170
{% elif trigger.to_state.state | int > 60 and w == 320 %}
-98
{% elif trigger.to_state.state | int <= 60 and (w == 480 or w == 800) %}
170
{% else %}
98
{% endif %}
- variables:
vol: "{{ ((trigger.to_state.state | int * 2) / (1 + (trigger.to_state.state | int / 100))) | int }}"
- service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload: >
{"page":2,"id":20,"val":{{ vol }}}{"page":2,"id":20,"value_str":{{ vol }}}
# Push volume slider value to all plates upon slider release
# Value pushed and shared is from the active plates operating slider - NOT HA's volume sensors state value
# value_str is not updated on the other plates. Only upon local slider changes. So we have to update value_str as well
# Revised 20230213: Dynamic, individual value_ofs_x pushed to each plate
- id: openhasp_sonos_push_volume_slider_value_to_all_plates
alias: openHASP Sonos push volume slider value to all plates
mode: restart
trigger:
- platform: mqtt
topic: hasp/+/state/p2b20
condition:
- condition: template
value_template: "{{ trigger.payload_json.event == 'up' }}"
# Set offset for volume setting within the volume slider
# Specific values needed, depending on slider width
# 320 pixel width devices: -98 & 98
# 480/800 pixel width devices: -170 & 170
# sensor.sonos_volume = 60 equals value 75 on volume slider
# Here we directly push slider value from plate (trigger.payload_json_val)
action:
- repeat:
for_each: >
{{ state_attr("group.hasp_sonos_devices", "entity_id") | list }}
sequence:
- variables:
w: "{{ state_attr((repeat.item), 'tftWidth') }}"
- service: mqtt.publish
data:
topic: "hasp/{{ state_attr((repeat.item), 'node') }}/command/p2b20.value_ofs_x"
payload: >
{% if trigger.payload_json.val | int > 75 and (w == 480 or w == 800) %}
-170
{% elif trigger.payload_json.val | int > 75 and w == 320 %}
-98
{% elif trigger.payload_json.val | int <= 75 and (w == 480 or w == 800) %}
170
{% else %}
98
{% endif %}
- service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload: >
{"page":2,"id":20,"val":{{ trigger.payload_json.val }}}{"page":2,"id":20,"value_str":{{ trigger.payload_json.val }}}
# Listen for changes from ALL plates (+ in topic) !
# Remember plates have to share same object mapping (page/object number)
# More simple 'media_player.media_play_pause' service can't be used, as HA Sonos integration has 'issues/bugs' with this service, using specific slave speaker configurations
# Should be retested when Sonos integration has been updated
- id: openhasp_sonos_play_toggle
alias: openHASP Sonos play toggle
mode: single
trigger:
- platform: mqtt
topic: hasp/+/state/p2b17
condition:
- condition: template
value_template: "{{ trigger.payload_json.event == 'down' }}"
action:
service_template: >-
{% if trigger.payload_json.val == 1 %}
media_player.media_pause
{% elif trigger.payload_json.val == 0 %}
media_player.media_play
{% else %}
media_player.media_stop
{% endif %}
data:
entity_id:
- group.sonos_all
# Push available slave speakers to plate with updated status (red or green)
# Press slave speaker button to toggle speakers join/unjoin status
# On 320 & 480 width devices, choose Sources or Playlists from tab and press 'arrow down' to end and close tab
# 050223: Revised trigger from plate objects to slave speakers group/all available speakers entity_id change
# 050223: Added update to primary speaker object (p2b89)
- id: openhasp_sonos_slave_speakers_to_plate
alias: openHASP Sonos slave speakers to plate
mode: restart
trigger:
- platform: state
entity_id:
- group.sonos_all
- group.sonos_all_speakers
attribute: entity_id
action:
- service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload_template: >
{"page":2,"id":89,"text":"{{ state_attr(state_attr('group.sonos_all_speakers', 'entity_id')[0], 'friendly_name') }}"}
- service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload_template: >
{"page":2,"id":93,"options":{% set ns=namespace(counter="") %}{% for sources in state_attr('group.sonos_all_speakers', 'entity_id')[1:] %}{% set friendly = "#008000 " + state_attr((sources), 'friendly_name') + '#' if sources in state_attr('group.sonos_all', 'entity_id') else "#FF0000 " + state_attr((sources), 'friendly_name') + '#' %}{% set ns.counter = ns.counter + '\"'+ friendly +'"' %}{% if not loop.last %}{% set ns.counter = ns.counter + ',' + '"\\n"' + ',' %}{% endif %}{% endfor %}[{{ns.counter}}]}
- id: openhasp_sonos_slave_speakers_unjoin
alias: openHASP Sonos slave speakers unjoin
mode: queued
trigger:
platform: mqtt
topic: hasp/+/state/p2b93
variables:
val: "{{ trigger.payload_json.val | int(0) }}"
entity: "{{ state_attr('group.sonos_all_speakers', 'entity_id')[1:] }}"
condition:
condition: and
conditions:
- condition: template
value_template: "{{ true if entity[val] in state_attr('group.sonos_all', 'entity_id') else false }}"
- condition: template
value_template: "{{ trigger.payload_json.event == 'down' }}"
action:
- service: media_player.unjoin
data:
entity_id: "{{ entity[val] }}"
# Now uses sensor.sonos_volume which is the correct master speaker / HA volume
# Join speakers one by one from toggle button on plate
# Set mute status and volume to match master speaker BEFORE joining speaker !
- id: openhasp_sonos_slave_speakers_join
alias: openHASP Sonos slave speakers join
mode: queued
trigger:
platform: mqtt
topic: hasp/+/state/p2b93
variables:
val: "{{ trigger.payload_json.val | int(0) }}"
entity: "{{ state_attr('group.sonos_all_speakers', 'entity_id')[1:] }}"
condition:
condition: and
conditions:
- condition: template
value_template: "{{ true if entity[val] not in state_attr('group.sonos_all', 'entity_id') else false }}"
- condition: template
value_template: "{{ trigger.payload_json.event == 'down' }}"
action:
- service: media_player.volume_mute
data:
entity_id: "{{ entity[val] }}"
is_volume_muted: "{{ true if state_attr((state_attr('group.sonos_all', 'entity_id')[0]), 'is_volume_muted') else false }}"
- service: media_player.volume_set
data_template:
entity_id: "{{ entity[val] }}"
volume_level: "{{ states.sensor.sonos_volume.state | float(1) / 100}}"
- service: media_player.join
data:
entity_id: "{{ state_attr('group.sonos_all', 'entity_id')[0] }}"
group_members: "{{ entity[val] }}"
- id: openhasp_sonos_change_master_speaker_btnmtrx
alias: openHASP Sonos change master speaker btnmtrx
mode: single
trigger:
- platform: mqtt
topic: hasp/+/state/p2b89
- platform: mqtt
topic: hasp/+/state/p2b95
# Check for either Sunton 7" device master speaker button 'hold' action or
# tab menu 'Master' value (object p2b95) on all other devices
condition:
condition: template
value_template: "{{ trigger.payload_json.event == 'hold' or (trigger.payload_json.val == 3 and trigger.topic.split('/')[-1] == 'p2b95') }}"
# Create option list for master speaker button matrix
# Skip first entity_id in group.sonos_all_speakers as this entry is always current master speaker
variables:
sas: >
{% set ns=namespace(counter="") %}
{% for sources in state_attr("group.sonos_all_speakers", "entity_id")[1:] %}
{% set friendly = "#0000FF " + state_attr((sources), "friendly_name") + "#" if sources in state_attr("group.sonos_all_speakers", "entity_id") else "#FF0000 " + state_attr((sources), "friendly_name") + "#" %}
{% set ns.counter = ns.counter + '\"'+ friendly +'"' %}
{% if not loop.last %}
{% set ns.counter = ns.counter + "," + '"\\n"' + "," %}
{% endif %}
{% endfor %}
{{ ns.counter }}
action:
- service: mqtt.publish
data:
topic: "hasp/{{trigger.topic.split('/')[1]}}/command/jsonl"
payload: >
{% if state_attr(("openhasp." + trigger.topic.split('/')[1]), "tftWidth") == 800 %}
{"page":2,"id":93,"hidden":1}{"page":2,"id":88,"hidden":0,"options":[{{sas}},"\n","#FFFFFF E X I T#"]}
{% else %}
{"page":2,"id":88,"hidden":0,"options":[{{sas}}]}
{% endif %}
- id: openhasp_sonos_choose_new_master_speaker_group
alias: openHASP Sonos choose new master speaker group
mode: single
trigger:
- platform: mqtt
topic: hasp/+/state/p2b88
condition:
condition: template
value_template: "{{ trigger.payload_json.event == 'down' }}"
# Store chosen speaker in variable
# Skip entity_id[0] in search = current master speaker (not included in button matrix)
variables:
# Store chosen speaker in variable. Data will be lost before automation has finished
# Skip entity_id[0] in search = current master speaker (not included in button matrix)
# Skip entity_id if chosen speaker is 'E X I T'
cs: >
{% if trigger.payload_json.text != '#FFFFFF E X I T#' %}
{{ state_attr('group.sonos_all_speakers','entity_id')[trigger.payload_json.val + 1] }}
{% endif %}
# Check if chosen speaker already is part of existing group and store it
# Data will be lost before automation has finished
in_group: >
{% set ns = namespace(ig = "not_in_group") %}
{% for entity_id in state_attr("group.sonos_all", "entity_id") %}
{% if entity_id == cs %}
{% set ns.ig = entity_id %}
{% endif %}
{% endfor %}
{{ true if ns.ig != "not_in_group" else false }}
action:
# If device is a 7" Sunton, then hide master speaker button matrix and unhide slave speaker button matrix
# Any other devices just 'emulate' an 'arrow up' press in a dropdown menu and exit tab menu completely
- service: mqtt.publish
data:
topic: "hasp/{{trigger.topic.split('/')[1]}}/command/jsonl"
payload: >
{% if state_attr(("openhasp." + trigger.topic.split('/')[1]), "tftWidth") == 800 %}
{"page":2,"id":88,"hidden":1}{"page":2,"id":93,"hidden":0}
{% else %}
{'page':2,'id':95,'to_back':1,'hidden':1}{'page':2,'id':23,'hidden':1}{'page':2,'id':26,'hidden':1}{'page':2,'id':22,'hidden':0}{'page':2,'id':15,'hidden':0}
{% endif %}
# Skip last service calls if chosen speaker is 'E X I T'
- condition:
- condition: template
value_template: "{{ trigger.payload_json.text != '#FFFFFF E X I T#' }}"
# Choose new chosen master speaker (group)
- service: group.set
data_template:
object_id: sonos_all
entities: >
{{ state_attr("group.sonos_all_speakers","entity_id")[trigger.payload_json.val + 1]}}
# If chosen speaker is not 'E X I T' and chosen speaker already is part of existing group
# then unjoin speaker from current group
- if:
- condition: template
value_template: >
{{ in_group }}
then:
- service: media_player.unjoin
data_template:
entity_id: "{{ cs }}"
else:
# Using group commands to push updates to ALL plates !
# Remember plates have to share same object mapping (page/object number)
# Color code for button toggle value 'on' not set as static in jsonl on purpose.
# This in order NOT to change color immediately on button press, but instead wait for attribute change to 'playing'
- id: openhasp_sonos_set_play/pause/idle_icon
alias: openHASP Sonos set play/pause/idle icon
mode: parallel
trigger:
- platform: state
# Dynamic entity_id trigger - Sonos master speaker state
entity_id:
- sensor.hasp_sonos_master_state
to:
- playing
- paused
- idle
action:
service: mqtt.publish
data:
topic: hasp/plates/command/jsonl
payload_template: >-
{% if trigger.to_state.state == 'paused' %}
{"page":2,"id":17,"val":0,"text":"\ue40a"}
{% elif trigger.to_state.state == 'playing' %}
{"page":2,"id":17,"val":1,"text":"#FFAC00 \ue3e4#"}
{% else %}
{"page":2,"id":17,"val":0,"text":"\ue4db"}
{% endif %}
# Every five seconds - Push media_duration to plates when playing
# Don't push update for Sonos sources (radio stations) - check for media_position attribute presence
# When paused, page will get exact update from HA's media player state changes and update plates
## 0303023 Revised media_duration to cope with Spotify Radio
- id: openhasp_update_media_duration
alias: openHASP update media duration
mode: single
trigger:
platform: time_pattern
seconds: '/5'
condition:
condition: template
value_template: "{{ (true if state_attr(state_attr('group.sonos_all','entity_id')[0], 'media_position') != None else false) and states('sensor.hasp_sonos_master_state') == 'playing' }}"
action:
service: mqtt.publish
data_template:
topic: hasp/plates/command/p2b41.val
payload: >-
{{ (as_timestamp(now(),0) - as_timestamp(state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'media_position_updated_at'),0)) | round(0) + (state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'media_position')) | int(0) }}
# Listen for changes from ALL plates (+ in topic) !
# Remember plates have to share same object mapping (page/object number)
# Only change for sources (radio stations) - not for playlists
# 190822: Revised to use new sensor.sonos_favorites as source_list attribute has been deprecated
# 240223: Revised to one combined automation for both previous/next events
# 030323: Revised condition_template to cope with e.g. Spotify Radio 'channels' and treat these as a 'playlist' instead of a 'source'
# It's possible to skip to next track when playing. Skipping when paused is not possible.
# Previous track can't be selected
- id: openhasp_sonos_prev_next_source
alias: openHASP Sonos previous/next source
mode: single
trigger:
- platform: mqtt
topic: hasp/+/state/p2b18
payload: '{"event":"down"}'
- platform: mqtt
topic: hasp/+/state/p2b16
payload: '{"event":"down"}'
condition:
- condition: template
value_template: "{{ true if state_attr(state_attr('group.sonos_all','entity_id')[0], 'media_duration') else false }}"
action:
- service: media_player.select_source
data_template:
entity_id: "{{ state_attr('group.sonos_all', 'entity_id')[0] }}"
source: >
{% set source_start = states('sensor.sonos_source_index_start') | int(0) %}
{% set entity_id = state_attr('group.sonos_all', 'entity_id')[0] %}
{% set current = states('sensor.media_source') %}
{% set source_list = state_attr('sensor.sonos_favorites', 'items').values() | list %}
{% set index = source_list.index(current) %}
{% if trigger.topic.split('/')[3] == 'p2b18' %}
{% set next = source_start if current == source_list[-1] else index + 1 %}
{{ source_list[next] }}
{% else %}
{% set prev = -1 if current == source_list[source_start] else index - 1 %}
{{ source_list[prev] }}
{% endif %}
# Push source state changes to dropdown objects on all plates ("show_selected":1 attribute on dropdown object)
# Update current attribute, if source is in favourites list
# Check if source is playlist as condition
# Template will fail (warning) if source is not in favourites list due to use of .index()
- id: openhasp_sonos_push_source_change_to_dropdown_object
alias: openhasp Sonos push source change to dropdown object
mode: single
trigger:
platform: state
entity_id: sensor.media_source
condition:
- condition: template
value_template: '{{ states("sensor.media_source") != "no source" }}'
action:
- service: mqtt.publish
data_template:
topic: hasp/plates/command/p2b99.val
payload: >
{% set source_start = states('sensor.sonos_source_index_start') | int(0) %}
{% set entity_id = state_attr('group.sonos_all', 'entity_id')[0] %}
{% set current = states('sensor.media_source') %}
{% set source_list = state_attr('sensor.sonos_favorites', 'items').values() | list %}
{% set index = source_list.index(current) %}
{{ index - source_start }}
# Next/previous track - Only change for playlists - not for sources (radio stations)
- id: openhasp_sonos_next_previous_track
alias: openHASP Sonos next/previous track
mode: single
trigger:
- platform: mqtt
topic: hasp/+/state/p2b16
payload: '{"event":"down"}'
- platform: mqtt
topic: hasp/+/state/p2b18
payload: '{"event":"down"}'
condition:
- condition: template
value_template: "{{ states('sensor.media_source') == 'no source' }}"
action:
service_template: >-
{{ 'media_player.media_previous_track' if trigger.topic.split('/')[3] == 'p2b16' else 'media_player.media_next_track' }}
data:
entity_id: "{{ state_attr('group.sonos_all', 'entity_id')[0] }}"
## 240223 New automation
## Moved from CC event part p2b98/p2b99
- id: openhasp_sonos_select_source_from_dropdown_list
alias: openHASP Sonos select source from dropdown list
mode: queued
trigger:
- platform: mqtt
topic: hasp/+/state/p2b98
- platform: mqtt
topic: hasp/+/state/p2b99
condition:
- condition: template
value_template: "{{ trigger.payload_json.event == 'up' or trigger.payload_json.event == 'changed' }}"
action:
# In sequence: Hide and push tabview object to background
# Hide speaker glyph & master and slave speaker text
# Show Artist glyph & Artist/Artist & album text
- service: mqtt.publish
data:
topic: "hasp/{{trigger.topic.split('/')[1]}}/command/jsonl"
payload: "{'page':2,'id':95,'to_back':1,'hidden':1}{'page':2,'id':23,'hidden':1}{'page':2,'id':26,'hidden':1}{'page':2,'id':22,'hidden':0}{'page':2,'id':15,'hidden':0}"
# Only 'changed' event should alter source selection
- condition: template
value_template: "{{ trigger.payload_json.event == 'changed' }}"
# Calculate source_list indexed to be chosen from returned [val] + number of playlist 'sources'
# Can't just use returned [text] as some sources ends with '|' if their title/artist attributes are to be flipped
# Or
# Show all sources (playlists) starting with '*'. 'Real' sources are without prefixed asterix ('*') and shown in separate tab (p2b99)
# Show on plate without prefixed '*' and add again to source upon selection
- service: media_player.select_source
data:
entity_id: "{{ state_attr('group.sonos_all', 'entity_id')[0] }}"
source: >
{% if trigger.topic.split('/')[3] == 'p2b98' %}
*{{ trigger.payload_json.text }}
{% else %}
{{ (state_attr('sensor.sonos_favorites', 'items').values() | list)[states('sensor.sonos_source_index_start') | int + trigger.payload_json.val] }}
{% endif %}
## 240223 New automation
## Moved from CC properties part p2b98
# Show sources (playlists) on plate without preceeding '*' and add again to source upon selection
- id: openhasp_sonos_push_playlist_updates_to_plates
alias: openhasp Sonos push playlist updates to plates
mode: single
trigger:
platform: state
entity_id: sensor.hasp_sonos_playlist_options
action:
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b98.options
payload: "{{ states('sensor.hasp_sonos_playlist_options') }}"
## 240223 New automation
## Moved from CC properties part p2b99
# Filter out all sources starting with '*'. These are playlists to be shown in separate tab (p2b98)
- id: openhasp_sonos_push_source_list_updates_to_plates
alias: openhasp Sonos push source list updates to plates
mode: single
trigger:
platform: state
entity_id: sensor.hasp_sonos_source_options
action:
- service: mqtt.publish
data:
topic: hasp/plates/command/p2b99.options
payload: "{{ states('sensor.hasp_sonos_source_options') }}"
# Dynamic support for both 320, 480 and 800 pixel width devices
# Target entity_id is now also dynamic and templated.
# Zoom service removed as push_image now supports upscaling of images if fitscreen argument is set in data field
- id: openhasp_push_image_to_plate
alias: openHASP push image to plate
mode: restart
trigger:
- platform: state
entity_id: sensor.hasp_sonos_master_image
# Delay needed in order to remove some of the double images pushed to the plates (source still often generates 2 images though)
for:
milliseconds: 250
- platform: homeassistant
event: start
- platform: state
entity_id:
- group.hasp_sonos_devices
attribute: entity_id
for:
seconds: 10
condition:
- condition: template
value_template: "{{ state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'entity_picture') != None }}"
# Push images for each openHASP entity in group.hasp_sonos_devices
# with dynamic width/height from each device sensors tftWidth attribute
# using local HA IP address from HA integration - Internal IP
# Automation now supports full dynamic mix of different image sizes
# Width/height for Sunton 7" device is set to 355
# Image width/height for all other devices will be identical with device width
action:
- repeat:
for_each: >
{{ state_attr("group.hasp_sonos_devices", "entity_id") | list }}
sequence:
- variables:
w: "{{ state_attr((repeat.item), 'tftWidth') }}"
- service: openhasp.push_image
data:
obj: p2b40
image: "http://{{states('sensor.local_ip')}}:8123{{state_attr(state_attr('group.sonos_all', 'entity_id')[0], 'entity_picture')}}"
fitscreen: 1
width: >
{% if w == 800 %}
355
{% else %}
{{ w }}
{% endif %}