Play/ pause music in automation, only if playing

Hi,

I use this in my automation to speaking events.

- alias: TTS_axxon
  trigger:
    platform: state
    entity_id: input_boolean.axxon0
    to: 'on'
  action:
  - service: media_player.media_pause
  - service: tts.google_say
    entity_id: media_player.googlehome0098
    data:
      message: 'Der er aktivitet i indkørslen'
      cache: true
  - delay: '00:00:03'
  - service: media_player.media_play

It’s working great if I am listing to music, but if no music is playing when the automation starts, then the media_player.media_play starts the music.

How do I add an if loop, so that the music is only paused and played again if it was playing when the automation was triggered.

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.)

Let me know if you need a concrete example.

Do i need to create a device i can use as a boolean, or can i create one on the fly in the automation script?

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.

Hello @pnbruckner

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

  action:
  - service_template: >
      {% if is_state('media_player.XXX', 'playing') %}
        input_boolean.turn_on
      {% else %}
        input_boolean.turn_off
      {% endif %}
    entity_id: input_boolean.media_was_playing
  - ...
  - condition: state
    entity_id: input_boolean.media_was_playing
    state: 'on'
  - service: media_player.media_play
    entity_id: media_player.XXX

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.

nano .storage/input_boolean 
{
    "data": {
        "items": [
            {
                "icon": "mdi:play",
                "id": "media_was_playing",
                "name": "Media Was Playing"
            }
        ]
    },
    "key": "input_boolean",
    "version": 1
}
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.