Hello guys!
So here is the solution I’ve found
It is not the best way to do it, but it works:
## Scripts ##
ghm_play_media:
sequence:
- condition: template
value_template: "{{ not is_state('media_player.casa_inteira' , 'playing')}}"
- service: media_player.volume_set
data_template:
entity_id: >-
{%- set players = ['media_player.banheiro_social', 'media_player.banheiro_da_suite', 'media_player.cozinha', 'media_player.quarto', 'media_player.sala', 'media_player.suite'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
volume_level: '{{volume}}'
- service: media_player.play_media
data_template:
entity_id: >-
{%- if is_state('input_boolean.boa_noite', 'on') %}
{%- set players = ['media_player.banheiro_social', 'media_player.cozinha', 'media_player.quarto', 'media_player.sala'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
{%- else %}
{%- if states.media_player.banheiro_social.state != 'playing' and
states.media_player.banheiro_suite.state != 'playing' and
states.media_player.cozinha.state != 'playing' and
states.media_player.quarto.state != 'playing' and
states.media_player.sala.state != 'playing' and
states.media_player.suite.state != 'playing' %}
media_player.casa_inteira
{%- else %}
{%- set players = ['media_player.banheiro_social', 'media_player.banheiro_da_suite', 'media_player.cozinha', 'media_player.quarto', 'media_player.sala', 'media_player.suite'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
{%- endif %}
{%- endif %}
media_content_type: 'audio/mp3'
media_content_id: '{{media}}'
ghm_tts:
sequence:
- condition: template
value_template: "{{ not is_state('media_player.casa_inteira' , 'playing')}}"
- service: media_player.volume_set
data_template:
entity_id: >-
{%- set players = ['media_player.banheiro_social', 'media_player.banheiro_da_suite', 'media_player.cozinha', 'media_player.quarto', 'media_player.sala', 'media_player.suite'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
volume_level: '{{volume}}'
- service: tts.amazon_polly_say
data_template:
entity_id: >-
{%- if is_state('input_boolean.boa_noite', 'on') %}
{%- set players = ['media_player.banheiro_social', 'media_player.cozinha', 'media_player.quarto', 'media_player.sala'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
{%- else %}
{%- if states.media_player.banheiro_social.state != 'playing' and
states.media_player.banheiro_suite.state != 'playing' and
states.media_player.cozinha.state != 'playing' and
states.media_player.quarto.state != 'playing' and
states.media_player.sala.state != 'playing' and
states.media_player.suite.state != 'playing' %}
media_player.casa_inteira
{%- else %}
{%- set players = ['media_player.banheiro_social', 'media_player.banheiro_da_suite', 'media_player.cozinha', 'media_player.quarto', 'media_player.sala', 'media_player.suite'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
{%- endif %}
{%- endif %}
message: '{{tts}}'
## Tests ##
ghm_play_media_test:
sequence:
- service: script.ghm_play_media
data_template:
media: 'http://hassio.local:8123/local/audio/tts/some_audio.mp3'
volume: 0.5
ghm_tts_test:
sequence:
- service: script.ghm_tts
data_template:
tts: '<speak>Say something with Polly or Google format</speak>'
volume: 0.5
It checks if all speakers group is playing something, if so, script does not start at all. If it is not playing, get only entity_id
of devices not playing and pass the variables needed.
Also, I’ve added an “if” so it can determinate if someone has started the “good night” mode on the house (is_state('input_boolean.boa_noite', 'on')
), so it only plays the media on devices outside the main bedroom.
Another cool feature is to pass the volume needed on each announcement/media play.
I will set this as solved.
Thanks @petro and @123 for all the help!