Set variable value for Sonos preset from smartthings


#1

1) Give a description of the problem
I am trying to make a single piston that will control multiple virtual “playlist” switches and play the corresponding sonos preset. I’m not very knowledgable with expressions and haven’t been able to find a similar solution online.

2) What is the expected behaviour?
When I turn on the Eagles playlist, sonos will play the preset number 7. If I turn on the Foreigners playlist, preset 12 will play.

3) What is happening/not happening?
I can’t get the correct preset ID value when I select any playlist but the first one in the list.

**4) Post a Green Snapshot of the piston![image|45x37]

5) Attach logs after turning logging level to Full
(PASTE YOUR LOGS HERE THEN HIGHLIGHT ALL OF THE LOGS AND CLICK ON THE </> ICON TO FORMAT THEM CORRECTLY)

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

The correct form of arrayItem() is arrayItem(index,list), wherein index is an integer value starting at 0. Using your variable Preset, arrayItem would produce the following values with the given index.

arrayItem(0,Preset) = 7
arrayItem(1,Preset) = 12

The problem is that your piston is using Station, a device variable as an index and that’s simply not going to work. You need to use either an explicit integer value or an integer variable. To do that, you need to convert Station into an integer value by using IF/THEN, Switch or others statements.

The most rudimentary form of this (assuming there are only two switches and two stations), would be

IF
Station = Switch 5
Then
ID = 5
ELSE
ID = 7
ENDIF

This could also be written:

Set Variable {ID} = (Station=“Switch 5” ? 5 : 7)

Again, you will have to alter your approach if you are working with more stations, but hopefully you get the idea.

As an aside, I can’t recall if playPreset is actually working with Sonos anymore, as there were a lot of changes recently to the API that are not reflected in webCoRE. I use a node.js server with webCoRE to manage all my Sonos devices and presets, so playPreset is just not something I use anymore and so I don’t know.


#3

You may find this interesting as well… Any fast way to populate an array?


#4

Thank you both for your assistance. As is fairly obvious, I am not well-versed in the expressions arena. The Sonos playlist ID listed in the smartthings device status does work but I was hoping not have to create a virtual switch titled with a number, hence the attempt to associate the playlist name with the corresponding playlist ID number but I just can’t figure that part out.

I went to plan B and created virtual switches with the matching sonos ID number. I am using this in sharptools as a dashboard juke box. I took a look at sharptools rules but it was too limited for my needs.