Sonos binding - works perfectly

Just to say that this is excellent - thank you to everyone who contributed to it.

Do you mind sharing your setup?

I am having some problems with getting this to work, but I am pretty sure it is me doing something stupid, and not the binding that is at fault, :slight_smile:

It partially worked for me. The Thing data and commands worked well but I had trouble with the audio sink for notifications (unreliable or truncated audio). I’ve started using the sonos-http-api and now I can discover Sonos favorites and automatically set up the openHAB side of Alexa voice control for them.

Although I’m only using those notifications seldom they do work reliable for me. (“Speeking” to visitors while I’m away is sort of fun;-) )
My Setup: OH2.1 stable on a Raspi2, SonosBinding, TTS via VoiceRSS

Do yo mean this https://github.com/jishi/node-sonos-http-api?

And then you use the HTTP binding from openHAB, or am I on the wrong track?

Yes, that’s it. I use the HTTP ReST API from my Jython scripts/rules. The scripts create an item provider, query the service for favorites and then creates and registers favorites-related switch Items with Alexa tags. I then have rules to respond to the switch changes and send ReST commands to the sonos-http-api.

I found the notifications slow/unreliable when using the audio sink but much faster/reliable using the defined String item as follows:

String SonosPlayStudy { channel="sonos:CONNECTAMP:RINCON_5CAAFDE5A1E401400:notificationsound" }

then

sendCommand(SonosPlayStudy,"http://192.168.1.x:8080/static/tone.mp3")

Where the tone.mp3 file is placed in the etc/openhab2/html folder.

I’m running OH2.1 final

Interesting. I didn’t try that specific approach. I was using the word “notifications” loosely. My primary goal was TTS through the Sonos and it appears the item/command technique wouldn’t work for that purpose. I tried the Sonos audio sink for both TTS and sending audio files without much success (although it apparently works for some others).

I’m running an OH2.2 snapshot.

My explicit setup:
Items in .items file:

/*Sonos-Player*/ 
/*Kueche*/
Dimmer PlayKueche_Volume "Volume: [%s]" (EG_Kueche, gPlayer_Volume) {channel="sonos:PLAY1:RINCON_XYZ1400:volume"}  
Player PlayKueche_Controller (EG_Kueche) {channel="sonos:PLAY1:RINCON_XYZ1400:control"}
Dimmer PlayKueche_NotificationVolume "NotificationVolume"(EG_Kueche, gPlayer_NotificationVolume) {channel="sonos:PLAY1:RINCON_XYZ1400:notificationvolume"}

/*Gästezimmer */
Dimmer PlayGaestezimmer_Volume "Volume: [%s]" (OG_Gaestezimmer, gPlayer_Volume) {channel="sonos:PLAY1:RINCON_XXX1400:volume"}   
Player PlayGaestezimmer_Controller (OG_Gaestezimmer) {channel="sonos:PLAY1:RINCON_XXX1400:control"}
Dimmer PlayGaestezimmer_NotificationVolume "NotificationVolume"(OG_Gaestezimmer, gPlayer_NotificationVolume) {channel="sonos:PLAY1:RINCON_XXX1400:notificationvolume"}/*Wohnzimmer*/

// Wohnzimmer
Dimmer PlayWZ_L_Volume "Volume: [%s]" (EG_Wohnzimmer, gPlayer_Volume) {channel="sonos:PLAY1:RINCON_YYY1400:volume"}   
Player PlayWZ_L_Controller (EG_Wohnzimmer) {channel="sonos:PLAY1:RINCON_YYY1400:control"}
Dimmer PlayWZ_L_NotificationVolume "NotificationVolume"(EG_Wohnzimmer, gPlayer_NotificationVolume) {channel="sonos:PLAY1:RINCON_YYY1400:notificationvolume"}

//Chromecast

Dimmer ChromeRadio_Volume "Volume: [%s]" (EG_Wohnzimmer, gPlayer_Volume) {channel="chromecast:audio:abcd:volume"}   
Player ChromeRadio_Controller (EG_Wohnzimmer) {channel="chromecast:audio:abcd:control"}
String SayCommand
String Lautsprecher

on HabPanel:

<div class="form-inline">
  <div class="form-group">
    <input type="text" class="form-control" no-snap-drag="true"
           ng-model="myvalue" ng-value="itemValue('SayCommand')"></input>
    <button type="button" class="btn btn-primary"
           ng-click="sendCmd('SayCommand', myvalue)">Sag es!</button>
  </div>
  <div class="btn-group" uib-dropdown>
  <button id="single-button" type="button" class="btn btn-primary" uib-dropdown-toggle>
    {{itemValue('Lautsprecher')}} <span class="caret"></span>
  </button>
  <ul class="dropdown-menu" uib-dropdown-menu role="menu" aria-labelledby="single-button">
    <li role="menuitem"><a ng-click="sendCmd('Lautsprecher', 'Küche')">Küche</a></li>
    <li role="menuitem"><a ng-click="sendCmd('Lautsprecher', 'Wohnzimmer')">Wohnzimmer</a></li>
    <li role="menuitem"><a ng-click="sendCmd('Lautsprecher', 'Gästezimmer')">Gästezimmer</a></li>
    <li role="menuitem"><a ng-click="sendCmd('Lautsprecher', 'ChromeRadio')">ChromeRadio </a></li>
    
