Skip to content

Instantly share code, notes, and snippets.

@xbmcnut
Created June 22, 2020 10:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xbmcnut/09cfe7509dccb01ad512a5c6d4baa519 to your computer and use it in GitHub Desktop.
Save xbmcnut/09cfe7509dccb01ad512a5c6d4baa519 to your computer and use it in GitHub Desktop.
How to get TTS announcements without interrupting Google speakers that are already being used.
# ghm_tts:
alias: 'Google Home Notifier'
sequence:
- service: media_player.volume_set
data_template:
entity_id: >-
{%- set players = ['media_player.kitchen_home', 'media_player.lounge_home', 'media_player.bathroom_speaker', 'media_player.ensuite_speaker'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
volume_level: '{{volume}}'
- service: tts.google_say
data_template:
entity_id: >-
{%- if states('media_player.kitchen_home') != 'playing' and
states ('media_player.lounge_home') != 'playing' and
states ('media_player.bathroom_speaker') != 'playing' and
states ('media_player.ensuite_speaker') != 'playing' %}
media_player.tts_homes
{%- else %}
{%- set players = ['media_player.kitchen_home', 'media_player.lounge_home', 'media_player.bathroom_speaker', 'media_player.ensuite_speaker'] %}
{{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
{%- endif %}
message: '{{tts}}'
## How this works: First section [volume] looks at which media players are not playing (!=playing) and lists
## out all that aren't from the defined players list under the 'set players' section
## The {{volume}} level is pulled from the script that invoked this script and those that aren't playing get their volumes set.
## The next data template for tts.google_say looks again for media players that are not playing and if none are,
## it chooses the defined group of media players (media_player.upstairs_homes) which has to be set up in the Google Home app.
## So the broadcast will happen to all speakers in that group.
## If any of the speakers are playing, the script will instead list all media players out separately so as not to
## interrupt the speakers that are playing music. The actual text spoken is pulled from the {{tts}} section
## from the script invoking this script.
## N.B Instead of having {{ states.media_player.kitchen_home.state != "playing"}}, you can change
## this to {{ states.media_player.kitchen_home.attributes.media_content_type != 'music' }} which looks
## specfically if the device is actually playing music not just playing (anything). Of course, you can use any
## attribute you like.
## Help here https://community.home-assistant.io/t/help-creating-a-template-sensor-to-overcome-google-cast-issue/113159/14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment