Making a switch item for sonos in OH2

I have been using OH1 for quite some time, and have now started the merge towards OH2.

For my first ‘hello world’ test, I am trying to use an Amazon Echo Dot to play/pause my Sonos kitchen speaker.

I have OH2 beta 4 installed on a Raspi3, and can access the UIs through the web interface. I installed the Sonos binding through the paper UI, and it automagically found my speakers (nice feature!). Through the paper UI I can control the speaker, and play/pause. So far, so good.

I then want to set up my own sitemap, and make a simple test switch to play/pause the speaker. Later, I can make Alexa control the switch, without exposing it in the UI.
This is where I start to struggle…

The Sonos device is in the paper UI, but I guess that I need to create it in config through Eclipse as well, to make the Switch item.

I have simplified as much as possible, to try and locate the problem.

The only config file in the system is:

sonos.items

Player SonosKitchenControl    "Control [%s]" { channel="sonos:PLAY3:RINCON_XXXXXXXXXXXXXXXXX:control"}

Switch SonosKitchenOnOff    "Play/Pause"    { sonos="[ON:sonos:PLAY3:RINCON_XXXXXXXXXXXXXXXXX:control:play], [OFF:sonos:PLAY3:RINCON_XXXXXXXXXXXXXXXXX:control:pause]"}

and the simple sitemap:

test.sitemap

sitemap test label="Main Menu"
{
        Frame label="Sonos Kitchen" {
            Default item=SonosKitchenControl label="Sonos" icon="settings"
            Switch item=SonosKitchenOnOff
          }
}

When I navigate to the classicUI and append the sitemap to the url, i get the two items, as expected. However, only the ‘Player’ item is working. I get a play and a pause button, which works perfectly.
I also get the switch item shown, but nothing happens when I use it.

I have tried replacing ‘play’ and ‘pause’ with ‘start’ and ‘stop’, with no change.

It is great that the stability has improved for the OH2 Sonos binding, but the documentation is extremely limited: https://github.com/sumnerboy12/openhab2/tree/master/addons/binding/org.openhab.binding.sonos

Any tips, or pointing out any obvious mistakes I made, will be appreciated.

(on a side note: It seems that when you create a new sitemap through eclipse, you need to restart the system (or maybe just OH), before the sitemap is available to browse. This took me a bit of time to realise)

I don’t know sonos but there seems to be a mix of OH 2 and OH 1 style Item definitions.

With OH 2 bindings you will only ever see { channel="<channel ID>" } where <channel ID> is the name of a Channel on a Thing.

Based on the documentation it looks like the binding only supports Play/Pause using a Player Item which would explain why the first one works and the second one does not.

I recommend reading the following to understand Things, Channels, and Items and how they relate to each other.
http://docs.openhab.org/concepts/index.html

Thank you for the input.

I re-read the documentation on things, which I believe that I have understood.
To simplify things, I just wanted to both define the Switch item, and define the actions of it, in the same line.
I still don’t know how to do this, or if it is impossible because of the Things and items scheme.

However, I now have it working, by doing the following:

Defining my Sonos speaker in sonos.things:

Thing sonos:kitchen:1 [ udn="RINCON_XXXXXXXXXXXXXXXXX", refresh=60]

Then defining the player item linked to that thing, and the switch in sonos.items:

Player SonosKitchenControl    "Control [%s]" { channel="sonos:kitchen:1:control"}
Switch PlayOrPauseRadioInKitchen "Radio In Kitchen" [ "Switchable" ]

And instead of defining the actions of the switch, in the same line in the .items file, I created a rule to do what I want, by sending command to the Player item, in a sonos.rules file:

// Play or pause kitchen speakers, with Alexa
rule "play or pause kitchen speakers"
when
    Item PlayOrPauseRadioInKitchen received update
then
    if (PlayOrPauseRadioInKitchen.state==ON) {
        sendCommand(SonosKitchenControl, PLAY)
    } else {
        sendCommand(SonosKitchenControl, PAUSE)
    }
end

After exposing the PlayOrPauseRadioInKitchen item in the Hue bridge, my amazon echo recognised it, and I can now say “Alexa, turn on the kitchen radio” or “Alexa, turn off the kitchen radio”. She will reply with an “OK”, and the rule executes perfectly.

So, I am marking this as solved, altough I still wanted to implement this behaviour directly in the Switch item deifinition, instead of having a rule. On the other hand, I can now start expanding the rule to make sure that the right speakers are grouped, set a specific volume level, and add more rules and switches to handle specific requests to start different online radio streams via the many available channels in the sonos binding.

1 Like

hi this my item:

Player SONOS1_controller "Control" <mysonoswhite> (controllerGroup) {channel="sonos:PLAY1:RINCON_5CAA445404DA01400:control"}

