Playing a mp3 file on sonos with automation throwing an error

So I was following the guide here http://www.vmwareinfo.com/2017/11/building-digital-cuckoo-clock-with-home.html but since I already have an actual cuckoo in the house I decided to do the grandfather clock instead.

Here is my automation for it

###################################
##  Grandfather Clock simulation.
## Plays Westminster Chimes on the hour and chimes 1 time on the half
## Script from CCOSTAN cuckoo clock but modified for the grandfather
###################################

- alias: Grandfather Clock
  trigger:
    - platform: time
      minutes: 45
      seconds: 00
    - platform: time
      minutes: 30
      seconds: 00

  condition:
   - condition: time
     after: '00:08:00'
     before: '21:30:00'


  action:
    - service: media_player.volume_set
      entity_id:
        - media_player.living_room
      data:
        volume_level: 0.50

    - service: media_player.play_media
      data_template:
        entity_id:
          - media_player.living_room
        media_content_id: >
          {% if now().strftime("%M")|int == 30 %}
          /config/sound/GrandFatherChime01.mp3
          {% else %}
          /config/sound/westminster.mp3
          {% endif %}
        media_content_type: audio/mp3

Yes I know I have it set for 45 after because I’ve been testing it out :slight_smile:
But whenever it runs it throws

2018-01-01 18:45:01 ERROR (SyncWorker_7) [soco.services] UPnP Error 714 received: Illegal MIME-Type from 192.168.7.125
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/soco/services.py", line 404, in send_command
    self.handle_upnp_error(response.text)
  File "/usr/lib/python3.6/site-packages/soco/services.py", line 465, in handle_upnp_error
    error_xml=xml_error
soco.exceptions.SoCoUPnPException: UPnP Error 714 received: Illegal MIME-Type from 192.168.7.125
2018-01-01 18:45:01 ERROR (SyncWorker_7) [homeassistant.components.media_player.sonos] Error on play_media with UPnP Error 714 received: Illegal MIME-Type from 192.168.7.125

I"ve searched around and all I can find related to it is just SPOTIFY stuff. There’s gotta be something stupid that I’m missing in my config to have sonos play a file. Or maybe I screwed up by putting the files locally on the box? I just moved my config from a VM to a PI the other day. Thanks for your help.

BTW finally put in the right magical search terms in Google and figured this out. I had to put the files for the chimes in a www folder and then used the following code to make the calls to the files

action:
    - service: media_player.volume_set
      entity_id:
        - media_player.living_room
      data:
        volume_level: 0.35

    - service: media_player.play_media
      data_template:
        entity_id:
          - media_player.living_room
        media_content_id: >
          {% if now().strftime("%M")|int == 30 %}
          http://192.168.7.2:8123/local/Toll-2Quarter.mp3
          {% else %}
          http://192.168.7.2:8123/local/{{now().strftime("%I")}}-hour.mp3
          {% endif %}
        media_content_type: music

Just basically telling sonos to call the HASS via http and play the file. Win

2 Likes

How did you determine that ip address? it doesn’t seem to be the same one that was in your error message. Thank you!

I used the IP address of the hass for this since I stuck the files on it directly.

1 Like

Thank you. Because I’m using https I had to use the domain (duckdns) that I use to access home assistant.

My actual issue was that my file was at www/local/bell.mp3 , not www/bell.mp3 . Once I moved the file there, all was gravy. :slight_smile:

1 Like

I’m happy to hear that you got it all working.

I created a little Python script that will generate the audio files based on a single mp3 file.

https://blog.maartenpaauw.com/2018/08/19/pendulum-clock-audio-generator.html

Hi, could you please help me identifying problem with the approach to play an mp3 file on Sonos through automation? I am running Hassio 0.75.3 with below code which is bundled with alarm clock that turns on hue bulb at alarm time. Alarm is working with bulb but not at all playing mp3 file below:

- id: office_morning_alarm
  alias: 'Hue light on gradually with alarm'
  hide_entity: False
  initial_state: 'on'
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time_long.state }}'
  condition:
    condition: state
    entity_id: input_boolean.alarm_clock_status
    state: 'on'
  action:
  - service: script.wake_up
  - service: media_player.sonos_snapshot
    data:
      entity_id: media_player.family_room
  - service: media_player.volume_set
    data:
      entity_id: media_player.family_room
      volume_level: 0.5
  - service: media_player.play_media
    data:
      entity_id: media_player.family_room
      media_content_id: http://192.168.0.214:8123/local/doorbell.mp3
      media_content_type: music
  - service: media_player.sonos_restore
    data:
      entity_id: media_player.family_room

wake_up script simply turns on hue bulb with preset bringhtness. Mp3 file doorbell is placed under www folder in config directory. I am also able to access & play media file via web browser via the address http://192.168.0.214:8123/local/doorbell.mp3. There is no error associated with Sonos in log file. I also have TTS script for Sonos that works very well with text messages.

try using www instead of local in the media_content_id

I tried www instead of local doesn’t work, even accessing it from web browser gives error 404: Not Found .

I tried reformatting audio file to 44.1KHz sampling/16bit it started working (using local) with both mp3 and wav format but still there is something that doesn’t allow 4 second audio file to run fully and finishes in less than a second. I had similar experience with Sonos while playing text messages with google TTS, it was playing those messages partly and chopping off last portion of it.

I use the following to play mp3s on my chromecast…

- alias: 'Play Heavy Rain Lullaby'
  initial_state: 'on'
  trigger:
    - platform: state
      entity_id: input_select.lullaby
      to: "Heavy Rain MP3"
  action:
    - service: media_player.turn_on
      data:
        entity_id: media_player.living_room_tv
    - delay: 0:0:02
    - service: media_player.play_media
      data:
        entity_id: media_player.living_room_tv
        media_content_id: http://192.168.178.20:8123/local/Media/effects/HeavyRainfallThunder60Min.mp3
        media_content_type: audio/mp3

The only difference to yours is I use audio/mp3 instead of music for the content id.

Thanks for your inputs, it is working with audio/mp3 as well as with music. I also figured out the reason for output getting chopped, it was due to sonos.restore service that not letting file to play completely. I added 5 seconds delay just before -service: media_player.sonos_restore in my code and it played well but with the condition of mp3 or wav audio file with 44.1KHz sampling rate/16bit conversion.

1 Like

Thanks for bringing this problem of sonos restore up. I spent half a day in order to figure out why my script (which worked fine in v0.60) does no longer work in v0.78.

I have put an additional wait command which prevents the restore to happen before longer audio files have finished.

  - service: media_player.volume_set
    data_template:
      entity_id: "{{ sonos_entity }}"
      volume_level: "0.2"
  - delay: "00:0:10"
  - wait_template: "{{ not is_state(sonos_entity, 'playing') }}"
    timeout: '00:06:00'
  - service: media_player.sonos_restore
    data_template:
      entity_id: '{{ sonos_entity }}'