Humidity Rule vs Manual Switch

Hello, All

First of all thank you for OpenHab application

I have a very simple rule which run the ventilation in Bathroom based on Humidity (Xiaomi sensor via Zigbee2mqtt usb stick)

rule "Bathroom Ventilation Humidity"
    Item Bathroom_Humidity changed
    var bathhumidity = (Bathroom_Humidity.state as Number)
    logInfo("Bathroom_Humidity: ", bathhumidity.toString)
    if (bathhumidity > 70) {
        if (sw1_26.state == OFF) {
            sendBroadcastNotification("Ventilation is ON")
    if (bathhumidity < 50) {
        if (sw1_26.state == ON) {
            sendBroadcastNotification("Ventilation is OFF")

It is working fine
But now i have a trouble - if i switch on ventilation manually (I have switch on the wall), ventilation will be switched off by this rule once new data came from sensor (if Humidity < 50).
How can I update my rule that if Ventilation is switched on via Wall Switch - do not switch it off based on Humidity?

Thank you in advance

You would need a way to detect “who” turned it on.

That’s easier than it sounds - when it was your rule that turned it on, there is a little block of code that runs only then. (fan-on and message)
If you create a global variable (one that survives between rule runs) or use a dummy Item, you can use that as a “flag” and set it active in that code. “It was me!”, effectively.

When your rule comes to turn the fan off, it would first look at that flag - was it me? - before taking action. When it does turn off the fan, it should clear the flag for next time.

For the sake of tidiness, you might also deal with the case where the fan has been turned on automatically but someone comes along and turns it off by hand. You’d probably want to clear the flag in that case.

1 Like

rossko57’s approach is further described with examples in Design Pattern: Expire Binding Based Timers Approach 1.