</div>

Rules:

val Number NotificationVolume = 20
val Number MaxVolume=30
rule "SagEtwas"


when 
  Item SayCommand received update
then
  var string AudioSink
  switch AudioSink {
    case Lautsprecher.state.toString=="Küche" : AudioSink="sonos:PLAY1:RINCON_XYZ1400"
    case Lautsprecher.state.toString=="Wohnzimmer" : AudioSink="sonos:PLAY1:YYY1400"
    case Lautsprecher.state.toString=="Gästezimmer" : AudioSink= "sonos:PLAY1:RINCON_XXX1400"
    case Lautsprecher.state.toString=="ChromeRadio" : AudioSink= "chromecast:audio:abcd"
    default:AudioSink="sonos:PLAY1:RINCON_YYY1400"
    }
  gPlayer_NotificationVolume.allMembers.filter(s | s.state!=NotificationVolume ).forEach [item | item.sendCommand(NotificationVolume)]
  say(SayCommand.state.toString,"voicerss:deDE",AudioSink)
  logInfo("SayCommand","Es ist alles gesagt!")
end

rule "IntialNotificationVolumeSetting"
//notwendig, damit NotificationVolume von Anfang an den korrekten Wert hat!
when 
  System started
then
   gPlayer_NotificationVolume.allMembers.forEach [item | item.sendCommand(NotificationVolume)]
   logInfo("IntialNotificationVolumeSetting","NotificationVolume set!") 
end 

rule "MaxVolumeChecker"

when
    Item PlayKueche_Volume changed or
    Item PlayGaestezimmer_Volume changed or
    Item PlayWZ_L_Volume changed
then
    logInfo ("MaxVolumeChecker", "MaxVolume ={}", MaxVolume)
    gPlayer_Volume.allMembers.filter(s | s.state >= MaxVolume).forEach[ s |
        s.sendCommand(MaxVolume)
    ]
end

SetUp on PaperUI

Default AudioSink: PlayWZ_L
Default TextToSpeech VoiceRSS Text_to_Speech Engine
Default Voice VoiceRSS German(Germany)

How does this work? I see you defined items with a “PlayWZ_L” prefix but I don’t see any audio sinks with that Sonos device/thing identifier? Where does PaperUI get the “PlayWZ_L” id?

In my case, the Paper UI only showed “sonos:PLAY1” as a possible sink although I have three Play 1 speakers with unique device/thing identifiers.

I let OH detect the Sonos-things by itself and used the auto detected channel names in the items file. Yes, I changed the actual ID before posting it in here.The names of the Things were custom set corresponding to the item names (I. E. PlayWZ_L)

Are you saying you let OH2 discover the Sonos Things (which I also did) and then you manually changed the names (vs. the UIDs like “sonos:PLAY1:RINCON_XYZ1400”) of the discovered things to names like “PlayWZ_L”?

If so, I didn’t change the names of the discovered Sonos Things so maybe OH2 assigned the same name to all three devices and that confused the Paper UI?

Correct, I did exactly that!
However the original names where all specific . E. I. each device had been assigned a different name by OH already, I just wanted to have a more readable one.

Have your 3 Sonos a different room name ?

If that question was for me, YES my Sonos devices haves room-specific names. However I posted my WORKING Setup, thanks to your work :+1:. @steve1 seems to have Problems especially with the notifications.

It seems he has even problems with the discovery which is weird.

No, discovery (as in, dynamic discovery of Sonos devices) is one feature that worked as expected. However, it’s possible the same name (different UID) was assigned to each discovered thing (if that’s what you mean by “discovery”).

As I’ve said before, my primary problem was with the audio sink. I didn’t use the Thing notification channel so I can’t comment on that. I was attempting to use TTS for voice “notifications” via the audio sink. This wasn’t just a Thing naming issue because I also tried it from the Karaf console using the UIDs. There was no audio when the speakers were grouped. When they were ungrouped the audio was truncated to a fraction of a second. It was the same behavior for voice or for audio files played from Karaf (and consistent with related ESH actions). So far, the most significant difference from other installations seems to be that I’m running the OH2.2 snapshot, so it’s possible this is a new problem.

I am also running OH 2.2 and I have no problem. By the way, I am working to improve the sound notification system.

Incidentally, after further experiment I found that the notifications were sometimes very delayed, and guessed this might be because the Sonos boxes go to sleep after a while. So I’ve a cron rule which puts a silent MP3 through each every hour, after testing to make sure there’s no track currently being played. Touch wood I now have almost-instant notifications (using the notificationsound channel).

Is there a way to play notifications out of many units all in sync?