Multiple case in switch statement

please read this

Oh, for some reason I missed the part what rlkoshak mentioned
 :face_with_peeking_eye:
So after reading it, I tried this:

        case receivedCommand >= 0  && receivedCommand <= 100 :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/ShutterPosition1", "50")

This doesn’t throw any error, but also the case don’t get triggered. As soon as I change receivedCommand to receivedCommand as Number, it keeps complaining this: “no viable alternative at input”

So since rossko57 mention that the receivedCommand isn’t a numeric variable, I tried this:

rule "Rollershutter back"
when
    Item shutterBack received command
then
val mqttActions = getActions("mqtt","mqtt:broker:mosquitto")
var Number shutterPosistion = receivedCommand as Number
logInfo("Rollershutter rule", "First part, value of shutterPosistion is = " + shutterPosistion.toString)
    switch (receivedCommand) {
        case UP :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutteropen")
        case STOP :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutterstop")
        case DOWN :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutterclose")
        case shutterPosistion >= 0  && shutterPosistion <= 100 :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/ShutterPosition1", "50")
    }
logInfo("Rollershutter rule", "Second part, value of shutterPosistion is = " + shutterPosistion.toString)
end

But again, the case don’t to seemed triggered by the value, also tried including the loginfo inside the switch{} part but that seem te be possible either, who can help me out?

I’m not an expert in Rules DSL and I haven’t used it in a while, but try this:

rule "Rollershutter back"
when
    Item shutterBack received command
then
val mqttActions = getActions("mqtt","mqtt:broker:mosquitto")
logInfo("Rollershutter rule", "First part, value of shutterPosistion is = " + shutterPosistion.toString)
    switch (receivedCommand) {
        case UP :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutteropen")
        case STOP :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutterstop")
        case DOWN :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutterclose")
        default : {
            var shutterPosistion = (receivedCommand as Number)
            if (shutterPosistion >= 0  && shutterPosistion <= 100) {
              mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/ShutterPosition1", "50")
            }
        }
    }
logInfo("Rollershutter rule", "Second part, value of shutterPosistion is = " + shutterPosistion.toString)
end

This isn’t going to work if receivedCommand is for example STOP.
See JimT suggestion to only attempt number if you already know it is not UP/DOWN/STOP

You might like to think about the logic a bit more though 
 if it is a command that is not UP/DOWN/STOP, what else could it be apart from a number 0-100?

Thanks, that did the trick, I now ended up with this rule and it works like I wanted. If controlled by the rollershutter buttons from OH, it does what it suppose to de, when controlled by Apple HomeKit, it also does what you command it to do:

rule "Rollershutter back"
when
    Item shutterBack received command
then
val mqttActions = getActions("mqtt","mqtt:broker:mosquitto")
    switch (receivedCommand) {
        case UP :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutteropen")
        case STOP :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutterstop")
        case DOWN :
            mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/Backlog", "shutterclose")
        default : {
            var shutterPosistion = (receivedCommand as Number)
            if (shutterPosistion >= 0  && shutterPosistion <= 100) {
              mqttActions.publishMQTT("cmnd/Shelly25RollerShutter_Back/ShutterPosition1", shutterPosistion.toString)
            }
        }
    }
end

Maybe if somebody is looking for something similar, this will help.

Yea I checked the openHAB Log Viewer when I used the Apple HomeKit item to see what command it will send when you do different things with it. It will always send a number between 0-100, so for now, this rule will work like it suppose to do :slight_smile:

Yes, my point was that the following code is therefore redundant

It’s always true, you don’t ever get to this test if it were UP/DOWN/STOP