this the sitemap:

Default item=SONOS1_controller

no rule needed

1 Like

Yes, that works as well, for getting the control in the UI, although it does not utilize the concept of ‘Things’ in OH2.

However, it is not enough for me to have the player item in the sitemap and UI.
I want to control my sonos speakers just by talking to Alexa, without having to use the UI.

For this, I need the additional Switch, and the rule.

Correction: the .thing part does not work after all, but seemed to do so, because the change I made in the files apparently takes some time before taking effect.

This works, without a .thing file:

Sonos.items:

Player SonosKitchenControl    "Control [%s]" { channel="sonos:PLAY3:RINCON_XXXXXXXXXXXXXXXXX:control"}
Switch PlayOrPauseRadioInKitchen "Radio In Kitchen" [ "Switchable" ]

Sonos.rules:

 // Play or pause kitchen speakers, with Alexa
    rule "play or pause kitchen speakers"
    when
        Item PlayOrPauseRadioInKitchen received update
    then
        if (PlayOrPauseRadioInKitchen.state==ON) {
            sendCommand(SonosKitchenControl, PLAY)
        } else {
            sendCommand(SonosKitchenControl, PAUSE)
        }
    end

@Mivia did you find a way to rename the sonos items they all come up in PaperUI as (like your example):

sonos:PLAY3:RINCON_XXXXXXXXXXXXXXXXX

I have 3 play3 speakers and wanted to name time:

sonos:Lounge:RINCON_XXXXXXXXXXXXXXXXX
sonos:Kitchen:RINCON_XXXXXXXXXXXXXXXXX
etc.

But cant see a way to do this in OpenHab2 in openhab1 i could do this via the config file.

@James_Hiscott no, I believe that you can do something like I tried in my items file:

Thing sonos:kitchen:1 [ udn="RINCON_XXXXXXXXXXXXXXXXX", refresh=60]

But as previously stated, I could not get this working like I wanted to. Maybe one day we will get a bit more examples for documentation in the new OH2 sonos binding. For now, I guess that it is reasonable enough to have a binding for a beta version not fully documented. Will keep an eye out for any updates…

for me it’s working when using this notation:

creating a sonos.things

Thing sonos:PLAY1:kueche 	[ udn="RINCON_5CAAFDC4F60201400", refresh=60]
Thing sonos:PLAY1:schlafzimmer 	[ udn="RINCON_949F3E12F88E01400", refresh=60]

and my sonos.items look like this:

Player so_ku_controller   "Kueche Controller"                          (Sonos) {channel="sonos:PLAY1:kueche:control"}
Dimmer so_ku_Volume       "Kueche Volume [%.1f %%]" <soundvolume>      (Sonos) {channel="sonos:PLAY1:kueche:volume"}
Switch so_ku_Mute         "Kueche Mute"             <soundvolume_mute> (Sonos) {channel="sonos:PLAY1:kueche:mute"}
String so_ku_CurrentTrack "Kueche Now playing [%s]" <text>             (Sonos) {channel="sonos:PLAY1:kueche:currenttrack"}

Player so_sz_controller   "SchlaZi Controller"                          (Sonos) {channel="sonos:PLAY1:schlafzimmer:control"}
Dimmer so_sz_Volume       "SchlaZi Volume [%.1f %%]" <soundvolume>      (Sonos) {channel="sonos:PLAY1:schlafzimmer:volume"}
Switch so_sz_Mute         "SchlaZi Mute"             <soundvolume_mute> (Sonos) {channel="sonos:PLAY1:schlafzimmer:mute"}
String so_sz_CurrentTrack "SchlaZi Now playing [%s]" <text>             (Sonos) {channel="sonos:PLAY1:schlafzimmer:currenttrack"}

this seems to work for now.
but I’m not sure if this is correct because the things are now twice in paperUI => control

@DRAGandDROP Thanks for that, I was going to say what did you do with the items that get discovered in PaperUI.

Will give it a try as you suggest till someone else can help out with the “real” way.

@Mivia Yep i know, I was confused as the binding is 1.9 and not 2.0 so wonder if that is why it only has examples for openHab 1.x

Will update this thread if i work anything else out.

Hi all,

I am experimenting with the same issue.

In OH1.8.3 I had the following items:
Switch SonosBad_PlayPause {sonos="[ON:bad:play],[OFF:bad:pause]"}
that worked well.

