Offer option to loop/repeat an audio file on the media_player.play_media service

Would love a native way to do this. For those that don’t want to mess with scripts if you install music assistant this generates new media players (duplicates of your current ones including nest speakers) and allows you to send the command media repeat all command which will loop the audio. Benifit is thistle doesn’t get caught up in loops or interrupted. You can even pause and play and it will playing on loop.

Your seven-step process worked for me too based on testing; thank you very much for the cheat sheet. Haven’t set up completely to use at night because it took me a while to get working. It wasn’t working at first, but then realized in logs wasnt triggering, so added a trigger to go with the ‘while’ time range in step 5.

It’s been some time, but would you mind sharing the code? I am pretty new to home assistant and your explanation was not enough for me to get this up and running.

Spent far too much time trying to get this right so thought I would document it here.

Here is what I did:

  1. created a automation which implements the while loop when a light switch is switch on:

The problem I was having was if I stopped the mp3 playing then I had to wait until the delay had run down before I could re-start the mp3.
So I created another automation which does the following steps:

  1. stops the audio
  2. Turn off the first automation
  3. Turn on the first automation

Code for first automation:

alias: play2
description: Play music when the light comes on
trigger:
  - platform: state
    entity_id:
      - light.picture_leds_switch_1
    to: null
    from: null
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.picture_leds_switch_1
            state: "on"
        sequence:
          - repeat:
              sequence:
                - service: media_player.play_media
                  target:
                    entity_id: media_player.office_speaker
                  data:
                    media_content_id: >-
                      media-source://media_source/local/04-Won't Fight Your
                      War.mp3
                    media_content_type: audio/mpeg
                  metadata:
                    title: 04-Won't Fight Your War.mp3
                    thumbnail: null
                    media_class: music
                    children_media_class: null
                    navigateIds:
                      - {}
                      - media_content_type: app
                        media_content_id: media-source://media_source
                - delay:
                    hours: 0
                    minutes: 5
                    seconds: 13
                    milliseconds: 0
              while:
                - condition: state
                  entity_id: light.picture_leds_switch_1
                  state: "on"
mode: single

Code for second automation:

alias: stopmp3
description: Stop music when the light goes off
trigger:
  - platform: state
    entity_id:
      - light.picture_leds_switch_1
    to: "off"
    from: "on"
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.picture_leds_switch_1
            state: "off"
        sequence:
          - service: media_player.media_stop
            metadata: {}
            data: {}
            target:
              device_id: 538c2bef9b21fb879fbd6948f5da6f80
          - service: automation.turn_off
            metadata: {}
            data:
              stop_actions: true
            target:
              entity_id: automation.play2
          - service: automation.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: automation.play2
mode: single

Hope this helps someone

You should be able to achieve your goal with a single automation. Set its mode to restart and configure its State Trigger to detect the light turning on and off.

alias: example
trigger:
  - platform: state
    entity_id: light.picture_leds_switch_1
    to:
      - 'on'
      - 'off'
    from:
      - 'off'
      - 'on'
condition: []
action:
  - if: "{{ trigger.to_state.state == 'on' }}"
    then:
      - repeat:
          sequence:
            - service: media_player.play_media
              target:
                entity_id: media_player.office_speaker
              data:
                media_content_id: >-
                  media-source://media_source/local/04-Won't Fight Your War.mp3
                media_content_type: audio/mpeg
              metadata:
                title: 04-Won't Fight Your War.mp3
                thumbnail: null
                media_class: music
                children_media_class: null
                navigateIds:
                  - {}
                  - media_content_type: app
                    media_content_id: media-source://media_source
            - delay:
                minutes: 5
                seconds: 13
          while:
            - condition: state
              entity_id: light.picture_leds_switch_1
              state: "on"
    else:
      - service: media_player.media_stop
        target:
          entity_id: media_player.office_speaker
mode: restart
1 Like

That worked, thanks I should have RTFM…