How do you get a button to trigger rules randomly?

Hi there,

I created a series of rules in RM and would like to run them randomly each time I push on a button. In other words: I push a button, and a random rule among my 10 rules will run.
I only saw randomness associated with time delays, which is not what I'm looking for.
Not sure how to achieve this. Thank you!

Since you already have multiple other rules, what I would do is make one new rule triggered by the button press, set a local number variable (that you create for this exact purpose) to a random value (this is an action you can do when setting variables in Ruled Machine), then use a series of IF THEN/ESLE-IFs, etc. (conditional actions) in the rule to do a "Run Rule Actions" on one of the others, depending on the value of this variable.

That's the general idea. I'm not sure how familiar you are with Rule Machine, so just ask if you need more help!

1 Like

If you are going to make a new rule, I would think you should simply use the 10 actions from the other 10 rules in this new rule instead of 10 actions of running another rule. (Does that make sense?) Then you can delete the other 10 rules. Also, I believe you can use Simple Conditional for each and then not need a bunch of "Else"s.

2 Likes

That works too; I suggested this since the other rules already exist, and sometimes smaller, separate rules are easier to maintain. There would be no difference in outcome with a simple conditional, but every expression would have to be evaluated on each execution of the rule actions, whereas "full" conditionals allow the more efficient (though this shouldn't really matter in real usage...) use of ESLE-IF, and it's more flexible in the long term if you want to add additional actions inside.

But, as usual with Hubitat, there are lots of ways to reach the same goal. :slight_smile:

2 Likes

Thank you so much to both! I am going to try - I haven't tried variables yet, nor ELSE-IF. I'm pretty new at this,

So, I must be doing something wrong because it is not working properly.
This is what I did:

  • I set up a hub variable. I wanted to set a variable number that goes from 1 to 5, but not sure how to do that. This is what I did: |Name: Scene#|Type: Number|Connector: Variable|Value: 5

  • Then I created a random rule generator with ELSE and IFs (these are all nested under each other, which doesn't appear below)

Trigger: Black Button button 1 pushed

Set Scene# to random(Scene#(5))
IF (Variable Scene#(5) = 1(F) [FALSE]) THEN
Run Actions: Movie scene 1
ELSE
IF (Variable Scene#(5) = 2(F) [FALSE]) THEN
Run Actions: Movie scene 2
ELSE
IF (Variable Scene#(5) = 3(F) [FALSE]) THEN
Run Actions: Movie scene 3
ELSE
IF (Variable Scene#(5) = 4(F) [FALSE]) THEN
Run Actions: Movie scene 4
ELSE
IF (Variable Scene#(5) = 5(T) [TRUE]) THEN
Run Actions: Movie scene 5
END-IF

  • This is an example of rule I created. I created 5. All rules have the same format and the same trigger (Black button pushed).

Trigger: Black Button pushed

Set Volume on Bathroom Sonos to 25
Dim: Bathroom light shower, Bathroom light sink
Bathroom Sonos: Play Track: I included the link here
On: Movie Scene 1 (Command only switches that are off)
Dim: Bathroom light shower, Bathroom light sink: 100 --> delayed: 0:01:15

What happens is that I push the button, and for some reason the variable is then set to 0 and the Movie Scene 1 starts playing every single time. When I press again, it is the same rule that plays.

Bathroom Sonos is playing
Bathroom Sonos is transitioning
Bathroom light sink was turned off
Bathroom light shower was turned off
Scene# variable is 0
Black Button Button 1 pushed

Could you please help? Thank you!

Can you share an actual screenshot of your rule? If you're using nested IFs inside ELSEs, you also don't need to do that--I suggested ELSE-IF, which, among other benefits, will make it easier to read with cleaner indentation. (This is available from the menus, the same way you add conditional actions in the first place.)

From your screenshot above, it looks like the last time the rule ran, your variable was set to 5 and so the Movie Scene 5 rule should have executed. What you should do to figure out what's happening if that didn't happen is enable all logging on this rule, then check the output in Logs after any execution.

Looking at this more, it appears you might have a problem with your "random" setting of the variable. Did you choose this same variable for both the variable to set and as a variable operand ("variable"/"first variable")? If so, you really want to choose "constant" for that second option, then specify the maximum value (5, it appears--or actually you might want to do 4 and take all your comparisons down by 1, as the minimum value is 0 and not 1).

Trying to upload images in jpg or png in my message, and I get an error message:
Sorry can't embed media items in a post

OK, I think this is indeed where the problem lies!

@bertabcd1234 I'm getting closer but still have issues: when the random number picked is anything but 1, the rule won't run (even if it says it does). When the random number is 1, rule 1 plays as expected.

Actually, when I go in each non-working rule (0, 2, 3, 4), and go to the "Define a Rule" page and click on "run actions", these rules won't run either.

I include below some screenshots:
This is the random rule generator w/o nesting:

This is the global variable page:

This is my log of non working rules (nothing happens)

This is my log of working rule:

Now this is rule 1:


and the action page for rule 1:

By comparision, this is rule 0 (that does not work) - looks the same to me, except for the local variables on the define rule page.

Do you have any idea what I'm doing wrong? Thank you so much!

I realize by the way that I have switched to simple conditional (but it seems this part is working?)

@stephen_nutt Thank you! I tried the simple conditional (see my screenshots below). It seems to be working. But each of my rules are pretty long - a single rule would be very long and less easy to maintain I think.

There was no need to switch to a simple conditional, for the reasons I've mentioned above, but either should work.

I don't see any reason that this rule shouldn't work, and your logs suggest it is. Do you have logging enabled on the "target" rules, and if so, do you see anything there when this rule is supposed to have run those actions? In one of your screenshots, this looks like it a should have happened around 7:26:53.

You can filter the logs to just specific apps (like rules), which may make for more readable log captures.

There was no need to switch to a simple conditional, for the reasons I've mentioned above, but either should work.

I misunderstood what you meant when you said I should not nesting IF inside ELSEs. This is what I used to have:

I didn't manage to take the IF indentations (Is that what "nesting"is?). I'm all for having the most efficient rule, because I want to grow my list of random rules to a much larger number than 5 in the future.

I don't see any reason that this rule shouldn't work, and your logs suggest it is. Do you have logging enabled on the "target" rules, and if so, do you see anything there when this rule is supposed to have run those actions? In one of your screenshots, this looks like it a should have happened around 7:26:53.

Nothing appears in the rule log. Yes, logging is enabled. I can only pause, stop and restart the rules in the rule page, but it won't run. I may have to delete them all and re-create them. I think I messed things up when I played around with the rules (tried out conditions, then deleted them, etc.)! Hopefully it will work with clean rules.

Yes, each if your IF THENs is nested inside another IF THEN. I would highly suggest the use of ELSE-IF instead. You can add one via the "Conditional Actions" menu, the same was as IF THEN is in the first place. This would be logically equivalent in outcome without the awkward nesting upon nesting that you can see from the indentation hints the editor shows you. But you have simple conditionals (IFs) now, which should also work, so maybe don't change unless you're into trying things out to learn for no other reason. :slight_smile:

This is unusual. You could certainly try re-creating the rule and pointing your "random" rule to this new one instead to see if that helps. If you ever used a required expression, there was a specific bug relating to the rule not always "seeing" that it's OK to trigger again when the expression was removed and "Cancel pending actions..." was selected, which will be fixed in the next release or can be worked around in the meantime by re-adding any required expression, unchecking the option, and removing it (throwing this out there in case that's something you may have done and it's easier than re-creating the rule).

I'd keep logging enabled on all rules in question to make things easier to troubleshoot should they not work as expected. Good luck!

Thank you so much for your patience!! Your response was super useful. And YES, I used required expressions! As well as what you described, so it makes a lot of sense.

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.