Sonos_snapshot/restore not working

Hi all,

I have a script that interrupts sonos to ring a bell, i have added a sonos snapshot and restore but it isnt working. I have added a shot delay after taking the snapshot just in case the snapshot was taking a few seconds but that didnt work either.

anyone have any ideas or have similar issues?

  - alias: 'Call Bell'
trigger:
  - platform: state
    entity_id: sensor.small_button
    to: 'single'
action:
  - service: media_player.sonos_snapshot
    data_template:
      entity_id:
        - media_player.kitchen
        - media_player.upstairs
  - delay:
      seconds: 3
  - service: media_player.volume_set
    data_template:
      entity_id: 
        - media_player.kitchen
        - media_player.upstairs
      volume_level: 0.6
  - service: media_player.select_source
    data_template:
      entity_id: 
        - media_player.kitchen
        - media_player.upstairs
      source: Boxing Ring Bell 1
  - service: media_player.turn_on
    entity_id: 
      - media_player.kitchen
      - media_player.upstairs
  - delay:
      seconds: 10
  - service: media_player.sonos_restore
    data_template:
      entity_id:
        - media_player.kitchen
        - media_player.upstairs
  - service: mqtt.publish
    data:
      topic: zigbee2mqtt/0x00158d00016bf669
      payload:  '{"click":"waiting"}'

Does anyone use this functionality and could share a code snippt?

I’m seeing the same thing but in my appdaemon script. Not sure what I’m doing wrong

1 Like

I had the same problem thru Node-red until I found out that if you start the music from anything else than the sonos app the snapshot function isn’t working.
So make sure you don’t start the music from the Spotify app, that was my problem at least.

Hope it helps!

Does that include the embedded Spotify in the app? Because I still have the problem

Did you find a solutions to this?

I too am having problems with restoring after a TTS

I found that snapshots won’t work with Spotify or some other music inputs.

Hi,

