Sonos script to announce text, help a noob please

I’d like to announce something on the sonos speakers if say my living room light is turned off.

I’ve been reading for the past two hours and it sounds like I need a script first that I can use in my automation.

I go to Configuration>Scripts>Add script>Edit as YAML.

I paste the script from here:
https://www.home-assistant.io/cookbook/sonos_say/
But I keep getting "Message malformed: extra keys not allowed @ data[‘script’]"

I understand that I need to change entity_id: “{{ sonos_entity }}” with my sonos i.e entity_id: media_player.living_room_3

But still I cannot get the script to save. What else am I missing here? What is volume, message, and delay. Do I need to declare that somewhere? Why is the script failing?

Thank you!

Paste everything except the first line (“script:”). You will also have to adjust the indentation of everything two spaces to the left.

You don’t change anything in the script. You change the variables passed to the script by the automation:

automation:
  - alias: 'test'
    trigger:
      - platform: state
        entity_id: input_boolean.mytest # change the trigger to what you need.
    action:
      - service: script.sonos_say
        data:
          sonos_entity: media_player.office  # Change this to the Sonos player you want to use
          volume: 0.5
          message: 'Your husband coming home!' # and this obviously
          delay: '00:00:05'

I tried that but it still throws the same error.

  1. Removed the first line.
  2. Corrected the indentation of every line two spaces to the left.

Maybe my eyes, but it looks like the hyphens are aligned with the E of sequence, should be aligned with the Q.

1 Like

Tried 10 different permutations. One indent each, 2 indent each, mix and match. No matter what I do it keeps complaining about the first line.

Maybe the ui editor has the naming key of the script elsewhere, so the screen you’ve posted there should start at alias and the sonos_say bit should be in a different box?

I know the ‘company line’ is to use the UI, but honestly for the sake of a simple copy and paste…

1 Like

Ah that was it. I don’t use the UI script editor so never would have figured that out.

1 Like

thanks. Here’s what worked.

1 Like

Was just a guess tbh, never use the ui either.

1 Like