[SOLVED] Sonos - Increase/Decrease volume (do-while action)

Hi,
I’m trying to create a rule that allows me to control the volume of my Sonos player using a KNX switch.

These are the relevant items:

Switch KNX_GV_Keuken_VolumeUp_10_1_1							"Sonos Volume omhoog"					<soundvolume>			(gGV_Keuken, gSonos) 		{ knx="10/1/1" }
Switch KNX_GV_Keuken_VolumeDown_10_1_2							"Sonos Volume omlaag"					<soundvolume>			(gGV_Keuken, gSonos) 		{ knx="10/1/2" }

Player Sonos_GV_Keuken					"Radio keuken"						<soundvolume>		(gGV_Keuken, gSonos) 		{channel="sonos:PLAY5:RINCON_000E58834E6A01400:control"}
Dimmer Sonos_GV_Keuken_Volume			"Radio keuken - volume"				<soundvolume>		(gGV_Keuken, gSonos) 		{channel="sonos:PLAY5:RINCON_000E58834E6A01400:volume"}

As long as the item “KNX_GV_Keuken_VolumeUp_10_1_1” has state “ON”, the volume should keep increasing at a certain pace. I tried doing this via a do/while approach. But that didn’t work.

rule "Increase volume Sonos Keuken"

when   
    Item  KNX_GV_Keuken_VolumeUp_10_1_1 changed from OFF to ON
then
	logInfo("Debug","Sonos Debug1")
    while(KNX_GV_Keuken_VolumeUp_10_1_1.state == ON) {
		Sonos_GV_Keuken_Volume = Sonos_GV_Keuken_Volume+1
        Thread::sleep(1000)
    }
	logInfo("Debug","Sonos Debug2")
end

I’m getting in my debug-log the “Sonos Debug1” value, but never the “Sonos Debug2” value. So I’m guessing I’m doing something wrong there. I couldn’t find many do/while examples, so I’m not sure if I follow the correct approach.

1 Like

I have done something similar to fade out the volume. My solution was to use a variable:

var Number volsleep = Sonos_Bed_Volume.state as DecimalType
while (volsleep > 0) {
volsleep = volsleep - 1
Thread::sleep(1000)
sendCommand(Sonos_Bed_Volume, volsleep)
}

@michaeljoos: thanks! That was the example I needed!

I can now control the volume of my Sonos using my KNX switch. :slight_smile:

rule "Increase volume Sonos Keuken"

when   
    Item  KNX_GV_Keuken_VolumeUp_10_1_1 changed from OFF to ON
then
	var Number v_adjustvolume = Sonos_GV_Keuken_Volume.state as DecimalType
    while(KNX_GV_Keuken_VolumeUp_10_1_1.state == ON) {
		v_adjustvolume = v_adjustvolume + 1
		sendCommand(Sonos_GV_Keuken_Volume, v_adjustvolume)
        Thread::sleep(250)
    }
end


rule "Decrease volume Sonos Keuken"

when   
    Item  KNX_GV_Keuken_VolumeDown_10_1_2 changed from OFF to ON
then
	var Number v_adjustvolume = Sonos_GV_Keuken_Volume.state as DecimalType
    while(KNX_GV_Keuken_VolumeDown_10_1_2.state == ON) {
		v_adjustvolume = v_adjustvolume - 1
		sendCommand(Sonos_GV_Keuken_Volume, v_adjustvolume)
        Thread::sleep(250)
    }
end

1 Like

I was looking into the same but with a KNX dimmer. I already use the ON/OFF channel to play/pause but have not integrated volume control yet.
Any idea how this would be configured?

Hi Bart,
It’s pretty simple (given the rule above).

First, create a GA in ETS and assign it to your KNX switch:
image

Secondly, make sure the value of that GA is “ON” when the button is pressed and “OFF” when released:
image
I have a Gira Tastsensor 3, maybe your switch does not have that option.

Define an item:

Switch KNX_GV_Keuken_VolumeUp_10_1_1 { knx="10/1/1" }

Implement the rule above and you’re done. :slight_smile:

Thanks Dries

I’ll see if my switch can support that, but I doubt I’ll be able to do it with the same sensor-button as play/pause. It’s a Basalte sentido configured as single button switch/dimmer. The dimming actions should produce INCREASE/DECREASE events if I’m not mistaken, which I can use instead of ON/OFF.

I’ll report on my attempt.

Hi Dries,

I copied your rule and replaced the items with my own.

The rule get executed but in stead of increasing by 1 for example, the only thing what happens is that the sonos volume item receives the command 1 and get set to 1…

Any idea what’s wrong?

Sorry, no idea what is wrong.

Could you share your rule?

Just to be sure, if you look at my example:
Item “KNX_GV_Keuken_VolumeUp_10_1_1” can only have two values (ON/OFF or 1/0).
Item “Sonos_GV_Keuken_Volume” is a decimaltype, so can technically have any number as a value.

So please share your rule, and tell us which item has value 1.