First, please always post YAML code using proper formatting as described in the link at the top of the page.
Basically, at the start of the script you need to record whether or not the music is playing, e.g., in an input_boolean. Then at the end of the script you can add a condition step that only lets the last step (that starts the music) run if the input_boolean was set to ‘on’ (i.e. true.)
Create the Input Boolean in your configuration. Then you can use it in the script (aka action) part of your automation. I’d suggest not having it show in the frontend.
Could you please provide an example? As I have been struggling after creating an input boolean from Configuration > Helpers > + > Toogle type
My automation is a bit different but the goal is the very same: if any audio/music is playing, have it paused, the message broadcasted, then audio/music resumed
- id: '1234567890123'
alias: Someone arrived home
description: ''
trigger:
- entity_id: person.thisperson
event: enter
platform: zone
zone: zone.home
condition: []
action:
- data:
message: This person arrived home
service: notify.notify
- data:
message: This person arrived home
service: tts.google_say
Your support is appreciated in advance.
Regards,
PD. Forgot to mention my simple setup: 1 Chromecast (entity: media_player.living_room_tv) + 1 Google Home Mini (1 media_player.kitchen_speaker) + 1 Home Group (media_player.home_group) to send audio/music/notifications to both devices
Thank you for your feedback. I have had to simplify and change automation to check a change of state in the light switch to test more easily. I have run several different tests and this is the closest I have been to success.
Scenario: radio.net music is cast from mobile app to Living Room Chromecast plugged to TV set. Music is playing on the TV set.
automation.yaml
- id: '0000000000000'
alias: Living room light on
description: ''
trigger:
- entity_id: switch.living_room_light
platform: state
to: 'on'
condition: []
action:
- data: {}
service: script.1111111111111
scripts.yaml
'1111111111111':
alias: Test2
sequence:
- service_template: >
{% if is_state('media_player.living_room_tv', 'playing') %}
input_boolean.turn_on
{% else %}
input_boolean.turn_off
{% endif %}
entity_id: input_boolean.media_was_playing
- data:
message: Living room light is now on
entity_id: all
service: tts.google_say
- delay: 00:00:05
- condition: state
entity_id: input_boolean.media_was_playing
state: 'on'
- service: media_player.media_play
entity_id: media_player.living_room_tv
Output: Living room light is turned on via Home Assistant, then music stops playing on TV set (radio.net music app disconnects from TV set; Chromecast photo slideshow mode), text message is broadcast in TV set (tts) but radio.net music does not resume on TV set but actually begins playing back in mobile app! while Chromecast photo slideshow mode continues on TV set.
Thank you for your valuable feedback. Maybe there is no way Chromecast or Google Home Mini can resume music playback after all?
PD. Just in case it was the problem was casting from mobile app, I have added a Chromecast Radio Jukebox to my Home Assistant setup to find out that the very same issue occurs.