# Help Me Simplify This Rule

I've created a rule in Rule Machine to set the mini split in my bedroom based on three hub variables, 1) bedroom away true/false, 2) hub mode and 3) desired heat/cool mode (I have a three zone unit, so I have to globally manage that variable so that they all operate in the same mode, since mixed mode scenarios default to heat.)

I'm hoping someone with more experience with Rule Machine can help me structure this rule in a way that simplifies it. The biggest hurdle I have is in my limited understanding of how "else" works. I'd love to be able to be able to make the three variables top level conditions instead of setting up each condition at every level of the rule. I hope that makes sense. Thanks in advance for sharing your wisdom!

To get `ELSE-IF` rather than an `ELSE` with a nested `IF THEN`, you'll need to choose `ELSE-IF` from the actions menu, similar to how you inserted the `IF THEN` in the first place. (This will only appear when you're inside an `IF THEN`, hopefully even when you're editing something after already finishing something else....if not, I suppose you could now add it wherever you can and move it if needed.)

That will take care of one of your questions and make the rule easier to read, at least. I didn't look at the rest much for they reason. But if you have questions about the rule logic, I'm sure someone could take a look at that too!

Thanks! Can you help me understand the difference between "ELSE" and "ELSE-IF"? Does one respect the IF condition above and one starts a new condition?

`ELSE` matches anything not covered by your `IF THEN` or any previous `ELSE-IF`s. If you have this pattern:

``````IF (x) THEN
// do one thing
ELSE
// do another thing
END-IF
``````

The exacty one of those two commented-out sections will execute: the first if `x` is true, otherwise the second.

If you have this:

``````IF (x) THEN
// do one thing
ELSE-IF (y) THEN
// do something else
ELSE
// do another thing
END-IF
``````

Then still exactly one of those will execute, whichever matches first: the first if `x` is true, the second if `y` is true (and `x` was not), or the third if nothing else matches.

You have as many `ELSE-IF`s as you need, and you can leave off the `ELSE` if you don't need something that catches anything that wasn't matched with a previous check.

You have something like this:

``````IF (x) THEN
// do one thing
ELSE
IF (y) THEN
// do another thing
END-IF
END-IF
``````

This kind of thing can be constructed to have a similar outcome as the last situation I described above (or any of the others, with some changes), but it's a bit messier than it needs to be when that second `ELSE` could have just been an `ELSE-IF` — saving some nesting, making it easier to read, etc.

At the very least, you'll see they are quite different in that `ELSE-IF` requires an expression to evaluate.

1 Like

In the case of IF followed by ELSE:
If the IF test is false, then one or more statements under the ELSE will ALWAYS be executed (until an ENDIF is encountered).

In the ELSE-IF case, if the 1st IF proves to be false, then the next ELSE-IF is tested with the knowledge that the last one wasn't executed.

This is helpful. I'm currently stuck trying to figure out how to set up the two overarching conditions for "away=true" and "away=false". I've attached the simplified test rule I created to show what I'm trying to accomplish, along with the logs showing that the rule is skipping everything after the first false IF statement.

1 Like

Yes, that `ELSE-IF` is embedded in your first `IF THEN`. And because they test for opposite conditions, they'll never both me true at the same time (so when your outer one is true, that inner one will never be--hence it's skipped).

If you aren't sure what I mean with embedding or nesting, what's next may help.

One thing I see missing from your screenshot is any `END-IF`s. You need these to "close out" each block. Besides the fact that these are all missing, you can see the indentation in the list that hints at this embedding. See the example I gave above for how `END-IF` works or look at the docs or videos ("?" icon or video icon in the uppercase right of Rule Machine -- and many apps) to see more examples.

1 Like

I think I understand what you've shared. I tried adding an END-IF but I am not sure where to add it in order to accomplish my goal. Maybe I'm going about the whole thing wrong. Here's a diagram of what I'm hoping to accomplish. How I've done it before is with tons of full statements with every condition, like in my original screenshot. That works, but is super tedious and results in a TON of lines. Is there a better way to structure it than that?

Something like the you diagrammed should work if you "close out" your `IF THEN`s with `END-IF`s where needed, as described above. That will mostly help with appearance/indentation; you'll still need almsot the same number of lines (actually one less per `ELSE-IF` if you do it that way instead, but probably nothing significant).

In general, it is often recommended that smaller, simpler rules are better. In your case, creating two different rules triggered on whatever that use a required expression (testing the hub variable for the desired value; triggering will be prevented when the expression is false) might be a way to make things easier to see and edit.

Alternatively, I'm not sure what all this rule is doing for you or if this would work for you, but there are apps like Thermosat Scheduler that can help you automate the setpoint of a thermostat without writing a rule where you have to implement all this logic yourself. If you haven't at least considered this possibility first, I'd check it out!

2 Likes

Another option might be to use a State Machine. I have read about them, but haven't tried it yet, but it might simplify the logic.

1 Like

Unfortunately, Thermostat Scheduler and Thermostat Controller are both designed for single stage furnaces and therefore have some features that don't work right with the mini split. So, I'm basically trying to recreate Thermostat Scheduler and Thermostat Controller with my own rules.

I think I've made some progress with this latest round. I tested it and it behaves how I want. Here's a screenshot.

1 Like

Thanks for the help! I got it working how I want it to. And now I understand a lot more about END-IF and ELSE-IF!

1 Like

Glad you got it working! In case you haven't noticed since your last screenshot, you're technically missing four or so `END-IF`s at the end of the rule in that screenshot -- "closing" these blocks in the same way that you're doing in the middle of the rule actions, too. The difference here is that since you have no actions afterwards, RM is smart enough to infer what you mean instead of throwing an error, so it doesn't affect the outcome, but it's still a good habit. That way, you don't forget it when you do need one, like you discovered above.

3 Likes

Your ELSE followed by "IF (Variable Daniels Room Away = false) THEN" is redundant. Your first IF is "= true" so the ELSE would cover when = false. Checking Variable Daniels Room again isn't necessary.

1 Like

Yes there are still some improvements you need to make.

For example as @pseudonym mentioned the 2nd Daniels room away test is not required as it can only be true or false so if the else runs from above it must already be as you want.

2nd the 2nd else should actually be a else IF of bedroom mode desired = cool.

Then add the last END IF to close it and it will read correctly and flow better.

This is super helpful. I feel like I'm learning great stuff through this and I appreciate all of you giving feedback. Here's my latest iteration. I think I was able to implement the things you guys noted, but please let me know what I'm still missing. Thank you all so much!

Side Note: It's set to repeat because the mini split can be finicky and doesn't always receive the commands the first time. Having the commands sent a second time seems to get it to 99% or so reliability.

You're missing an END-IF. You can see that the END-REP doesn't line up with Repeat. Not lining up is a clue that you're missing something. There is also and ELSE or END-IF option next to the Create New Action, which is also a clue that something is missing. Add an END-IF before the END-REP and I think you'll be good.

2 Likes

Yes that's all you need now. Click the end IF at the bottom then use the up arrow to the right to move it up one space.

So the last 3 actions should be

``````      END-IF
END-IF
``````

END-REP

I just added the last END-IF. How's it looking now?

I counted 5 IF's but it looks like you only have 4 END-IF's but hard to tell since you didn't post the whole rule. I still think you're missing one.

1 Like