[re-opened] Sonos script works occasionally please help fix it

Tags: #<Tag:0x00007f466de45c68> #<Tag:0x00007f466de457b8>

Hi everybody,

I am trying to play an audio file whenever a certain switch is turned on, and another file when that switch is turned of. This worked fine for a while, but suddenly it only works occasionally (less than 50% of the time); sometimes the audio will play right away, sometimes it will play randomly after the switch has been toggled, and often it will not play at all. Does anybody know how to fix this and/or how I can determine what is going on?

automation

automation:
  - id: "snd_tv_aus"
    alias: "[Sound] TV Aus"
    trigger:
      platform: state
      entity_id: switch.schlafzimmer_tv
      to: "off"
    action:
      - service: script.sonos_play
        data:
          sonos_entity: media_player.schlafzimmer_son
          volume: 0.32
          mytype: 'audio/mp4'
          myurl: 'http://ip/local/audio/andreas/pa_ausraster_schnauze.mp3'
          delay: '00:00:02'

  - id: "snd_tv_an"
    alias: "[Sound] TV An"
    trigger:
      platform: state
      entity_id: switch.schlafzimmer_tv
      to: "on"
    action:
      - service: script.sonos_play
        data:
          sonos_entity: media_player.schlafzimmer_son
          volume: 0.32
          mytype: 'audio/mp4'
          myurl: 'http://ip/local/audio/misc/20thcentury_short.mp3'
          delay: '00:00:10'

The data is passed on to my script below; from what I understand, delay is the time until the next action is preformed… so I set 2 seconds for the first automation and 10 for the second, as these are the lengths of the files. Before I had this delay, they would not play at all (probably because the action was performed but without the delay, it was ended again right away…). Is this correct? Or what is the delay? It doesn’t seem to be the time before playing the file, as -in those cases the script works correctly- both files will play right away, not after two or ten seconds.

script

  sonos_play:
    alias: "Sonos Notify script"
    sequence:
     - service: sonos.snapshot
       data_template:
         entity_id: "{{ sonos_entity }}"
     - service: sonos.unjoin
       data_template:
         entity_id: "{{ sonos_entity }}"
     - service: media_player.volume_set
       data_template:
         entity_id: "{{ sonos_entity }}"
         volume_level: "{{ volume }}"
     - delay: 00:00:02
     - service: media_player.play_media
       data_template:
         entity_id: "{{ sonos_entity }}"
         media_content_type: "{{ mytype }}"
         media_content_id: "{{ myurl }}"
     - delay: "{{ delay }}"
     - service: sonos.restore
       data_template:
         entity_id: "{{ sonos_entity }}"

This is based on the sonos example tts script.

What it should do:

  • save current sonos settings
  • unjoin
  • set volume
  • play media (data passed to script from action below)
  • delay (so that the audio will finish playing before restoring)
  • restore previously saved sonos settings

I usually toggle the tv (which is the trigger switch for this) via Alexa; at t he beginning, Home Assistant would recognize this and play the audio file. Last night it did not play either of the files (no audio when turning on, nor when turning off). However, when I later activated my “good night” routine, which turns off the tv in case it is still on -which it wasn’t as I had turned it off before so Home Assistant shouldn’t think it was still on- it then played the file.

This doesn’t make sense to me. The tv was turned off (by Alexa), yet the audio file hadn’t been played until it had been *turned off again by that “good night” routine.

I am using the same script for my morning alarm to play an audio file. That works fine each time (except it doesn’t play the file for it’s entire duration no matter what I do). So I assume that this has something to do with the TV…

The TV is connected to the Sonos, so perhaps the script triggers slightly before my Sonos detects the TV input and therefore switches to TV instead before actually playing the file (because it detects the TV input after the script has been triggered and switches to the TV as it was the last thing it detected)? However, if this was the case, why won’t it play the turn off part, either? That script won’t start until the TV has been turned off, so the TV should not be able to interfere with that action.

I realized when I manually turn the switch from my smartphone, the script will be more likely (but not 100% reliable) to work. If I turn the tv on and the audio does play, then turn the tv off, the audio will play as well. Which is really strange. IF there was something wrong with my script, it should not play at all - but not just work some of the time.

Thank you for your help =)

Use

media_content_type: music
1 Like

Thank you. It works at the moment (cannot say for sure, as my previous solution sometimes works fine as well). Tested 5 times each, so 10 triggers went through as expected :slight_smile:

Unfortunately, this semi-broke again.

This morning, my alarm turned on the lights but did not play any sound (sound triggered through the script above).

I then started the tv to test the script, because toggling the tv switch will trigger the script as well. When I turned on the tv, nothing happened. Then I turned the tv off and it played the sound. Then I turned the tv back on and it also played a sound. These three triggers happened within one minute.

So once it didn’t work, a few seconds it worked (for another action), another few seconds later it did work for the first action that did not work half a minute ago. What could cause this?

This is more or less “just for fun” (I wake up before the alarm 99.9% of the time and the tv sound is just cool to have), but I’d still like to have a working automation.

Due to the fact that this works some times and then won’t work at others, I cannot look at the log to determine what was wrong (if anything) right away. Is there a good way to debug this? I thought of adding a telegram notify to the script to check whether or not it triggered correctly; however, in case of the alarm I am pretty positive that it did trigger correctly, otherwise the lights wouldn’t have turned on either (still the sound was missing).