SONOS doorbell sound trough local mp3 file. Script + automation done but still not working

Hi guys i need some help with a script automation combination for Home Assistant. I’m trying to play a doorbell sound i have added to /WWW/ as a mp3.

My automation:

  - alias: 'Zigbee deurbel'
    initial_state: 'on'
    trigger:
      platform: event
      event_type: deconz_event
      event_data:
        id: xiaomi_smart_switch_1
        event: 1002
    action:
      - service: script.sonos_doorbell
        data:
          sonos_entity: media_player.sonos_woonkamer
          volume: 0.5
          delay: '00:00:15'

The script i copied from SONOS TTS:
sonos_doorbell:

  alias: "Sonos doorbell 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 }}"
   - service: media_player.play_media
     data:
       entity_id: "{{ sonos_entity }}"
       media_content_id: https://notgoingtotellyou.duckdns.org:8123/local/bell-effect.mp3
       media_content_type: music
   - delay: "{{ delay }}"
   - service: sonos.restore
     data_template:
       entity_id: "{{ sonos_entity }}"

The current playing track stops but then nothing happens. When i check the HA logs:

> Error while executing automation automation.zigbee_deurbel. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data['entity_id']
> Error executing script script.sonos_doorbell. Invalid data for call_service at pos 4: not a valid value for dictionary value @ data['entity_id']

I don’t know how to fix the error’s so maby someone can help? I thinks its my code but im not sure. When i go to https://notgoingtotellyou.duckdns.org:8123/local/bell-effect.mp3 I do hear the sound of the mp3 and when I test it without script etc the Sonos device plays the tune:

 - service: media_player.play_media
   data:
     entity_id: media_player.sonos_woonkamer
     media_content_id: https://notgoingtotellyou.duckdns.org:8123/local/bell-effect.mp3 
     media_content_type: music

In the fourth step of your script (with the service call to media_player.play_media), you need to change data to data_template.

1 Like

Woow that was the solution… Thanks alot, sometimes a second pair of eyes helps.

1 Like

Hi all, I’m trying to set up the same but when I enter:
https://xxxxxx.duckdns.org:8123/local/doorbell.mp3 I don’t hear anything. What am I missing?

Can you copy your config?

Hi VGE,

solved it, when I deleted the 8123 port it worked… strange but it works!

1 Like

In what local directory are you punting your mp3 files to play? I made a folder inside the config folder to keep the audio files organized but I can’t get the system to play anything to the Sonos speakers. I’m trying to limit my configuration to local polling as much as possible, and no access from the outside. If I’m not home I access homeassistant through my VPN firewall, so I don’t use duck DNS or SSL on my setup.

Any help is appreciated, thanks you.

1 Like

You’ll need to create a folder called “www” in the folder where your configuration.yaml lives. And put your mp3 in there. Then you can use this url to link in your script:

https://xxxxxx.duckdns.org:8123/local/doorbell.mp3

1 Like

Thank you,
I finally got that working.
Regards,

DP.

I made 2 separate scripts using your example. First to give the MP3 as a variable so I can use the same script for multiple mp3 files (doorbell, burglar alarm and whatever comes to mind). And a second to play the MP3 in all rooms.

sonos_mp3_one_room:
  alias: Sonos - MP3 - One Room
  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 }}'
  - service: media_player.play_media
    data_template:
      entity_id: '{{ sonos_entity }}'
      media_content_id: '{{ sonos_source }}'
      media_content_type: music
  - delay: '{{ delay }}'
  - service: sonos.restore
    data_template:
      entity_id: '{{ sonos_entity }}'

sonos_mp3_all_rooms:
  alias: Sonos - MP3 - All Rooms
  sequence:
  - data_template:
      entity_id:
      - media_player.kitchen
      - media_player.living_room
    service: sonos.snapshot
  - data_template:
      entity_id:
      - media_player.kitchen
      - media_player.living_room
    service: sonos.unjoin
  - data:
      master: media_player.living_room
    entity_id: all
    service: sonos.join
  - data_template:
      entity_id:
      - media_player.kitchen
      - media_player.living_room
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: media_player.living_room
      media_content_id: '{{ sonos_source }}'
      media_content_type: music
    service: media_player.play_media
  - delay: '{{ delay }}'
  - data_template:
      entity_id:
      - media_player.kitchen
      - media_player.living_room
    service: sonos.restore

These can be used with the following automation actions:

  action:
  - data:
      delay: 00:00:03
      sonos_source: https://xxxxxxxxxxxxxxxx.duckdns.org:8123/local/doorbell.mp3
      volume: 0.3
    service: script.sonos_mp3_all_rooms

or

  action:
  - data:
      sonos_entity: media_player.livingroom
      delay: 00:00:03
      sonos_source: https://xxxxxxxxxxxx.duckdns.org:8123/local/alarm.mp3
      volume: 0.9
    service: script.sonos_mp3_one_room
