Help Me Simplify This Rule

ELSE matches anything not covered by your IF THEN or any previous ELSE-IFs. 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-IFs 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. :slight_smile:

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-IFs. 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 THENs with END-IFs 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. :sweat_smile:

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-IFs 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. :smiley:

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

Yes @pseudonym is correct I miss counted you need one more. They should all line up once you add one more. So the repeat and the end repeat will be the 1st indent. Then your see the next IF, ELSE and END-IF line up then the nested IF' and IF-ELSE's will line up with there END-IF's

2 Likes


Okay, how does this look? :sweat_smile:

1 Like

Nice, that looks better

1 Like