@Reijer and others
I’ve been doing some updates to the script to support resuming playback on speaker groups and multiply Spotify accounts. I’ve also made it generic, so it can be used for all the Google Home speakers, and not only one specific speaker.
Prerequisites and comments for this new version:
- You need to create groups in home assistant with the media_players belonging to the speaker groups in the Google Home app. This is used for the
player_resume
variable
- My speaker group media_player.home_group contains all my Google Home speakers, so that is why I did not include a check if the speaker is belonging to that group for the
player_resume
variable
- In spotcast there is one primary account, you need to enter that one in the variable
primary_spotcast
- For Spotify you need to add the accounts to the Spotify integration, and also to spotcast. Be sure to name the spotify entity_id’s like
media_player.spotify_{{ spotcast user }}
- After the script has been called (e.g. because it announced the doorbell rang) the
app_name
for radio will no longer be TuneIn Free
. So the script did not recognize anymore that radio was playing. As a work around I added a variable where the first part of the TuneIn url of frequently used radio stations can be added, so the media_content_id
can be compared to that list.
- I’ve added a variable where you can add Google Nest Hubs (the ones with screen). If a TTS was played on such a device, and no music is resumed, the TTS cast icon was not removed from the screen.
- I’m using Microsoft TTS, you eed to change the TTS service to yours if you are not using Microsoft
- I’ve added fields, so you see which variables can be entered when using the GUI, both in yaml mode, and full GUI. player and tts_message are required. If volume is not entered it will use the
volume_old
variable for the TTS volume (so basically it will remain the same)
- There is a short delay for updating the
media_content_id
attribute for the Spotify integration. I use that to determine which spotcast account should be used. So therefor in case there is only one Spotify account active, it will use that one. In case there is more than one active and the new song just started, it could be that the account is not recognized. In that case the primary account will be used.
The script can be started like this in an automation or script:
In case you want to have your script/automation wait for the TTS script to be completed
- alias: "TTS voor Nest Hub Kitchen"
service: script.google_home_say
data:
tts_message: "Hello, hello! This is a test!"
player: media_player.google_kitchen
volume: 0.35
In case you want your script/automation to resume with the next action immediately
- alias: "TTS voor Nest Hub Kitchen"
service: script.turn_on
target:
entity_id: script.google_home_say
data:
variables:
tts_message: "Hello, hello! This is a test!"
player: media_player.google_kitchen
volume: 0.35
And this is then the amended script:
google_home_say:
alias: "TTS for Google Home"
icon: mdi:cast-audio
mode: parallel
fields:
tts_message:
description: "Message to be uses as TTS for Google Home."
example: "Hello, this is a test message."
required: true
selector:
text:
player:
description: "The target Google Home."
example: media_player.google_keuken
required: true
selector:
entity:
integration: cast
domain: media_player
volume:
description: "Volume for TTS message (value between 0 and 1)."
example: 0.25
required: false
selector:
number:
min: 0
max: 1
step: 0.05
mode: slider
variables: # General variables for script
players_screen:
- media_player.google_kitchen
# TuneIn stream url without http(s):// until first /. Used because app_name
# is not visible after this script has been used before in the same session
frequent_radio:
- icecast.omroep.nl
- playerservices.streamtheworld.com
# Make sure the entity_id's match this format, and the last part should
# match the spotcast account
spotify_media_players:
- media_player.spotify_user1
- media_player.spotify_user2
- media_player.spotify_user3
primary_spotcast: "user1"
sequence:
- alias: "Variables for this specific run of the script"
variables:
volume_old: >
{{ state_attr(player, 'volume_level') | default('0.25', true) | round(2) }}
spotify: >
{{ is_state_attr(player, 'app_name', 'Spotify') }}
radio: >
{{
is_state_attr(player, 'app_name', 'TuneIn Free')
or (state_attr(player, 'media_content_id')
.split('//')[1]| default('no/tunein', true)).split('/')[0]
in frequent_radio
}}
media_content: >
{{ state_attr(player, 'media_content_id') | default('geen', true) }}
player_resume: >
{% if is_state('media_player.home_group', 'playing') %}
media_player.home_group
{%
elif ((player in state_attr('group.first_floor_group', 'entity_id'))
and is_state('media_player.first_floor_group', 'playing'))
%}
media_player.first_floor_group
{%
elif ((player in state_attr('ground_floor_group', 'entity_id'))
and is_state('media_player.ground_floor_group', 'playing'))
%}
media_player.ground_floor_group
{% else %}
{{ player }}
{% endif %}
screen: "{{ player in players_screen }}"
spotcast_account: >
{%
set spotify_playing = expand(spotify_media_players)
| selectattr('state', 'eq', 'playing')
| map(attribute='entity_id')
| list
| count
%}
{% if spotify_playing == 1 %}
{{
(
expand(spotify_media_players)
| selectattr('state', 'eq', 'playing')
| map(attribute='entity_id')
| join
).split('_')[2]
}}
{% else %}
{{
(
expand(spotify_media_players)
| selectattr('attributes.media_content_id', 'eq', media_content)
| map(attribute='entity_id')
| join
).split('_')[2] | default(primary_spotcast, true)
}}
{% endif %}
- alias: "Apply TTS volume"
service: media_player.volume_set
target:
entity_id: "{{ player }}"
data:
volume_level: "{{ volume | default(volume_old, true) }}"
- alias: "Send TTS message"
service: tts.microsoft_say
data:
entity_id: "{{ player }}"
message: "{{ tts_message }}"
- alias: "Short delay to make sure the TTS message has started"
delay: 2
- alias: "Wait until TTS message is complete"
wait_template: "{{ states(player) in ['idle', 'off'] }}"
- alias: "Set volume back to old state"
service: media_player.volume_set
target:
entity_id: "{{ player }}"
data:
volume_level: "{{ volume_old }}"
- alias: "Google Home with screen?"
choose:
- conditions: "{{ screen }}"
sequence:
- alias: "Turn Google Home on to return to idle mode (photo display)"
service: media_player.turn_on
target:
entity_id: "{{ player }}"
- alias: "Was something playing?"
choose:
- alias: "Spotify?"
conditions: "{{ spotify }}"
sequence:
- alias: "Primary spotcast account?"
choose:
- conditions: "{{ spotcast_account == primary_spotcast }}"
sequence:
- alias: "Resume Spotify with primary spotcast account"
service: spotcast.start
data:
entity_id: "{{ player_resume }}"
default:
- alias: "Resume Spotify with specific account"
service: spotcast.start
data:
entity_id: "{{ player_resume }}"
account: "{{ spotcast_account }}"
- alias: "Radio?"
conditions: "{{ radio }}"
sequence:
- alias: "Resume radio"
service: media_player.play_media
target:
entity_id: "{{ player_resume }}"
data:
media_content_id: "{{ media_content }}"
media_content_type: "music"