Java Switch Case in openhab rules

An important thing to realize is that even though Java classes are available in the rules, the Rules and Scripts are written in the Xtend language, not Java. Switch statements in Xtend work significantly differently than they do in Java.

Review the page linked to by @thorsten_gilfert.

Also, I think the symbol for AND should actually be &&.

That being said, something like this might work:

switch Pump01 {
    case Pump01.state==ON && Pump02.state==ON && Relay02.state==OFF: sendCommand(Relay02, ON)
    case Pump01.state==OFF && Pump02.state==ON && Relay02.state==OFF: sendCommand(Relay02, ON)
    case Pump01.state==ON && Pump02.state==OFF && Relay02.state==OFF: sendCommand(Relay02, ON)
    case Pump01.state==OFF && Pump02.state==OFF && RElay02.state==OFF: sendCommand(Relay02, OFF) 
}

There is no need to have cases that do nothing in the statement. I’m not sure if Xtend will even let you do that actually. One big thing to note about Xtend switch statements is that they do not fall through so there is no need for the breaks.

That being said, I think you can collapse this into a single if/else statement:

if(Relay02.state == OFF && (Pump01.state==ON || Pump02.state==ON)) sendCommand(Relay02, ON)
else if(Relay02.state == ON && Pump01.state == OFF && Pump02.state==ON) sendCommand(Relay02, OFF)

Rich

1 Like