[RESOLVED] Help with intermittent failure of script

Edit 2: Sorted, it was incompatible audio files, nothing to do with the script.


I’ve got a script that runs when someone presses a big red “do not press” button in the bathroom. It changes the volume of a Sonos speaker, plays a random song from a local folder and changes the lighting in the room.

It works perfectly about 90% of the time, but the rest of the time the lights change but the song doesn’t play, which really ruins the fun!

I don’t see any errors in any logs, but I’m not sure if I’m looking in the right place. I’ve looked in the logbook and in Settings > System > Log, and checked the full log.

The script is written in YAML but I’ve checked the traces in the UI but there are no errors/failures on runs where no sound plays, each step passes. I did notice the “related logbook entries” tab shows different things during different runs, even successful ones, and it actually shows different services as calling the changes rather than the service defined in the script. Eg the first step is to change the volume with media_player.volume_set, and then later the lights are changed with media_player.volume_set (according to the logbook tab, at least).

My random song picker is kind of complicated, I cobbled it together from different forum posts. It’s a sensor that reads a folder, and then a template to pick a random item from the sensor and I had to use a template to edit the path of the files to get it to play properly. I’d be suspicious this was the problem but again, it works most of the time.

I’d really like this to work every single time, can anyone help me figure out where the problem is?

Here’s my script, but I’m pretty sure it’s ok.

big_red_button:
  alias: Big Red Button
  sequence:

  # Play random song from Big Red Button Folder (had to edit path with replace variable to get it to work)
  - service: media_player.play_media
    data:
      media_content_type: music
      media_content_id: >-
        {{ state_attr('sensor.big_red_button',
        'file_list')|random|replace('/media/Sounds/','/media/local/Sounds/') }}
    target:
      entity_id: media_player.bathroom_sonos
      
  # Turn overhead light off
  - service: light.turn_off
    target:
      entity_id: light.bathroom

  # Turn on disco lights
  - service: light.turn_on
    target:
      entity_id: light.bathroom_1
  # Wait 1 second so lights are staggered
  - delay: '00:00:01'
  # Turn on second disco lights
  - service: light.turn_on
    target:
      entity_id: light.bathroom_2

Edit: removed volume change step to simplify things, didn’t help.

I don’t have any experience with Sonos, but I have had occasional issues similar to what you have described with other media players if I don’t have a small delay between volume change and play service calls.

Interesting! I’ll remove that step and see if it helps. I can always set the volume separately if it turns out to be the problem.

Sadly that didn’t help. Ah well!

Can you post a trace from a “failed” execution?

I was about to do that when I noticed the selected song was a .m4a file, and out of interest I tried a few more. Every time the song fails, it’s a .m4a, but .mp3 files play no problem. I think that might be the answer!

I’m going to remove all the .m4a files and convert them, see what happens…

1 Like

Yep, that was it, no more silent button presses!

More specifically, these are tracks I’ve edited on my computer to cut out the first bit and start at a “good” bit. There are some unedited .m4a tracks working fine, so whatever I did has rendered them unplayable (they play fine on my computer).