Switch / case rule in Blockly?

Hi,

I am currently trying to move all my rules from .rules files into rules which make use of Blockly etc. inside the main ui.

Is it possible in Blockly to recreate a switch / case rule?

rule "LivingRoom - Button"
when
    Item LivingRoomButton_Click received update
then
    switch(LivingRoomButton_Click.state.toString) {
        case "single": {
            logInfo("broker trigger debug", "triggered single click")
	        if(LivingRoom_1_LEVEL.state >= 1) {
            	LivingRoom_1_LEVEL.sendCommand(OFF)
	        }
	        else if (LivingRoom_1_LEVEL.state <= 0) {
	            LivingRoom_1_LEVEL.sendCommand(ON)
            }            
        }
        case "double": {
            logInfo("broker trigger debug", "triggered double click")
	        if(LivingRoomAmbilight.state >= 1) {
            	LivingRoomAmbilight.sendCommand(OFF)
	        }
	        else if (LivingRoomAmbilight.state <= 0) {
	            LivingRoomAmbilight.sendCommand(ON)
            }             
        }
        case "triple": {
            logInfo("broker trigger debug", "triggered triple click")
        }
        case "quadruple": {
            logInfo("broker trigger debug", "triggered quadruple click")
        }
        case "many": {
            logInfo("broker trigger debug", "triggered many click")
        }
        case "long": {
            logInfo("broker trigger debug", "triggered long click")
            TestDummy.sendCommand(3)
        }
        case "long_release": {
            logInfo("broker trigger debug", "triggered long_release click")
        }                
    }
end

Maybe use “if/else-if” blocks? Similar, switch is just syntactic sugar.

1 Like

Hello together,
is there another solution for that without if else statements?

Why would you need one? What would you gain over using if / if-else blocks?

Why we have switch in Javascript and why we not use else if? Same question…
If you have so many if boxes you get lost. There are so many blocks eg text or arrays you can put the needed data together. There will maybe a same way for cases and switch.

Sorry to say that, but switch on some occasions is just more elegant, and if you don’t do the typical break after each statement, you can do very nasty things…

But as it seems there is no switch block, you could just get it in with the script block… especially as u already have the code…

You may could even get it in with normal blocks in between… There is a way.