I have tested a number of variations:
For me restore works with
TuneIn
Spotify
Tidal
It does not work with Spotify Connect (I assume this is due to the fact that the connection to Spotify is cut by a TTS.
I have identified another problem. Restore does not work if line out level (settings/room settings/[room]/advanced sound/line out level) is set to fixed. I can see that everything is restored but play does not resume. I have tested changing only this parameter by setting line out level to variable and then restore works fine. As I want the setting to be “fixed” I would like to know if anyone knows why this happens and if there is a solution.

1 Like

I’m currently also facing some issues with the sonos.restore option.
I’m using node-red flow to take a sonos-snapshot of the kitchen speaker, then play a file via TTS and do some more stuff and next use the sonos.restore to resume playback.

BUT here is the problem: when there is no music playing on the kitchen speaker and I trigger the TTS then the sonos.restore always kicks in and resumes playing the previous audio even when there was no audio playing at the moment of the sonos.snapshot.
You get what I mean?

If there was music (spotify or tunein radio) playing at the moment of the sonos.snapshot this is resumed correctly with the sonos.restore.

My only issue is when there is NO music/audio playing the sonos.restore will always resume playback.

Any tips?

Here is my node-red flow:


[{"id":"bfc686bb.a45ca8","type":"trigger-state","z":"ebbc7cb3.0c2d5","name":"snoepla","server":"779066dc.805a78","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"binary_sensor.rfxcom_doorwindowsensor_5_snoepla","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"woiwqxlz357","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":100,"y":740,"wires":[["9fe59670.3168d8","3fcbe9fa.b624e6"],[]]},{"id":"9fe59670.3168d8","type":"time-range-switch","z":"ebbc7cb3.0c2d5","name":"6am and 11pm","lat":"52.00472","lon":"5.92784","startTime":"06:00","endTime":"23:00","startOffset":0,"endOffset":0,"x":340,"y":740,"wires":[["a327d743.a530c8","1ee009cb.b1b676"],["a327d743.a530c8"]]},{"id":"3fcbe9fa.b624e6","type":"debug","z":"ebbc7cb3.0c2d5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":330,"y":840,"wires":[]},{"id":"a327d743.a530c8","type":"function","z":"ebbc7cb3.0c2d5","name":"create payload","func":"msg.method = \"sendPhoto\";\nmsg.payload = {\n    photo: \"/config/www/snapshots/snapshot_cam5_keuken.jpg\",\n    caption: \"CAM4 - de snoepla is open!\"\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":740,"wires":[["b64bffe.ace1"]]},{"id":"1ee009cb.b1b676","type":"change","z":"ebbc7cb3.0c2d5","name":"prompt","rules":[{"t":"set","p":"tts1","pt":"flow","to":"ALARM! ALARM! Wie steelt er lekkers uit de la?","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":800,"wires":[["cc21a765.8010b8"]]},{"id":"b64bffe.ace1","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"Save snapshot cam5","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"camera","service":"snapshot","entityId":"camera.me_cam5_keuken","data":"{\"filename\":\"/config/www/snapshots/snapshot_cam5_keuken.jpg\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":true,"x":760,"y":740,"wires":[["452a4c54.ac3f94"]]},{"id":"cc21a765.8010b8","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"unjoin","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"sonos","service":"unjoin","entityId":"media_player.keuken","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":650,"y":800,"wires":[["ffbdd0a5.e7b5"]]},{"id":"452a4c54.ac3f94","type":"delay","z":"ebbc7cb3.0c2d5","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1040,"y":740,"wires":[["7e5d18da.7ff7a8"]]},{"id":"ffbdd0a5.e7b5","type":"api-current-state","z":"ebbc7cb3.0c2d5","name":"sonos state","server":"779066dc.805a78","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"media_player.keuken","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":800,"wires":[["130678db.af1027"]]},{"id":"7e5d18da.7ff7a8","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"notify simon","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_isims","entityId":"","data":"{\"title\":\"Beweging gedetecteerd!\",\"message\":\"De snoepla is geopend.\",\"data\":{\"push\":{\"badge\":1,\"category\":\"respond_doorbell\"},\"attachment\":{\"url\":\"http://xxx.xxx.xxx.xxx/picture/5/current/?_username=xxxx&_signature=xxxxxxxxxa\",\"content-type\":\"jpeg\",\"hide-thumbnail\":false}}}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1210,"y":740,"wires":[[]]},{"id":"130678db.af1027","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"pause","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"media_player","service":"media_pause","entityId":"media_player.keuken","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":930,"y":800,"wires":[["c2ae5e48.25411"]]},{"id":"c2ae5e48.25411","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"snapshot","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"sonos","service":"snapshot","entityId":"media_player.keuken","data":"{\"with_group\":\"true\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1060,"y":800,"wires":[["f36ec39d.0738c"]]},{"id":"f36ec39d.0738c","type":"delay","z":"ebbc7cb3.0c2d5","name":"","pauseType":"delay","timeout":"100","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":860,"wires":[["447b78ec.338058"]]},{"id":"447b78ec.338058","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"volume 50%","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"media_player","service":"volume_set","entityId":"media_player.keuken","data":"{\"volume_level\":0.5}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":830,"y":860,"wires":[["ca308724.f192c8"]]},{"id":"ca308724.f192c8","type":"random","z":"ebbc7cb3.0c2d5","name":"random","low":"1","high":"4","inte":"true","property":"random_number","x":980,"y":860,"wires":[["ef9b8544.ec7098"]]},{"id":"ef9b8544.ec7098","type":"change","z":"ebbc7cb3.0c2d5","name":"change","rules":[{"t":"change","p":"random_number","pt":"msg","from":"1","fromt":"num","to":"Alarm! Alarm! wie pakt er lekkers uit de la?","tot":"str"},{"t":"change","p":"random_number","pt":"msg","from":"2","fromt":"num","to":"Hej boefje! Wat doe jij daar? ","tot":"str"},{"t":"change","p":"random_number","pt":"msg","from":"3","fromt":"num","to":"Waarschuwing! De snoepla is geopent!","tot":"str"},{"t":"change","p":"random_number","pt":"msg","from":"4","fromt":"num","to":"Stop! Lekkers pakken uit de la, weet je het zeker?","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":860,"wires":[["2946e41a.b2a23c"]]},{"id":"2946e41a.b2a23c","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"play prompt","server":"779066dc.805a78","version":1,"debugenabled":true,"service_domain":"tts","service":"google_say","entityId":"media_player.keuken","data":"{\"message\":\"{{random_number}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":670,"y":920,"wires":[["e80fb4f4.70b038"]]},{"id":"e80fb4f4.70b038","type":"delay","z":"ebbc7cb3.0c2d5","name":"","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":820,"y":920,"wires":[["e60f2bb0.caa5f8"]]},{"id":"e60f2bb0.caa5f8","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"restore","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"sonos","service":"restore","entityId":"media_player.keuken","data":"{\"with_group\":\"true\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":960,"y":920,"wires":[["9868672d.399638"]]},{"id":"9868672d.399638","type":"api-call-service","z":"ebbc7cb3.0c2d5","name":"unpause","server":"779066dc.805a78","version":1,"debugenabled":false,"service_domain":"media_player","service":"media_play","entityId":"media_player.keuken","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1100,"y":920,"wires":[[]]},{"id":"779066dc.805a78","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Playing around with sending txt to Sonos this morning. I had the same problem with the restore appearing to not work. I think it is actually the snapshot that is not having time to store info. I added a delay after the snapshot and then everything works even the restore.
So give the snapshot time to work with a delay just after the snapshot call like this.

  • service: sonos.snapshot
    data:
    entity_id: ‘{{ sonos_entity }}’
  • delay: 2

Or you can use the scripts delay argument with ‘{{ delay }}’ instead of 2

The delay has certainly made the sonos restore function work. This is my full script for an automation that…

  • If the security camera sees a vehicle enter into the drive way (unifi vehicle detection)
  • Actions are: - Check if music is playing. IF so, take a snapshot and set a helper to “on”.
  • Play the TTS - There is a vehicle in the drive way
  • Delay for 2 seconds added so it gives time for the TTS to play
  • Perform a resume if the helper from the snapshot check was turned on.
alias: "Notification - Car in driveway "
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.front_detections_vehicle
    to: "on"
condition: []
action:
  - if:
      - condition: state
        entity_id: media_player.living_room
        state: playing
    then:
      - service: sonos.snapshot
        data:
          entity_id: media_player.living_room
          with_group: false
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - service: input_boolean.turn_on
        data: {}
        target:
          entity_id: input_boolean.sonos_snapshot_and_resume
  - service: media_player.play_media
    target:
      entity_id: media_player.living_room
    data:
      media_content_id: >-
        media-source://tts/google_translate?message=There+is+a+vehicle+in+the+driveway
      media_content_type: provider
    metadata:
      title: There is a vehicle in the driveway
      thumbnail: https://brands.home-assistant.io/_/google_translate/logo.png
      media_class: app
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://tts
        - media_content_type: provider
          media_content_id: >-
            media-source://tts/google_translate?message=There+is+a+vehicle+in+the+driveway
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
    enabled: true
  - if:
      - condition: state
        entity_id: input_boolean.sonos_snapshot_and_resume
        state: "on"
    then:
      - service: sonos.restore
        data:
          entity_id: media_player.living_room
          with_group: false
      - service: input_boolean.turn_off
        data: {}
        target:
          entity_id: input_boolean.sonos_snapshot_and_resume
mode: single