Play two mp3 in automation

Hi
In automation I want to play 2 mp3s. I configured this but only the last file played.

- id: gg_food_waste  
  alias: gg_food_waste
  trigger:
    platform: time
    at: '19:45:00'
  condition:
    condition: time
    weekday:
      - tue
      - thu
      - sat
  action:
  - data:
      entity_id: group.google_mini
      volume_level: '0.7'
    service: media_player.volume_set
  - data:
      entity_id: group.google_mini
      media_content_id: '/media/local/1.mp3'
      media_content_type: audio/mp3
    service: media_player.play_media  
  - data:
      entity_id: group.google_mini
      media_content_id: '/media/local/2.mp3'
      media_content_type: audio/mp3
    service: media_player.play_media      

I also tried to insert a play_pause but I didn’t get any effect. Any idea? Thanks

The only way, I got this working, is adding a delay. This delay must be one second longer, than the first mp3.

The automation does not look, how long your mp3 is. It fire’s the action to play the first file, and right after it it fire’s the second, so the first is “overruled”.

This way the automation wait’s, until the first file finish and fire’s the second command after the delay.

Works fine here…

  alias: gg_food_waste
  trigger:
    platform: time
    at: '19:45:00'
  condition:
    condition: time
    weekday:
      - tue
      - thu
      - sat
  action:
  - data:
      entity_id: group.google_mini
      volume_level: '0.7'
    service: media_player.volume_set
  - data:
      entity_id: group.google_mini
      media_content_id: '/media/local/1.mp3'
      media_content_type: audio/mp3
  - delay: '00:04:32' #The lenght of the first mp3 plus one second
    service: media_player.play_media  
  - data:
      entity_id: group.google_mini
      media_content_id: '/media/local/2.mp3'
      media_content_type: audio/mp3
    service: media_player.play_media      

An alternative to using delay is a wait_template that waits for the media_player’s state to not be playing (in other words, it waits until the media_player has finished playing the first mp3 file). If you do this, don’t overlook to include a timeout value so (in case something goes wrong) it doesn’t wait forever.

For more information, see the example in the documentation for wait_template.

Before the wait template Tara’s talk about there must be at least two seconds delay to make sure the state of the media player changes to “playing”

Hellis81 is right; it takes a finite amount of time for the media_player to report it’s playing the first mp3 so the wait_template might act on the media_player’s initial non-playing state. The inclusion of a very short delay would ensure the media_player has time to report its latest state prior to executing the wait_template.

Another approach, that wouldn’t need any delay, would be to use a wait_for_trigger. It can employ a State Trigger that looks for a state-change like from: playing with an optional to: idle to indicate when the media_player has ceased playing.

  action:
  - service: media_player.volume_set
    target:
      entity_id: group.google_mini
    data:
      volume_level: '0.7'
  - service: media_player.play_media
    target:
      entity_id: group.google_mini
    data:
      media_content_id: '/media/local/1.mp3'
      media_content_type: audio/mp3
  - wait_for_trigger:
      - platform: state
        entity_id: group.google_mini
        from: playing
        to: idle
    timeout:
      seconds: 10
  - service: media_player.play_media
    target:
      entity_id: group.google_mini
    data:
      media_content_id: '/media/local/2.mp3'
      media_content_type: audio/mp3

Hi. For you answer.
I tried your suggestion. But an error is generated:

invalid config for [automation]: [service] is an invalid option for [automation]

Instead, Taras’s solution works

Thanks Taras, your suggestion worked

1 Like

Oh, i see, accidentally pasted on the wrong line :smiley: