Sonos playURI does not work triggered by dash button

I am quite new to openHAB and learning by doing and from code I find here with trial & error approach. Unfortunately I have never learned to write code thus not really written any code from scratch. But many things are working now already but I got stuck with something I considered simple.

I want to trigger a playuri command on my sonos when someone presses a dash button. Using the dash button for PLAY/PAUSE or switching HUE lights works well, so the button is working, but I don’t get my sonos to play this barking.mp3.

This is part of my .items file

// Abspielen von Files
String og_bad_sonos_PlayURI "Hundegebell" <text> { channel="sonos:PLAY1:RINCON_949F3E12345671400:playuri"}

and this is my rule for the trigger event

rule "Panic button - dog bark"
when
        Channel "amazondashbutton:dashbutton:ac-63-xx-xx-xx-xx:press" triggered
    then
        og_bad_sonos_PlayURI.sendCommand("http://10.82.1.yyy:8080/static/barking.mp3")
end

I also tried it with

rule "Panic button - dog bark"
when
        Channel "amazondashbutton:dashbutton:ac-63-xx-xx-xx-xx:press" triggered
    then
        sendCommand("og_bad_sonos_PlayURI", "http://10.82.1.yyy:8080/static/barking.mp3")
end

But it doesn’t work.
Then I tried with notificationsound and notificationvolume, - no way.

Using the URL in my Browser plays the barking properly, so the path is correct.
What am I doing wrong here? Any help appreciated, maybe just some simple thing.

Regards,

Uwe

Here’s my doorbell rule for comparison. I don’t use an amazon dash button i’m afraid so can’t comment on that component.

This works in OH2 fwiw.

var Timer doorbellTimer = null
val int timeoutSeconds = 2 // choose an appropriate value based on the length of the sound file

rule "Doorbell received ON"
when
    Item doorbellSwitch received command ON
then
	logInfo("Doorbell", "Someone is at the door")
        sendCommand(sonosPlay1Save, "ON") //saves what is currently playing
	sendCommand(sonosPlay1NotificationVol, 20) //sets the notification volume
	sendCommand(sonosPlay1Notification, "http://192.168.1.20:8080/static/doorbell.mp3")  //the sound file to play
	sendCommand(sonosPlay1Restore, "ON") // restores the previously playing music
    if(doorbellTimer == null) {
        doorbellTimer = createTimer(now.plusSeconds(timeoutSeconds), [|
            doorbellSwitch.sendCommand(OFF) //returns the doorbell switch to the off position so it can be triggered again if required.
            doorbellTimer = null
        ])
    }
    else {
        doorbellTimer.reschedule(now.plusSeconds(timeoutSeconds))
    }
end

items:

String		sonosPlay1Notification		"Notification Sound"	<songtitle>		(gSonosPlay1)			{channel="sonos:PLAY1:Dining:notificationsound"}
Dimmer 		sonosPlay1NotificationVol	"Notification Volume"	<volume>		(gSonosPlay1)			{channel="sonos:PLAY1:Dining:notificationvolume"}
Switch		sonosPlay1Save	                "Save Playlist"					(gSonosPlay1)			{channel="sonos:PLAY1:Dining:save"}
Switch		sonosPlay1Restore		"Restore Playlist"				(gSonosPlay1)			{channel="sonos:PLAY1:Dining:restore"}

Thanks Paul,

I got it to work now. My initial problem was, that the file barking.mp3 which is delivered with openHAB in openhab-conf/Sounds is coded with a sampling rate which is not supported by sonos :frowning2:
However, even when trying your URL for the doorbell (http://192.168.1.20:8080/static/doorbell.mp3) it did not work for me.
What worked out was the doorbell.mp3 file delivered with openhab, as sonos if fine with that file. So I copied it to openhab-conf/html and then it worked.

I did a setup from scratch with your code as the basis (but w/o the timers) as

var Timer doorbellTimer = null

shows an EOL error for me.

I like the save / restore portion which I learned from your code about. Thanks

So what I did now (and maybe oversized) is, that I used the dash button trigger event to trigger a (“virtual” Switch Item which then is linked to a similar rule as yours.

If anyone tries to do the same with a dash button (and if you find a way to do it smarter with less code please let me know), here it is:

Here we go with the items:

// Items for my Bathroom (bad in german language)

Player og_bad_sonos_ctrl "Control [%s]" (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:control" }
Dimmer og_bad_sonos_volume "Volume [%.1f %%]" <soundvolume> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:volume" }
Switch og_bad_sonos_mute "Mute" <soundvolume_mute> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:mute" }
Switch og_bad_sonos_led "LED" <switch> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:led" }
String og_bad_sonos_CurrentTrack "Now Playing [%s]" <text> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:currenttrack" }
String og_bad_sonos_state "Status [%s]" <text> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:state" }
String og_bad_sonos_Notification "Notification Sound" <songtitle> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:notificationsound" }
Dimmer og_bad_sonos_NotificationVol	"Notification Volume" <volume> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:notificationvolume" }
Switch og_bad_sonos_Save "Save Playlist" (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:save" }
Switch og_bad_sonos_Restore "Restore Playlist" (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:restore" }

// virtual Switch Item for Amazon Alexa to switch sonos to PLAY/PAUSE via respective rule
Switch og_bad_sonos_PlayOrPauseRadio "Sonos im Bad" [ "Switchable" ]

// virtual Switch Item for Amazon Dash button to play from file
Switch og_bad_sonos_panic "Panic Switch Item"  [ "Switchable" ]

And the rules:


 // Play or pause bathroom speakers, toggle with Dash 1 Button
 rule "Dash 1 pressed to trigger sonos bad"
    when
        Channel "amazondashbutton:dashbutton:ac-63-be-zz-yy-xx:press" triggered
    then	
		
	if (og_bad_sonos_ctrl.state.toString == "PLAY") { 
        og_bad_sonos_ctrl.sendCommand(PAUSE)
        } 
    else if (og_bad_sonos_ctrl.state.toString == "PAUSE") {
        og_bad_sonos_ctrl.sendCommand(PLAY)
        }
end

// Toggle virtual switch item to ON / OFF Dash 2 Button - Part 1
rule "Panic dash button" 
    when
        Channel "amazondashbutton:dashbutton:ac-63-be-xx-yy-zz:press" triggered
    then
        if (og_bad_sonos_panic.state.toString == "ON") {
            og_bad_sonos_panic.sendCommand(OFF)
        } else {
            og_bad_sonos_panic.sendCommand(ON)
        }
end

// Toggle virtual switch item to ON / OFF Dash 2 Button - Part 2
rule "Panic received update" 
    when
        Item og_bad_sonos_panic received update
	then
		logInfo("Panic button pressed", "Someone needs help at the door")
    	sendCommand(og_bad_sonos_Save, "ON") //saves what is currently playing
		sendCommand(og_bad_sonos_NotificationVol, "50") //sets the notification volume
		sendCommand(og_bad_sonos_Notification, "http://10.82.1.yyy:8080/static/doorbell.mp3")  //the sound file to play
		sendCommand(og_bad_sonos_Restore, "ON") // restores the previously playing music
end

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

I am quite sure that I might not need both rules for the Panic button and somehow can live with the trigger channel from the dash button, but at least this works now.

Uwe

I simplified the rules, working well now with the Thread::Sleep settings to not interrupt the bell too early.
All in all it takes 6 seconds from pressing the dash button until the doorbell rings - which is mainly due to the dash button requiring time to start, connect to WiFi, send command. Ideas on how to speed things up are welcome, but I think this is what you have to deal with when using a dash button.

This is the working items section now. The virtual switch item for the dash button is obsolete now.

//------------------------------
// Sonos Bathroom (bad) first floor (og)
Player og_bad_sonos_ctrl "Control [%s]" (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:control" }
Dimmer og_bad_sonos_volume "Volume [%.1f %%]" <soundvolume> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:volume" }
Switch og_bad_sonos_mute "Mute" <soundvolume_mute> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:mute" }
Switch og_bad_sonos_led "LED" <switch> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:led" }
String og_bad_sonos_CurrentTrack "Now Playing [%s]" <text> (Sonos) { channel="sonos:PLAY1:RINCON_949F3E06744201400:currenttrack" }
String og_bad_sonos_state "Status [%s]" <text> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:state" }
String og_bad_sonos_Notification "Notification Sound" <songtitle> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:notificationsound" }
Dimmer og_bad_sonos_NotificationVol	"Notification Volume" <volume> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:notificationvolume" }
String og_bad_sonos_favorite "Favorite [%s]" <text> (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:favorite" }
String og_bad_sonos_radio "Radiosender [%s]" <text> (Sonos){ channel="sonos:PLAY1:RINCON_949F3E06744201400:radio" }
Switch og_bad_sonos_Save "Save Playlist" (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:save" }
Switch og_bad_sonos_Restore "Restore Playlist" (Sonos) { channel="sonos:PLAY1:RINCON_949F3Exxxxxxx1400:restore" }
end

The rules have been simplified now:

// Simplified Doorbell Test - Sample with Dash Button
rule "Doorbell Test - Amazon Dash Gilette"
when
        Channel "amazondashbutton:dashbutton:ac-63-be-xx-yy-zz:press" triggered
    then
        logInfo("Doorbell button pressed", "Someone at the door")
    	og_bad_sonos_Save.sendCommand("ON") //saves what is currently playing
		og_bad_sonos_ctrl.sendCommand("PAUSE") //pause what is currently playing
		og_bad_sonos_volume.sendCommand("80") //sets the volume for the doorbell
		Thread::sleep(500) // avoid sudden interruption
		og_bad_sonos_favorite.sendCommand("Doorbell")  //the sound file to play from sonos Favorites
		Thread::sleep(3000) // avoid sudden interruption
		og_bad_sonos_Restore.sendCommand("ON") // restores the previously playing music
end

I’musing the “say” command with my sonos boxes, the implemantation of that one for sonsos takes care of the Save and Restore by itself (i.e. the previously played source is continued) I think it’s the same for the “playSound” command. (Keeping fingers crossed. I found out about that by looking at the debug logs while using the say command.

So something like the following should be working for you:

rule “PlaySomething”

when
Channel “amazondashbutton:dashbutton:ac-63-be-xx-yy-zz:press” triggered
then
logInfo(“Doorbell button pressed”, “Someone at the door”)
var string AudioSink="sonos:PLAY1:RINCON_xxxxxxx1400 //put in your correct values!!
playSound(AudioSink,“doorbell.mp3”)
end

Hi Jürgen,

thanks for the input. I will give it a try.

am currently also testing with say command but don’t get it to work yet :frowning2:

rule "Say Something Test - Amazon Dash Gilette"
when
        Channel "amazondashbutton:dashbutton:ac-63-be-xx-yy-zz:press" triggered
    then
        logInfo("Gilette button pressed", "Hallo Welt")
    // Badezimmer	
    	og_bad_sonos_Save.sendCommand("ON") //saves what is currently playing
		og_bad_sonos_ctrl.sendCommand("PAUSE") //pause what is currently playing
		og_bad_sonos_NotificationVol.sendCommand("80") //sets the notificationvolume for bathroom speaker
		Thread::sleep(500) // avoid sudden interruption
		say("Hallo Welt","voicerss:deDE","sonos:PLAY1:RINCON_949F3Exxxxxxx1400")
		Thread::sleep(3000) // avoid sudden interruption
		og_bad_sonos_Restore.sendCommand("ON") // restores the previously playing music 
end

Maybe the Voice Service is not yet properly running/configured.

In my voicerss.cfg I have only the setting for my API-Key.
Under PaperUI-System-Configuration-Voice I have set Default TextToSpeech to “VoiceRSS TextToSpech Engine” and Default Voice is set to "VoiceRSS Deutdch(Deutschland)"
Additionally I have set one of my sonos boxes as a Default audio-Sink under PaperUI-System-Configuration-Audio.

[Edit:]
Here is my “Say Something” rule:

val Number NotificationVolume = 20
rule "Say Something"

when 
  Item SayCommand received update
then
  var string AudioSink
  switch AudioSink {
    case Lautsprecher.state.toString=="Küche" : AudioSink="sonos:PLAY1:RINCON_xxx1400"
    case Lautsprecher.state.toString=="Wohnzimmer" : AudioSink="sonos:PLAY1:RINCON_yyy1400"
    case Lautsprecher.state.toString=="Gästezimmer" : AudioSink= "sonos:PLAY1:RINCON_zzz1400"
    default:AudioSink="sonos:PLAY1:RINCON_xxxx1400"
    }
  //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"
//sets the notificationVolume for all players at systemstart!
when 
  System started
then
   gPlayer_NotificationVolume.allMembers.forEach [item | item.sendCommand(NotificationVolume)]
   logInfo("IntialNotificationVolumeSetting","NotificationVolume set!") 
end

You saved my day with the hint for an API key. I just did not know that voice rss requires registration and an api key. Now it works fine with the say command.
The playSound did not work when I just tried it.

Another issue with it would be that you can not define Thread:Sleep in the rules which is required to play the sound without wrong interruptions.

Thanks for the quick response. Now I learned how to use the say command with voicerss.

Uwe

No problem, tried to send the actually created sound-file from my service as an “example”. :disappointed: This forum doesn’t allow sending mp3’s.