Ventilation based on hood consumption

Got OpenHAB running on Ubuntu for a while now with KNX (basic stuff working) and I decided to dare go on the adventure of combining different systems.

First success was getting the Sonos and the Harmony to turn off when I press my central “all off” button when leaving home :thumbsup:
Second success came when I was able to start the Apple TV activity when pressing a button on my KNX-system which sends a KNX scene number on the bus. :smile: OpenHAB sees the value, with a simple if number=20 it does the right actions.

For the more advanced part I thought it would be nice to control my ventilation system based on different use cases in my home. One of them is when we’re cooking and the hood/damper is switched on to measure the power usage and use this to determine the percentage for the ventilation unit but I am hitting some bumps.

Mind you, beginner here with rules and the IDE and the IDE is not the most helpful sometimes…

My items for this one:

Number number_Amps_Dampkap "Dampkap [%.0f mA]" <kitchen> (grp_Amps) { knx="7.012:2/6/16" }

My rule

rule "VentilationBasedOnHood"
	when
		Item number_Amps_Dampkap received update
	then
		
		switch {
            case number_Amps_Dampkap.state >= 50:
            	// do stuff
            	break;
            	
			case number_Amps_Dampkap.state >= 200:
				// do stuff
            	break;
            case number_Amps_Dampkap.state >= 400:
				// do stuff
            	break;
            
            default: break;
        }
	end

I am in the assumption that to get a value I always have to use .state. However the IDE warns me about:

Hope somebody can clear this one up.

First of all, in the Rules DSL switch statements do not work the same as they do in Java, C, et al.

Do not use break. That will exit your entire rule. (On a similar note, a return in a lambda will return your entire rule, not just the lambda). You don’t need to because the cases do not fall through.

You need to provide something upon which you will be making comparisons after the switch, even if you are defining the conditions rather than just testing for equality.

I believe it will only take the first match. If not you may need to add an && to the second and third cases.

See: https://eclipse.org/xtend/documentation/203_xtend_expressions.html#switch-expression

A properly formatted version of your switch statement would be:

switch number_Amps_Dampkap.state {
  case number_Amps_Dampkap.state >= 50 : {
    // do stuff
  }
  case number_Amps_Dampkap.state >= 200 : {
    // do stuff
  }
  case number_Amps_Dampkap.state >= 400 : {
    // do stuff
  }
  // since your default doesn't do anything you don't need to make one.
}

If your //do stuff is a single line you don’t need the curly brackets.

If you want a fall through behavior you have multiple case statements separated by a comma.

From the link above.

def isMale(String salutation) {
  switch salutation {
    case "Mr.", 
    case "Sir" : true
    default : false
  }
}
1 Like