Help creating SONOS automation (newbie)

I’m trying to create my first automation but I always get into error and I really want to just give up on this Home Assistant thing.

Here’s what I wrote:

alias: New automation
description: ''
trigger: []
condition:
  - condition: device
    device_id: xxxxxxx
    domain: device_tracker
    entity_id: device_tracker.iphone
    type: is_home
action:
  - service: media_player.play_media
    target:
    entity_id: media_player.sonos
  data:
    media_content_type: "music"
    media_content_id: "https://open.spotify.com/playlist/...
  - service: media_player.shuffle_set
  data:
    entity_id: media_player.sonos
    shuffle: true
mode: single

Thanks in advance to anyone willing to help out a newbie.

The automation has no trigger (so it’s effectively a non-functional automation) and it has a few syntax error where certain lines are not indented correctly.

Even if you fix the indentation errors, it won’t be functional until you provide a proper trigger. What do you want to happen in order for this automation to execute? Perhaps the condition you created is what you actually want as the trigger?

1 Like

Thanks for the response. I haven’t put anything in it because I wanted to later activate this automation linking it with the iOS Shortcuts app with an automation there so that when I snooze or stop an alarm on my iPhone my SONOS speaker plays a specific playlist.

Where do I find a guide on how to use the correct syntax in Home Assistant? I’m finding everything a bit overwhelming :frowning:

In that case, it should be a script, not an automation.

1 Like

You could avoid most of the errors with using the UI automation editor …

1 Like

Alright, thanks

I tried to but I couldn’t get past the selection of an entity when selecting media_player.play_media as a service to then tell what to play.

In the official documentation. Here are the docs for scripts:

You should familiarize yourself with it (and for Automations) even if you intend to compose scripts and automations using the UI-based editors (as suggested by m0wlheld).

When you use the UI-based editors, they allow you to switch from visual mode to YAML mode. It’s a good way to see the YAML that’s being generated when you use the UI-based form.

1 Like

to add one more thing…

If you wrote this manually you can’t use device “stuff” (triggers, conditions, actions) in anything that is written in yaml by hand.

I started with the UI and then added using YAML the things that I wasn’t able to do with the UI.

e.g.

data:
    media_content_type: "music"
    media_content_id: "https://open.spotify.com/playlist/...
  - service: media_player.shuffle_set
  data:
    entity_id: media_player.sonos
    shuffle: true

At least I wasn’t able to do it.

When saving what I add to the “call service media_player.media_play” via yaml it defaults back to the UI standard yaml, not saving the extra code… how do I add what media it should play?

No issue on my side, but even then you can always switch that specific section to YaML to check proper indentation.

Thanks for the screenshot, I selected the wrong service “.media_play” instead of “.play_media”!

I suggest reviewing the documentation for media_player. It explains how each one of its service calls works.

It’s easy to mix up those two service calls because they have nearly identical names. One is used to “un-pause” a paused player (that’s the one you mistakenly used) and the other is for selecting and playing media (that’s the one you wanted).

1 Like