3-state item and its rules ... help wanted

Hello Folks

I ask you for advice in this following case:
My new home is equiped with controlled air exchange (kontrollierte wohnraumlüftung in german). Each room can have 3 States: - Off - Level 1 - Level 2. But only one at the same time.
For this, I’ve built 2 Proxy Switches for each Level and its physical device:

Switch	EG_Wohnen_SWI_WRL_1_Proxy			"Lüftung Stufe 1"					<fan>					(gSwitches, gEG_WRL, gEG_Wohnen)
Switch	EG_Wohnen_SWI_WRL_2_Proxy			"Lüftung Stufe 2"					<fan>					(gSwitches, gEG_WRL, gEG_Wohnen)

Switch			EG_Wohnen_SWI_WRL_1			"Wohnraumlüftung Stufe 1"			<fan>			(gEG_Wohnen, gEG_WRL)
Switch			EG_Wohnen_SWI_WRL_2			"Wohnraumlüftung Stufe 2"			<fan>			(gEG_Wohnen, gEG_WRL)

And some rules, which checks the state:

rule "WRL EG_Wohnzimmer Proxies changed"
  
  when
  	Item EG_Wohnen_SWI_WRL_1_Proxy changed or
  	Item EG_Wohnen_SWI_WRL_2_Proxy changed
  
  then
  	Thread::sleep(100)
  	if (EG_Wohnen_SWI_WRL_1_Proxy.state == ON) {
  		// Prüfen, ob die 2te Stufe wirklich aus ist, ansonsten wird sie ausgeschalten
  		if (EG_Wohnen_SWI_WRL_2.state != OFF) {
  			EG_Wohnen_SWI_WRL_2.sendCommand(OFF)
  			if (EG_Wohnen_SWI_WRL_2_Proxy.state != OFF) EG_Wohnen_SWI_WRL_2_Proxy.postUpdate(OFF)
  		}
  	Thread::sleep(200)
  		// Stufe 1 nach einer kurzen Pause einschalten
  		EG_Wohnen_SWI_WRL_1.sendCommand(ON)
  		logInfo("SmaWo WRL", "WRL Wohnen laeuft auf Stufe 1")
  	}

  	else if (EG_Wohnen_SWI_WRL_1_Proxy.state == OFF) {
  			// OFF kann direkt durchgestellt werden
  			EG_Wohnen_SWI_WRL_1.sendCommand(OFF)
  		}  	
  	  	
  	else if (EG_Wohnen_SWI_WRL_2_Proxy.state == ON) {
  		// Prüfen, ob die 1te Stufe wirklich aus ist, ansonsten wird sie ausgeschalten
  		if (EG_Wohnen_SWI_WRL_1.state != OFF) {
  			EG_Wohnen_SWI_WRL_1.sendCommand(OFF)
  			if (EG_Wohnen_SWI_WRL_1_Proxy.state != OFF) EG_Wohnen_SWI_WRL_1_Proxy.postUpdate(OFF)
  		}
  		Thread::sleep(200)
  		EG_Wohnen_SWI_WRL_2.sendCommand(ON)
  		logInfo("SmaWo WRL", "WRL Wohnen laeuft auf Stufe 2")
  	}
  	
  	else if (EG_Wohnen_SWI_WRL_2_Proxy.state == OFF) {
  		EG_Wohnen_SWI_WRL_2.sendCommand(OFF)
  	}
  end
  

But its still possible to get Level 1 and Level 2 at the same time to be ON.
Where is my fault?
Do you have some corrections for it?

Thank you

Michael

Why not use a Number item and sitemap entry such as
Switch item=KWL icon="climate" label="Stufe" mappings=[1="Stufe 1", 2="Stufe 2", 0="Aus"]

Hello Markus

Your idea sounds well.
How I have to design the rule for it?
I have the WRL attached to a fibaro double relay switch (Switch 1 and switch 2).

OFF = Switch 1 off, Switch 2 off
Level 1 = Switch 1 ON, Switch 2 off
Level 2 = Switch 1 off, Switch 2 ON

Thank you,
Michael

Come on, that’s basic programming…
(beware, untested…)

rule "3state"
when
    Item My3stateSwitch changed
then
    switch (My3stateSwitch.state as DecimalType) {
        case 0 :  { KWLIn1.sendCommand(OFF)
                         KWLIn2.sendCommand(OFF) }
        case 1 :  { KWLIn1.sendCommand(ON)
                         KWLIn2.sendCommand(OFF) }
        case 2 :  { KWLIn1.sendCommand(OFF)
                         KWLIn2.sendCommand(ON) }
    }
end

Hello Markus

Thank you, that works like a charme…

regards
Michael