A technical question about Conditional Actions

Over the years of adding to my Rule Machine, I have discovered that I have changed the way I write conditional actions, although the intended result is identical. I used to stack up [IF-THEN] actions in series, and now I sorta embed them all into one big [IF-THEN] action. Either way, the end result is essentially identical, but it leaves me wondering if one case better than another for any particular reason?

Typical early Conditional Action format:

[IF]Condition 1[Then]
Action1
[END IF]
[IF] Condition 2[THEN]
Action 2
[END IF]
[IF]Condition 3[THEN]
Action 3
[END IF]

Typical Current Format:

[IF]Condition 1[THEN]
Action 1 [ELSE]
[IF]Condition 2[THEN]
Action 2[ELSE]
[IF]Condition 3[THEN]
Action 3[ELSE]
Exit Rule
[END IF]
[END IF]
[END IF]

Short answer, no. Long answer, also noooooo.

EDIT: Short answer is yes. Long answer is, it depends on what is being checked and the desired outcome.

2 Likes

That's good. I didn't want to go back and change up a bunch of perfectly good rules because somebody told me it was 1% more efficient one way than the other...

1 Like

It depends on what you want to accomplish.
With the first method, it is technically possible for all of the Conditions to be true and all of the Actions to be executed - they are all unrelated to each other (unless your specific conditions make them so) and all of the IF statements will be evaluated in sequence.
With the second method, there is a nested relationship and only the Action for the first Condition found to be true will run (and the Exit Rule is unnecessary).

3 Likes

These two codes are not at all identical. Top code can execute 0 or up to 3 actions. Bottom code will always execute only 1 of them

6 Likes

That is a good point. But I did state the "intended result" was identical, so I guess assume I want only one output from the set of conditions. But I should have been more specific with my description.

1 Like

Actually is 0 or 1 of them.

Also you could try using RM 'Else If' for easier to read code for the second format. No exit rule required. Using the 'else if' format would make it clear to you that only 0 or 1 action would execute.

2 Likes

That's true.

I find that a lot of times, most of my current rules use the "Exit Rule" action as the very first action, and then each additional embedded IF seem to get more specific. Sometimes I have multipleof these chains of IF ELSE statements in a single rule. Usually the way I'd use that is one trigger has multiple different actions, depending on several different possible factors. I'd generally have the Exit Rule action first in each string, so that the action ends does nothing, UNLESS.... That way if I order the strings of embedded IFs in the proper order, each one has the ability to terminate the continuation of actions midway through the action, or perhaps before the action even starts, given the circumstances

Yup been there before.

Bottom code depends on whether there is a an 'else' on the final 'if'. 1 action if there is an 'else', 0 if not. The OP ha no terminating 'else' therefore 0 actions are possible.

1 Like

I should have known the best way to get the right answer is to post a wrong one :crazy_face:

1 Like

Has this place turned into Reddit?

It has been eye opening. I realize I have little understanding of how things work, and maybe it's been luck that I have so many rules that work the way I want them to.. but at the same time, a few sarcastic replies have actually brought many important issues into perspective, and provided insight.

I have only recently began to really explore the things Hubitat can actually do, so I have not really gone deep enough to need to ask for help until recently. Every time I ask for help, I learn ten times more than I thought I needed to know.

1 Like

Turns out, I DO have to go back and rewrite a bunch of perfectly functional rules... NOT because they don't work now, but because I have a better idea what I'm doing now. I'll probably ask another question soon that will blow this all wide open again and I'll be rewriting it all another time.

Welcome to the Sisyphean hobby of real home automation :sweat_smile:

1 Like

Welcome to the Sisyphean hobby of real home automation :sweat_smile:

It’s like a drug… every time I think I am done, I think of some new cool thing I could automate :crazy_face:

1 Like