Media Player play random media localy with cover

Hi, I have a sensor that randomly extracts a single music file from a local folder.
I would like to associate a cover for each file and to do this I created two other sensors to have the file synchronized with the cover.
These are my sensors:

  - platform: folder
    folder: "/config/www/musica/studioghibli/"

  - platform: template    
    sensors:
      file_studioghibli:
        value_template: >
          {{ state_attr('sensor.studioghibli', 'file_list') |random  |regex_replace(find='config/www', replace='local',
ignorecase=False) }}

  - platform: template    
    sensors:
      cover_studioghibli:
        value_template: >
          {{ states('sensor.file_studioghibli') |regex_replace(find='mp3', replace='png', ignorecase=False) }}

This is the result:


I want to play on my google home these files and I tried to do it with this script, but it doesn’t work:

suona_musica_studioghibli:
  sequence:
    repeat:
      count: 27
      sequence:
      - service: media_player.turn_on
        target:
          device_id: 38b7837d52b074699c7d1c490d0757e8
      - service: media_player.play_media
        data_template:
          entity_id: media_player.salotto
          media_content_type: audio/mp3
          media_content_id: >
            '{{ states(sensor.file_studioghibli) }}'
          extra:
            thumb: >
              '{{ states(sensor.cover_studioghibli) }}'
            title: 'Studio Ghibli' #'{{((state_attr("media_player.salotto", "media_title")))}}'
      - delay:
          hours: 0
          minutes: 0
          seconds: 11
          milliseconds: 0
      - wait_template: '{{ is_state(''media_player.salotto'', ''idle'') }}'

Is there a correct way to make the script work?
thank you

define ‘doesn’t work’. what do your logs say? What device is media_player.salotto - you say google home, do you mean it is a cast device Google Cast - Home Assistant

sorry, media_player.salotto is a google home mini, while it doesn’t work it means that when I start the script nothing happens.
What appears to me in the errors is this:

I modified this script to have the cover for each song. This is the original script and it works fine:

suona_musica_studioghibli:
  sequence:
    repeat:
      count: 27
      sequence:
      - service: media_player.turn_on
        target:
          device_id: 38b7837d52b074699c7d1c490d0757e8
      - service: media_player.play_media
        data:
          entity_id: media_player.salotto
          media_content_type: audio/mp3
          media_content_id: '{{ state_attr(''sensor.studioghibli'', ''file_list'')
            |random  |regex_replace(find=''config/www'', replace=''local'', ignorecase=False)
            }}'
          extra:
            thumb: https://www.lacooltura.com/wp-content/uploads/2020/09/ghibli-6.jpg
            title: 'Studio Ghibli' #'{{((state_attr("media_player.salotto", "media_title")))}}'
      - delay:
          hours: 0
          minutes: 0
          seconds: 11
          milliseconds: 0

Please remove the share my project category from this post, as it’s asking for help, not showcasing a finished project. You can move it to the configuration category.

1 Like