# Help with what i consider a complex rule

Ive now done this which i think gets 2 of my conditions sorted? am i going in the right direction?

EDIT, this isnt switching off the fan and im not certain why? values are both true

Tried this, but for some reason the fan still wont switch off.

edit, this seems to be working now.

Ok Ive just done this but no time to test it. Will it work or will i have problems?

For several reasons, I use two rules. This one for the fan, is quite similar to yours.

And this one for the lighting.

Nyce ceiling mounted sensor provides motion, temperature, and humidity. In the hall outside the master bedroom I have a temperature and humidity sensor that I use as a comparison source. Switches and dimmers are Lutron on a Caseta bridge that is on the Hub called C41 (a C-4 hub). One kind of unique thing with the lighting - there are three separate lights - the wall mounted lights over the mirror at the vanity and two lights built into the ceiling fan - a general light and a night light (5 watts or so). I use all three lights in a mode scenario with different lights and different time-out values by mode.

My Rule is "sort of" working but not doing what i want it to accurately, one of the main issues is i dont understand how/in what order actions are run or conditions read, basically im confused by the If-Then, Else-if and Else statements and how they work together.

Any idea on where i can find a tutorial on it?

The "If then else" is a staple of most high level languages, so a search on google will find you some things you can read or watch.

A quick search and I found this short youtube video that explains it pretty well.

1 Like

Thanks ill give it a look, didnt know if they behaved differently depending on the language.

In rule machine, is it possible to start a timer when a condition is met, which can then trigger different actions depending on when a new condition is met?

IE:
lights come on.
start fan
start timer
wait for event, Lights off.
If lights off > 120seconds after lights on Then
delay 5 mins
Else If < = 120 seconds after lights on Then
Fan off.

I think with my new understanding of if statements the following is as good as it gets? (if it works)

Remember the rule gets triggered every time the humidity changes or the light switch changes. Waits get canceled when a rule is re-triggered.

Also you might consider adding your end-ifs

That explains some weird behaviour! Thanks. Ive reduced the triggers by 50%... and added the end-if now im off to find out what it actually does....

is this possible? i cant see that it is. but cant understand why the feature isnt there, is there another way of achieving this?

You might be able to store a value in a global variable and check it on a subsequent rule that fires on the other condition if that makes any sense. Also you could create virtual device(s) that hold the "states" of your conditions as needed. The only tricky part is making sure everything is cleared properly.

https://docs.hubitat.com/index.php?title=Rule-4.0#Global_Variables

Here is an older post that describes things in more details (but some things may have changed a bit!).

1 Like

You can do this, just use variables to start a timer and stop a timer. A number variable can be set to current minute.

An example I have not tested but I think would do what you asked. Look if you want.

Summary

Thats brilliant thanks, im going to start googling to see what this rule is actually doing so i understand how its working before i do it.

Just one thing to note. a very odd behaviour in my rule and i cant see why its doing it, (i mean my version, not the one you have written) if i put end-if at the end then after a while the fan wont come on with the light, if i delete it, it starts working again.

Just to put another spin on this.
I have separated my Bathroom Lights and Bathroom Fan into 2 rules.
Rule #1 just controls the lights and ignores the fan.
Rule #2 controls the fan.
When the fan comes on, it pauses the light rule.
When the fan turns off it resumes the light rule, waits 5 seconds and then runs the actions in the light rule, which in turn will turn it off IF there is no motion. (Not the sitting down type motion. ).
I have a separate rule which evaluates the humidity level and either turns a global boolean variable true or false.
This stops the light/fan rule from continually triggering when humidity level changes. Only the variable rule will trigger on humidity changes.
Just another option for you to consider.
Lots of ways to skin that cat.

EDIT: My shower motor is plugged into a power monitoring outlet in the attic. This way the fan will turn on as soon as the power of the outlet goes above 50 which means the shower motor is running.
So the rule will trigger on motor running but will only turn false if the motor is not running AND humidity variable is false.

1 Like

In your ELSE-IF expression, you can remove the first term
“Variable bathsalusbelow50 = true” (and the following AND) because, if the ELSE-IF is executed, that term is necessarily True because it couldn’t be False if the IF was not executed.

1 Like

I have 2 ruels, one controls lights this one is for the fan only.

If i did that then if the fan was on due to high humidity, the lights wouldnt work!

not sure how this is better than the way that ive done it, in fact it sounds far more complicated.

min only triggers if the humidity rises.

very true.

my shower is mains water pressure fed not electric.

still think my way is simpler sill not sure why it stops running with the end-if statement at the bottom, trying it without for now to see if it sops running without the end-if

My fan rule turns the light on, hence pausing the light rule and the light turning off mid-shower because motion may not be detected and the lights may turn off.

I wasn't trying to imply that my way was better. It just stops the rule being continually evaluated as the humidity goes up and down.

I only posted my rule to give you ideas of alternative ways of doing things. Oh well...........

Hi i wasnt trying to say anything negative, i was asking you if id missed anything that would make my rule better, sorry for the confusion.

1 Like

Ive now been running the rule successfully for a few days without the End-if statement at the end of the rule.

If i add the End-if back in the rule runs fine for a day or so and then stops, does anyone know why this might be?

Download the Hubitat app