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.