I was looking for something like this! Thanks for this great script!
I have edited the script to fit my use case a little better:
In the Google Home app, I have bundled my two Google Home mini speakers together to a “Home Group” (the group has the name home_group. This home group is the default playback device.
This shows up as an extra media player in Home Assistant.
Whenever I say: “Hey Google, play my favourites” it will start playing my liked songs in Spotify on both speakers.
I want to play my tts on only a single device. This automatically stops playback on the entire group for some reason.
Whenever a tts comes along, I want playback to continue on both speakers.
Here are my edits:
tts_and_resume:
alias: TTS and resume
variables:
group_State: '{{ states(''media_player.home_group'') }}'
mediaplayer_State: '{{ states(tts_entity) }}'
mediaplayer_volume_level: '{{ state_attr(tts_entity,''volume_level'') }}'
mediaplayer_media_content_id: '{{ state_attr(tts_entity,''media_content_id'') }}'
mediaplayer_app_name: '{{ state_attr(tts_entity,''app_name'') }}'
mediaplayer_Source: '{{ state_attr(tts_entity,''media_channel'') }}'
sequence:
- service: media_player.volume_set
data:
entity_id: '{{ tts_entity }}'
volume_level: 0.15
- service: tts.google_translate_say
data:
entity_id: '{{ tts_entity }}'
language: '{{ language }}'
message: '{{ msg }}'
- delay:
seconds: 7
- service: media_player.volume_set
data:
entity_id: '{{ tts_entity }}'
volume_level: '{{ mediaplayer_volume_level }}'
- choose:
- conditions:
- condition: template
value_template: '{{ group_State == ''playing'' }}'
sequence:
- service: script.turn_on
target:
entity_id: script.resume_after_tts
data:
variables:
resume_entity: media_player.home_group
mediaplayer_State: '{{ mediaplayer_State }}'
mediaplayer_volume_level: '{{ mediaplayer_volume_level }}'
mediaplayer_media_content_id: '{{ mediaplayer_media_content_id }}'
mediaplayer_app_name: '{{ mediaplayer_app_name }}'
mediaplayer_Source: '{{ mediaplayer_Source }}'
default:
- service: script.turn_on
target:
entity_id: script.resume_after_tts
data:
variables:
resume_entity: '{{ tts_entity }}'
mediaplayer_State: '{{ mediaplayer_State }}'
mediaplayer_volume_level: '{{ mediaplayer_volume_level }}'
mediaplayer_media_content_id: '{{ mediaplayer_media_content_id }}'
mediaplayer_app_name: '{{ mediaplayer_app_name }}'
mediaplayer_Source: '{{ mediaplayer_Source }}'
mode: single
And the resume is done in another service:
resume_after_tts:
alias: Resume after TTS
sequence:
- choose:
- conditions:
- condition: template
value_template: '{{ mediaplayer_app_name == ''Spotify'' }}'
sequence:
- service: spotcast.start
data:
entity_id: '{{ resume_entity }}'
default:
- service: media_player.play_media
data:
entity_id: '{{ resume_entity }}'
media_content_id: '{{ mediaplayer_media_content_id }}'
media_content_type: music
- delay:
seconds: 8
- choose:
- conditions:
- condition: template
value_template: '{{ mediaplayer_Source != None }}'
sequence:
- service: media_player.select_source
data:
entity_id: '{{ resume_entity }}'
source: '{{ mediaplayer_Source }}'
- delay:
seconds: 4
- service: media_player.media_pause
data:
entity_id: '{{ resume_entity }}'
- choose:
- conditions:
- condition: template
value_template: '{{ mediaplayer_State == ''playing'' }}'
sequence:
- delay:
seconds: 4
- service: media_player.media_play
data:
entity_id: '{{ resume_entity }}'
mode: single
So it detects the state of the group and will continue playing on either the tts_entity or on the group depending on the state.
It is not the most elegant solution, but this is working fine for now.
If you have multiple groups however, you’ll have to come up with an even more clever way of knowing which group is playing.