2 Likes

Noto, do you have a multi-room (not all) script?

This is AWESOME
thanks for creating and sharing this!

i had a quick question please…

what is this “master” section for?

data:
      master: media_player.living_room
    entity_id: all
    service: sonos.join

and why is living room (or whatever the master one is) called out specfically on its own again here?

  - data_template:
      entity_id: media_player.living_room
      media_content_id: '{{ sonos_source }}'
      media_content_type: music
    service: media_player.play_media

finally… rather then call out indiviaully entities, could you have a group?
then every time you add a new Sonos speaker, you can add it to the group and it will flow into this?

You set it in the script so you’re sure that in the following steps you’re talking to the master and not to a slave spreaker. That’s also the reason they are first unjoined and then joined again.

media_player.living_room is a group containing 2 speakers. They are merged by Sonos because they are in the same room.

1 Like

ahhh thnaks
i dont have any linked speakers, only 1 sonos per room
so i assume it doesnt matter which one i pick to have there in the code?

No that doesn’t matter which you set as a master, as long as the rest of the script you send commands to that speaker

1 Like

thanks again, this is fantastic

Hey
this seems to have stopped working for me recently and im unsure why?
i checked all my entites and they are all the same

i got an error about the delay, so i wrapped that in single quotes ’ however it still doesnt seem to work

i think i also saw some errors about volume but they are not showing up now

automation:
- id: play_doorbell_sound_all_sonos
  alias: Play Doorbell Sound on All Sonos Devices
  trigger:
  - entity_id: binary_sensor.front_door_ding
    from: 'off'
    platform: state
    to: 'on'
  action:
  - data:
      delay: '00:00:01'
      sonos_source: http://homeassistantssd.local:8123/local/audio/doorbell.mp3
      volume: 0.4
    service: script.sonos_mp3_all_rooms
script:
sonos_mp3_all_rooms:
  alias: Sonos - MP3 - All Rooms
  sequence:
  - data_template:
      entity_id:
      - media_player.bedroom
      - media_player.ensuite
      - media_player.living_room
      - media_player.spectrum_studio
    service: sonos.snapshot
  - data_template:
      entity_id:
      - media_player.bedroom
      - media_player.ensuite
      - media_player.living_room
      - media_player.spectrum_studio
    service: sonos.unjoin
  - data:
      master: media_player.living_room
    entity_id: all
    service: sonos.join
  - data_template:
      entity_id:
      - media_player.bedroom
      - media_player.ensuite
      - media_player.living_room
      - media_player.spectrum_studio
      volume_level: '{{ volume }}'
    service: media_player.volume_set
  - data_template:
      entity_id: media_player.living_room
      media_content_id: '{{ sonos_source }}'
      media_content_type: music
    service: media_player.play_media
  - delay: '{{ delay }}'
  - data_template:
      entity_id:
      - media_player.bedroom
      - media_player.ensuite
      - media_player.living_room
      - media_player.spectrum_studio
    service: sonos.restore

Could anyone hook me up with the mp3 you are using for the ring?

HomeBee, my use case was slightly different, but my dumbed down solution might help you, if you’re still looking.

I’m running both S1 and S2 Sonos systems in the house so considered adjusting this to serve the file to both an S1 group and an S2 group, but the S1 and S2 grouping speeds seem a bit different and variable depending on what combination of speakers are grouped at the time, so the sync wasn’t ever going to be perfect with the mix of the two systems.

Since that’s the case, I decided not to worry about the sound being sync’ed at all.

My script dumbed down the original and removes the joining of different rooms and just defines the speakers (a mix of S1 and S2) that I want the sound to play on as a comma separated list in the entity_id: each time it is referenced. e.g.

entity_id: media_player.kitchen_sonos_move, media_player.ms_study, media_player.loft

It works well and the sync is only a bit out. The main aim was to hear the door bell in key rooms throughout the house, so job done.

I haven’t had the inclination to test if this might be quicker than joining all the speakers into their S1 and S2 groups and then playing the sound through the two groups, but I wonder if this way each speaker starts playing the chime when it’s ready, rather than waiting for the slowest speaker to group up before playing. Dunno.

Noto

I am struggling. I used the scripts you provided but I cannot hear anything. Perhaps I set the volume too low.

This is your automation script.

This is my automation script.

  action:
  - data:
      delay: 00:00:03
      sonos_entity: media_player.sonos_1
      sonos_source: https://192.168.xxx.xxx:8123/local/zapsplat_emergency_alarm_2.mp3
      volume: 0.1
    service: script.sonos_mp3_one_room
  mode: single

This is the location of the MP3 file on the RPi on which I run HA.

]

This is the location of the MP3 file in Media Browser.

Any and all pointers gratefully accepted.