SONOS TTS - Go back to queue


I've made a simple remote being able to control two different Sonos speakers (Kontor and Spisebord), one at a time.


My challenge is with Sonos TTS. When Sonos TTS is played, it is played as a an "special" song with no relation to the Sonos Queue. So when I resume the music the Sonos TTS is replayed. I cannot skip to next song or go to the previous song, since the TTS is not in the queue.

How do you tackle this?`


I suggest seeing if the playTextAndRestore command will work for you. I don't think there's a way to make this happen from Rule Machine's built-in actions, but it's not too difficult to do by using a Custom Action instead of the default Speak action.

But first: test it. Go to your device page in the admin UI. For Sonos (more generally, "Music Player" devices), you should see a "Play Text and Restore" command you can run. Fill in something for the text (the volume level is optional) while you have music playing, then click the button to execute the command. See if it speaks the text and then resumes the music as you expect. My guess is it will.

If so, to make this work in Rule Machine, don't use the "Send or Speak a Message" action. Instead, use the "Set Mode or Variables, Run Custom Action" action type, and choose "Run Custom Action" as the actual action. Then, for "Select capability of action device," choose "Music Player" (or really any capability that will make your Sonos show up). Then for "Select custom command," choose playTextAndRestore. Add a string parameter with your desired text. Optionally, add a Number parameter with your desired volume. Then, click "Done With this Action," and you should be good (as long as you inserted it into the right part of your rule...I should suggest using "Insert Action Before" when you start all of this :slight_smile: ).

Iโ€™m looking for this as well, and I just tried the play and restore and this didnโ€™t work for me on a Sonos beam with TV audio as the input. Will try on my other Sonos devices.

Hi @bertabcd1234.

Sorry for all the updates I've done on this post. I've discovered a lot and that is why. :slight_smile:

1. The playTextAndRestore command did not work.
It behaves the same way as Play Text and Speak in this scenario.
If the speaker is already playing music the TTS will perform and the music will resume. But when the speaker is not playing the TTS will speak and the audioSource will be set to "Unknown" as the TTS is.

2. What I've learned
When a TTS is played on the sonos the audioSource of the trackData is set to Unknown and then provided with a link to the TTS.
So it looks like this:

trackData : {"audioSource":"Unknown"}
I need to switch the audioSource back to Sonos Q.
It looks like this:

trackData : {"audioSource":"Sonos Q","station":null,"name":"........."}
If i need to do it to my Sonos Playbar the audioSource mich be switched back to something similar to this:

trackData : {"audioSource":"Aux Input","station":null,"name":........."}
I cannot see the option to change audioSource as a part of the Device List Commands. Any suggestion?

3. Solution (I need help here)
We need to extract 2 fields from trackData under the Current Stats of the device:

  • uri. When this is extracted we use the command Set Track with the result [1].
  • trackNumber. After we use Set Track we make a loop that runs trackNumber - 1 and uses the command Next Track in the loop.

I don't know how to retrieve these numbers, since they are in an array in a variable. If you say where I can program it, I will try. But there is no easy with variable swapping.

[1] URI is also available under State Variables, but as Mike stated:

Link: Device State Variables Not Updating in UI


Sorry for the delay. I discovered a lot and have posted my findings in a edit of the post above.

I'm tracking this thread, curious what you are stuck on, I'm not good at groovy / HE programming, but happy to try stuff.

1 Like

How do I add the value to select AWS voice to TTS action?
I would like to select the TTS voice โ€œRussellโ€

This issue sounds unrelated to the above, which is now also over three years old, so it may have been best to create a separate topic for this question. You will also need to provide more information, in particular, what app you are using to perform the TTS action. Let's assume the relevant piece is that you are using Rule Machine, as above (albeit now a newer version).

In this case, it does not look like any of the built-in RM actions allow you to choose a voice. However, the "Speak" command itself (which is likely what RM calls underneath the hood) does:
Screenshot 2023-04-25 at 11.48.57

So, the first thing I'd do is test this command from the device detail page to make sure it works for you. Type in something for the "Text" parameter, see if your desired "Voice name" parameter has the intended effect, and then click/tap the "Speak" button itself to execute the command. ("Volume," like any parameter without an asterisk, is optional.)

If so, you can use a "Custom Action" in Rule Machine to do this for you:

You'll need to add three parameters, a string, a number, and another string, corresponding to the three parameters, in order, you see under "Speak" on the device detail page โ€” which I suggested trying first since it's much easier to test things there.