Help with rule with 2 conditions to execute

Hello,

I looked on he forum, this sould have workt but it doesnt ,also tried “or” instead of “||”
An idea?

rule “SonosMB_on”
when
Item SonosConnectMasterBedroom_State changed to PLAYING
then
BEDROOMAVReceiverHC_Zone1_Power.sendCommand(ON)
BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand(“01”)
BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-10)
end

rule “SonosMB_off”
when
Item SonosConnectMasterBedroom_State changed to STOPPED || PAUSED_PLAYBACK
then
BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand(“05”)
BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-40)
end

This rule of mine has 2 triggers

rule "Basement Light On"
when
    Item basement_motion_1 changed from OFF to ON or Item basement_motion_2 changed from OFF to ON
then
    basement_light.sendCommand(ON)
    // start or reset Timer
    Ba

Try the following

rule “SonosMB_off”
when
Item SonosConnectMasterBedroom_State changed to STOPPED OR
Item SonosConnectMasterBedroom_State changed to PAUSED_PLAYBACK
then
BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand(“05”)
BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-40)
end

There is no need for two rules:

rule "SonosMB"
when
    Item SonosConnectMasterBedroom_State changed
then
    switch (SonosConnectMasterBedroom_State.state.toString) {
        case "PLAYING" : {
            BEDROOMAVReceiverHC_Zone1_Power.sendCommand(ON)
            BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand("01")
            BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-10)
        }
        case SonosConnectMasterBedroom_State.state.toString == "STOPPED" || 
             SonosConnectMasterBedroom_State.state.toString == "PAUSED_PLAYBACK" : { 
            BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand("05")
            BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-40)
        }
    }
end
2 Likes

Thanks,

This looks very neat and profesional!
I’ll it this evenig.

I let you know!

It doesn’t work
2019-10-22 20:52:14.726 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘SonosMB3.rules’ has errors, therefore ignoring it: [12,82]: no viable alternative at input ‘{’
[16,5]: extraneous input ‘}’ expecting ‘end’

In the second case at the end “:” is missing

Yep, missing colon in line 12 right before the opening curly bracket. I changed the code above…

It would be a little cleaner to use…

    switch (SonosConnectMasterBedroom_State.state.toString) {
        case "PLAYING" : {
            BEDROOMAVReceiverHC_Zone1_Power.sendCommand(ON)
            BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand("01")
            BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-10)
        }
        case "STOPPED", case "PAUSED_PLAYBACK" : { 
            BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand("05")
            BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-40)
        }
    }
2 Likes

that did the trick, i didn’t notice that!
Thank you!
Could it be the this rule is slower that my original rules?

I have yet to compare, but at first sight it seems the receiver reacts slower…

Unlikely. Because of the way the interpreter works, the very first run of a rule after loading/editing takes a little longer.

If you have concerns about the rule, add a timer

rule "SonosMB"
when
    Item SonosConnectMasterBedroom_State changed
then
    val long mstart = now.millis
    switch (SonosConnectMasterBedroom_State.state.toString) {
        case "PLAYING" : {
            BEDROOMAVReceiverHC_Zone1_Power.sendCommand(ON)
            BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand("01")
            BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-10)
        }
        case "STOPPED", case "PAUSED_PLAYBACK" : { 
            BEDROOMAVReceiverHC_Zone1_InputSource.sendCommand("05")
            BEDROOMAVReceiverHC_Zone1_Volume.sendCommand(-40)
        }
    }
    val long mstop = now.millis
    logInfo("SonosMB", "The Rule took " + (mstop - mstart) + " ms to run")
end

Probably more revealing is keeping an eye on your events.log - when did OH see the trigger condition, when are the commands sent?

Looks clean and it works

1 Like

You’re right it seems it as onl the first time.
Thanks

Ah, cool, didn’t know that this is actually working.

1 Like