In OH2 I tried following:
Switch SonosBad_PlayPause {channel="[ON:sonos:PLAY1:bad:play],[OFF:sonos:PLAY1:bad:pause]}
that don’t work.

Isn’t it supported anymore or did I do a wrong definition? (I mean the option of the item “switch”, to define ON and OFF like I did in OH1.8.3?)

@Mivia Thanks for the workaround.

Hi, thanks for great thread! I just set up openhabian on a PI3 and followed @Mivia’s steps for exposing my Sonos device for Alexa. First set up in Paper UI (Sonos controllers working), and after that created three files:

  1. created
    /etc/openhab2/things/sonos.things
    (using ssh nano) with following content:
    Thing sonos:kitchen:1 [ udn="RINCON_XX", refresh=60]
    (where RINCON_XX is the udn of the thing I have successfully set up and tested in Paper UI)
  2. created
    /etc/openhab2/items/sonos.items
    (using ssh nano) with following content (nothing more in file):
    Player SonosKitchenControl “Control [%s]” { channel=“sonos:kitchen:1:control”}
    Switch PlayOrPauseRadioInKitchen “Radio In Kitchen” [ “Switchable” ]
  3. created:
    /etc/openhab2/rules/sonos.rules
    (using ssh nano) with following content:
    // Play or pause kitchen speakers, with Alexa
    rule "play or pause kitchen speakers"
    when
    Item PlayOrPauseRadioInKitchen received update
    then
    if (PlayOrPauseRadioInKitchen.state==ON) {
    sendCommand(SonosKitchenControl, PLAY)
    } else {
    sendCommand(SonosKitchenControl, PAUSE)
    }
    end

I next told Alexa to discover devices (“Alexa, discover devices”) and was happy to hear that she had added a new device.
And when I followed with “Alexa, turn on the kitchen radio”, Aleza responded with “OK”, but nothing happened.

In log files I found following:
2017-07-08 17:56:45.657 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'sonos.things’
2017-07-08 17:56:45.743 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sonos.things’
2017-07-08 17:57:30.583 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'sonos.items’
2017-07-08 17:57:30.617 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sonos.items’
2017-07-08 17:58:55.423 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'sonos.rules’
2017-07-08 17:58:56.004 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sonos.rules’
2017-07-08 18:00:13.734 [WARN ] [ome.core.thing.internal.ThingManager] - Cannot delegate command ‘PLAY’ for item ‘SonosKitchenControl’ to handler for channel ‘sonos:kitchen:1:control’, because no thing with the UID ‘sonos:kitchen:1’ could be found.

This I didn’t quite understand.
My Thing, “sonos:kitchen:1” as set up in the sonos.things file, is set up with ID of Item I am using in Paper UI, and which is working in Paper UI Control panel. But I am thinking I have lost something here but not quite sure what. Anybody has an idea? Could there be some conflict with what has been set up using Paper UI, or should I have set up my sonos.things differently?

I’d change the rule to use the strings"PLAY" and “PAUSE” instead of unknown variables.
Additionally I would use a logInfo in order to get at least a feedback in the log if the rule is triggered at all.

What does PaperUI show for your SonosThing? Does it really show “sonos:kitchen:1” as the name of it, or does it only show a “1” as the name?

Ok, so I guess my sonos.things is probably wrongly set up.
Under
Paper UI > Things
I find the device as
sonos:zoneplayer:RINCON_XX
(replaced the full id with XX here)
I thought the ID was the only thing that mattered, and that the name I used in sonos.things was for referce in the sonos.items file. But should I adjust the name from:
Thing sonos:kitchen:1 [ udn="RINCON_XX", refresh=60]
to
Thing sonos:zoneplayer [ udn="RINCON_XX", refresh=60]
probably? Going to look more at this later today.

What is your setting for “Configuration”-“Item Linking”-“Simple Mode”? In case this mode is ON, OH will do item linking on its own, which migth explain why your things are named not the way you expect.

My overview for things shows :

PlayKueche ONLINE
PLAY 1
sonos:PLAY1:RINCON_B8E937E0C16201400

But I do not use a things file, I used the PaperUI alone to setup my things.
As far as I understand the configuration in your initial setup the name of your Player would have been “1”!

Got it working now!
Tried reading the docs http://docs.openhab.org/configuration/things.html and being an amature I was first confused with the “binding ID”, “type ID” and “Thing ID”. But I discovered that both binding and type are specified in the binding configuration (Sonos binding in this case).
But then I don’t understand how Mivia’s setup would be working since there is no sonos binding type called “kitchen”?
Further I gathered that the “Thing ID” could be anything I would define it to be in the sonos.things file:
Thing sonos:zoneplayer:stue [ udn="RINCON_XX", refresh=60]
and afterwards updated the sonos.items file accordingly:
Player SonosKitchenControl "Control [%s]" { channel="sonos:zoneplayer:stue:control"} Switch PlayOrPauseRadioInKitchen "Radio In Kitchen" [ "Switchable" ]
And now it works! Thanks guys for this thread!