Need help to create rule for shower fan

Hi,
I need help with creating a rule for my bathroom ventilation.
I have fan controlled by Shelly 1. Shelly is controlled by Xiaomi Wireless Wall Switch.

I have a rule that turns on the fan when humidity is over 75% (Xiaomi Himidity Sensor). And turn off when is below 75%.

But when I go to the bathroom and turn on the fan with a switch by myself (eg. going to poop), it turns off if when Humidity sensor recognizes it’s below 75%. I don’t want that.

Fan should automatically turn off only when was enabled by a rule of after 25 minutes. Could you help me?

This is what I have:

Items:

Number:Dimensionless    HTBathroom_Humidity         "Bathroom Humidity"                             <humidity>          (gTempBath)                     {channel="mihome:sensor_ht:158d0001149ad4:humidity", ga="thermostatHumidityAmbient"}

Switch              ShellyBathFan           "Bathroom Fan"                                                  {channel="shelly:shelly25-relay:68c63afa35a5:relay1#output", ga="Fan"} 

Rules:

rule "Hallway 2WallSwitch B.1"

when

    Channel "mihome:86sw2:158d0002afc109:ch1" triggered

then

    var actionName = receivedEvent.getEvent()

    switch(actionName) {

        case "SHORT_PRESSED": {

            if (ShellyBathFan.state == OFF )  {  ShellyBathFan.sendCommand(ON) }

                else  ShellyBathFan.sendCommand(OFF)     

        }

        case "LONG_PRESSED": {

        }

        case "DOUBLE_PRESSED": {

        }

    }

end




rule "Turn Bathroom Fan while shower"

when

    Item HTBathroom_Humidity changed

then

    if(HTBathroom_Humidity.state >=75 | "%" && ShellyBathFan.state == OFF) 

         {  ShellyBathFan.sendCommand(ON)   }

    if(HTBathroom_Humidity.state <70 | "%") 

         {  ShellyBathFan.sendCommand(OFF)  }

end

Thanks for your help

Add a condition to see if the fan was turned on due to humidity or manually. If it’s the first case, turn off when the sensor goes below 75%. If it’s the second case, turn off after 25 minutes.

One way to do this is to use an unbound proxy item to hold the check condition. Turn it on when the switch is pressed, so that your rule can check its state. If the state is ON, turn off after 25 minutes. If the state is OFF, then the fan turned on due to humidity should only turn off until humidity falls below 75%.

There are probably other (better) ways to accomplish this, but I like using proxy items. They’re easy for me to wrap my head around. :wink:

1 Like
var Timer tBathFan = null   									// create a global Timer var

rule "Hallway 2WallSwitch B.1"
when
    Channel "mihome:86sw2:158d0002afc109:ch1" triggered     	// wall switch triggered rule
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "SHORT_PRESSED": {                             	// short press, so toggle fan
			tBathFan?.cancel                                	// cancel an existing timer
            if (ShellyBathFan.state != ON ) {               	// if not ON
				ShellyBathFan.sendCommand(ON)               	// switch ON
				tBathFan = createTimer(now.plusMinutes(25),[	// and start auto off timer
					ShellyBathFan.sendCommand(OFF)              // when timer expires, send OFF command
					tBathFan = null                             // and deinitialize the timer var
				])  											// end of timer
			} else {											// if ON
				tBathFan = null 								// deinitialize timer var
				ShellyBathFan.sendCommand(OFF)  				// and switch OFF
			}
        }
        case "LONG_PRESSED": {
        }
        case "DOUBLE_PRESSED": {
        }
    }
end


rule "Turn Bathroom Fan while shower"
when
    Item HTBathroom_Humidity changed    														// humidity changed
then
    if(HTBathroom_Humidity.state >=75 | "%" && ShellyBathFan.state != ON)   					// if over 75% AND notON
        ShellyBathFan.sendCommand(ON)   														// switch ON
    if(HTBathroom_Humidity.state <70 | "%" && ShellyBathFan.state != OFF &&  tBathFan === null) // if under 70% AND not OFF AND no timer running
        ShellyBathFan.sendCommand(OFF)  														// switch OFF
end

simple example…

2 Likes