Switch on recievedCommand falling to default case

The rule below is falling through to the default case on all occasions.

can someone advise?

Thanks

Paul

rule "Automated Humidity Control"
when
Item BR1_Humid_auto received command or
Item BR2_Humid_auto received command or
Item BR3_Humid_auto received command or
Item BR4_Humid_auto received command or
Item LR_Humid_auto received command or
Item GR_Humid_auto received command or
Item HO_Humid_auto received command
then
	logInfo("auto_humid.rules", triggeringItem.name + " received a command:" +  receivedCommand)
	switch receivedCommand {
		case "ON" :	{logInfo("autohumid.rules", triggeringItem.name + " humidity contorl has been enabled")}
		case "OFF" : {logInfo("autohumid.rules", triggeringItem.name + " humidity control has been disabled")}		
		default : {logInfo("autohumid.rules", "Unexpectedly reached default case")}	
		}
end

I think this will be a state like ON , not a string “ON”

Thanks.

I had tried is simply with using ON and OFF without quotes but the said something about not a boolean.

Now I have it working correctly using the following.

rule "Automated Humidity Control"
when
Item BR1_Humid_auto received command or
Item BR2_Humid_auto received command or
Item BR3_Humid_auto received command or
Item BR4_Humid_auto received command or
Item LR_Humid_auto received command or
Item GR_Humid_auto received command or
Item HO_Humid_auto received command
then
	logDebug("auto_humid.rules", triggeringItem.name + " received a command: " +  receivedCommand)
	switch receivedCommand {
		case receivedCommand == ON :	{logInfo("autohumid.rules", triggeringItem.name + " humidity contorl has been enabled")}
		case receivedCommand == OFF : {logInfo("autohumid.rules", triggeringItem.name + " humidity control has been disabled")}		
		default : {logError("autohumid.rules", "Unexpectedly reached default case")}	
		}
	//if (receivedCommand == ON) logInfo("autohumid.rules", triggeringItem.name + " humidity contorl has been enabled")
	//if (receivedCommand == OFF) logInfo("autohumid.rules", triggeringItem.name + " humidity control has been disabled")
end

Thanks

Paul

Either use

switch receivedCommand.toString

or

case ON: ... 
case OFF: ... 

This cannot work as the result is a boolean (true or false)

case receivedCommand == ON: ... 
case receivedCommand == OFF: ... 
  • The type of items ? ( Switch or String ? )
  • Try with only one item in rule to debug

Actually, this could work in Rules DSL. Switch statements behave a little differently in Xtend then what we are used to. Unlike other languages where the Switch statement really is its own thing, here it is just a shorthand way to write out a bunch of if/else statements. If you have a case that returns a boolean then it should execute that case.

See this from the Time of Day DP:

  switch now {
        case now.isAfter(morning_start) && now.isBefore(day_start):       curr = "MORNING"
        case now.isAfter(day_start) && now.isBefore(afternoon_start):     curr = "DAY"
        case now.isAfter(afternoon_start) && now.isBefore(evening_start): curr = "AFTERNOON"
        case now.isAfter(evening_start) && now.isBefore(night_start):     curr = "EVENING"
        case now.isAfter(night_start):                                    curr = "NIGHT"
        case now.isAfter(bed_start) && now.isBefore(morning_start):       curr = "BED"
  }

And here is the docs: Xtend - Expressions.

So the case receivedCommand == ON, while redundant, should have worked, assuming that the Items are Switch Items. However, I’ve never used a Switch statement with the Enum types (i.e. ON/OFF, OPEN/CLOSED, etc) so there may be something incompatible with the OH Enum types and the Xtend switch statement.

@idkpmiller, you can normalize and eliminate all the possible problems with types by using:

switch receivedCommand.toString {
    case "ON": ...
}

Convert the command to a String and use a String for all the cases and so long as it is spelled right it will work.

1 Like

Thanks, i didn’t expect that.