Automatically start Sonos playback at 6:25am

So, I’m very new to HA, and I’d like to try and get my first piece of automation up and running.

I’d like to have one of my Sonos units (bedroom) play a specific radio station at a specific time, and possibly start with the volume at 0 and fade up to a specific level. Now, this is where I get a bit lost. I’ve looked at some examples, and I’m finding the structure a little confusing, as well as knowing if terms being used (say, script.turn_on) are something unique to the example I’m looking at, or specific to the entity, or just present in HA and can be called on as needed.

I’ve got an include in my main config to pull in a file containing my automation, which then has an include to pull in a file containing this particular piece of automation. Can I put everything in that file, or do I need to call a script?

I’m happy to try and work this out, but I guess I’d be after a reference guide to what commands I can use.

Cheers in advance all!

If you look at services under dev tools (looks like a computer tower icon). You can look at all of your domains and the services that are available to those domains.

For example in your question, script would be the domain and turn_on would be the service.

Hope that helps get you started. If you post your current automation I’m sure someone can help you fine tune it.

Hi Mike,

Thanks for the reply!

So, at the moment I’ve got:

configuration.yaml with !include for automation.yaml, with an !include for weekdaygetup.yaml, which currently looks like this:

alias: Weekday morning routine
trigger:

  • platform: time
    after: ‘06:25:00’
    weekday:
    • mon
    • tue
    • wed
    • thu
    • fri
      condition: or
      conditions:
    • condition: state
      enitity.id: ‘sn7plus’
      state: ‘home’
    • condition: state
      entity_id: ‘cn7’
      state: ‘home’
      action:

So, action is where I’m currently stuck.

For future reference, It’s helpful to paste your code using the preformatted text option. This will retain indents and spacing which is important in yaml.

Paste your code, highlight it and then click the “< / >” button at the top of the post editor.

That said. You could add an action to turn on a script.

Below is an example of a script pulled from Sonos: automation, scenes and specific playlists

script:
  sonos_play_radio
    sequence:
    - alias: "Radio play"
      service: media_player.play_media
      data:
        media_content_id: x-rincon-mp3radio://fm03-icecast.mtg-r.net/fm03_mp3
        media_content_type: music

Your config above would look something like below.

- alias: Weekday morning routine
  trigger:
    platform: time
    after: '06:25:00'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  condition: or
  conditions:
    - condition: state
      entity_id: sn7plus
      state: 'home'
    - condition: state
      entity_id: cn7
      state: 'home'
  action:
      service: homeassistant.turn_on
      entity_id: script.sonos_play_radio

Ah, I’d wondered how to paste ‘as code’ so the formatting was maintained - that’s what not enough sleep does to your brain!

Thanks for the advice Mike! I’ll pick that script apart later so I understand what’s it’s doing when I implement it. I work in IT, but I’m not a coder by nature, so this stuff is coming a little harder than, say, the networking side of this home automation project!

No problem. I’m in the exact same boat as you. Server admin, not a programmer and I’ve only been using HA for about 2-3 months. The community here is great though, so that helps a lot.

1 Like

@Mike_D where does one find the media_content_id: like what you are using the “x-rincon-mp3radio://”? I’ve seen these all over in example configs but have no idea where it’s coming from. Thanks!

Unfortunately I don’t have a Sonos. But I’m going to guess and say in the attributes of your Sonos media_player in the states-dev tool section. Probably need to be playing that station at the time you check.

You can just set it up in the sonos app as an alarm… less fun but much easier!