Sonos autoplay at start?


Is it possible to programm something in home assistant to start a sonos speaker (for example with a favorite playlist or radio station) when the sonos speaker gets connected to the electricity?


No idea? :frowning:

Yes that should be possible. Setup a sensor that detects the state of the sonos. If the state has a value, then (using automation) run a Shell script that triggers a favorite or playlist using the Node based HTTP sonos api.

Check out the stuff I’ve got in My git repo, you’ll find some code in the automation, script and shell command folders. I’ve got a Sonos based template sensor too.

Oh great!
Thanks for your examples! I will try it next week when i’m back from buisness Travel.

I try it this way:

I create a sensor which check the State of the player.

  - platform: template
    value_template: '{{ states.media_player.bad.state }}'

Then I create a trigger when the state change from ‘empty’ to ‘paused’.

The problem is, that NO new media_player appears when I plug in the Sonos Sub.

Is there a way to check every 10 sec or something for a new media_player device- so that I can check the state?

Thanks! And sorry for my english:)


can you explain me how you check the state of your Sonos sub?
In your Github sonos.yaml, you have two sensors- one for the kitchen and one for the sonos controller? what is the sonos controller- what does the ‘sonos_controller_online_shell’ do?

Do you scan the network with for the kitchen sub with this sensor?

- platform: command_line      
      scan_interval: 5
      command: /usr/bin/curl --silent GET | python -c 'import sys, json; print json.load(sys.stdin)["playbackState"]'
      name: 'sonos_kitchen_state_shell'

The IP is the IP of your sub in the kitchen?

Thank you

The online Shell is supposed to check the state of the nodejs, but I don’t think it’s working properly :frowning: is the ip of my raspberry pi where I’m running the Nodejs Sonos library. That library, on that link, returns some Json code that I’m parsing. But you might as well use the built in functionality to check the state. Checking state is done using the template sensor.

My solution:

  - platform: template
        value_template: '{{ states.media_player.bad.state }}'

    - alias: 'Auto Start Bad'
      initial_state: 'on'
      hide_entity: True
        platform: state
        entity_id: sensor.sonos_status_bad
        from: 'off'
        to: 'paused'
        service: media_player.play_media
        entity_id: media_player.bad
          media_content_id: x-rincon-mp3radio://
          media_content_type: music