A rule that randomly selects different scenes

Now that I have a tone of scenes imported from my Philips Hue bridges, I want to create a rule that will select a different scene every evening around sunset time. This would ideally only fire when HSM is not in Away mode (I haven't learned to use HSM yet but eventually want to). I should be able to designate 10-12 Hubitat CoCoHue scenes and it would randomly pick one every evening at sunset and turn it off around 10 pm or so.

Is there anything in RM that would allow such a rule to be created?

A rule that looks something like this should do what you want. The most helpful feature is the ability to use the "variable math" option when setting a variable to assign a random value to it:

Some comments: the scene will effectively be set at some random interval between sunset-15 and sunset+15 (i.e., the total time between the earliest time the delay could be up, which would be immediately after the rule triggers at sunset-15; or the latest the delay could be up, which is 30 minutes; most times, you'll get a value in between those two times). This could be simplified a bit if you want things to happen at the same time every day, but this is one way you can get in the time randomization that it sounds like you wanted.

Them, it checks if HSM is disarmed; if so, the rule exits (i.e., no further actions run). This is a "simple conditional," but you could expand it into a full IF THEN if you wanted to check for something more complex (i.e., not being armed to any of a few different modes, for example). Since you said you're not using HSM at the moment, you can probably leave this out for now--easy to add in later.

Then, we get to the "random scene" part. The trick here is creating two local variables, one to hold the "scene number, and the other to hold the maximum number of scenes you want, minus one (more on that in a second). In this example, I chose 10--which is actually 11 total since this starts counting at 0. These variables are sceneNumber and maxSceneNumber, respectively, but you can call them anything. They should be of type "number." (You actually don't need the maxSceneNumber variable; you could just use a literal value--the maximum number--instead of this variable on the "Set sceneNumber to random..." line, but I think the intent is clearer if you do it this way. Imagine yourself trying to edit this rule half a year from now.) The "Set sceneNumber to random(maxSceneNumber)line will set thesceneNumbervariable somewhere between 0 andmaxSceneNumber`, so in my example 0 and 10.

The rest of the rule just checks for the random number, which has now been assigned to the sceneNumber variable. To activate the CoCoHue scenes, I am calling the "push" command on button 1 (treating them as buttons). You could also use the "Turn on" (treating them as a switch) action, though I generally recommend the former for reasons you may have read about elsewhere by now.

What I didn't include here is how to turn them off. The easiest way is to add another action:

Wait for event: Time is 10:00 PM

Then add another action to turn off the affected lights or groups (or the scene device, though again I would generally not recommend that--all it does is the former, anyway, and the behavior is clearer that way). Perhaps you will want to wrap this "turn off" action after the wait in a conditional so it only happens if HSM is disarmed (or your presence device is not present, or however you want to handle this). Perhaps you want to add another random delay before this to make it happen at not exactly the same time every time as well.

Alternatively, you could just call "Cancel rule timers" on this rule from any rule. For example, you could have another rule that triggers on HSM becoming disarmed (or your presence device becoming present, or again however you want to handle this) and then runs that action on this rule. That will cancel any waits, delays (cancelable or not), and a host of other things, though for this rule that should work. (I'm pretty sure it won't erase the time-based trigger, but I suppose that's worth testing; I know it does affect periodic triggers until the rule is re-initialized.)

2 Likes

Bear with me as I'm still figuring out Rule Machine. I've been using Simple Automated Rule maker up until now.

What does "Cancel Delayed Actions" and "Delay 00:30:00 (cancelable) Random" do here?

I want this to not work when we are away from the house, so I assume I would do "IF (HSM status is Armed Away) THEN Exit Rule" which is how I have it configured.

Now how do I get "Set sceneNumber....." ? I went through the list of choices, looking for something that involved setting a variable, but the closest thing I found was setting a private boolean to true or false which I don't think is it.

I may have forgot to mention: you'll have to create two local variables for this to work (or global, I guess, but you might as well make them local unless you have a use for them elsewhere). This is done from the variables section on the initial page in the rule, so you'll need to do it before you go into the actions page. However, there, you should see options to set a variable (it's possible these actions are hidden unless you have variables, otherwise the docs can tell you where any action is found).

As for the conditional, wherever you actually want to test for there should work! Just wasn't sure if you wanted it tied to all arm modes, presence, or what.

where do I set local variables? I only see the option to set a global variable.

To create local variables, use the "Create, Set, or Delete Local Variables" section at the bottom of that particular rule:

To set a variable in your rule actions, it's the same for global or local variables, under the "Set Mode, Variables or File, Run Custom Action" category. Here's what it might look like for one of my actions above:

1 Like

OK, first of all, thank you! I think I'm starting to get the hang of this.

Here is what I have.

This will fire about 15 minutes before sunset, checking first to make sure HSM is not in Away mode. There are 17 possible scenes, and going from 0-16 it will turn on one of the designated scenes (I didn't do the button method you mentioned because I didn't fully understand it). Then around 10 pm it will start dimming the lights to nothing over about 30 minutes.

Does this look right to you? Assuming this works, I'm thinking about creating different versions of this rule for each season, and perhaps time of day also.

That looks like it would work to me--mostly! (The button thing is under the category with "Push a button," and you'd specify to push button 1, but the "on" command does the same thing with this device, so really either way would work.)

The "mostly" part comes from that "Start lowering" action, which will not work as you describe. What this does is send a startLevelChange(down) command to the bulb/group. You can test this on the device page to see what it does. It's great for using with a button device that you want to act like a dimmer (often paired with stopLevelChange() on button release), but it's unlikely to be what you want here. It also won't happen over 30 minutes. The action itself will just be delayed by 30 minutes (the "delay" feature you used here is available to all regular Rule actions and just schedules it to run at that point in the future). It sounds like you may want to use the setLevel() command with a destination level of 0 and a transition/fade time of 30 minutes (or 1800 seconds; the command itself accepts seconds, though different apps may present this differently, and again you can test this on the device page). This timeframe is a bit long but still within the specs of what Hue bulbs are reported to accept, and setLevel(0) is itself an odd command that may not work as expected (turning off the bulb) in all drivers, but I know this works like you want in CoCoHue (again, can't vouch for other drivers).

One other odd thing--which won't affect the behavior of your rule--is that your last couple actions wait for a specific time, then delay 30 minutes. You could just wait until 10:30 instead of 10:00, and the outcome would be the same (in this case; not sure if you were planning on adding more where this might make sense). Also, the "Cancel Delayed Actions" isn't really needed at the start of your rule since you don't have a random delay after your trigger time like I did, which would really only be useful if the rule re-triggered while that was still counting down, so honestly, I probably really didn't need it either in real life. :slight_smile:

1 Like

Thanks! So when I used the "Set Dimmer Level" command and put in 1800 for the "with this fade (seconds)" part, it says "bad value." That's a new one to me. It will accept 100, but not 1000 or higher.

Is there another way to do this, or should I just ignore that?

OK, so maybe that was the wrong command. I ended up using "Fade Dimmer Level Over Time" and got this:

Fade Living Room Hue Lights down to 0 over 30 minutes with 60.0 seconds interval

Which looks like it will do what I want?

You could use the "Fade dimmer over time" thing if you wanted, but that moves things from the driver to the app and requires you to cancel the "timer" this creates in the rule if you way to, say, tuen thr light on ff before the fade would be regularly done.

I'm not sure why Rule Machine won't accept valúes over 100 for the transition time. I suppose it's just a sanity check on the fade time value, but I think most Z-Wave devices still allow values bigger than this, so I'm not sure it's a good choice. Anyway, you can probably work around it by using a custom action instead. You'll have to manually specify two numeric parameters, the first being the level and the second the fade time (in seconds)--the same to values you'd use if running "Set Level" manually from the device page.

You're right, I have had that exact same problem. What would be the solution to that? Could the rule be configured to exit rule if the lights are turned off?

You could use another rule triggered on those lights turning off, then run "Cancel Rule Timers" on the rule with the fade. That will un-schedule all future jobs for that fade (plus just about any scheduled job the rule creates, as noted in the UI, but if that's all you have, it should be fine). But with Hue bulbs, again, you can probably avoid this in the first place by using "setLevel" as a custom action with the level and time parameters you want (though test this command on the device page first to see if it does what you want)--it's a bit odd that RM caps the number of seconds this low for the standard action, but again I assume that's just an arbitrary (and not too unreasonable in most cases) limit they decided on to keep anyone from making weird accidents...

1 Like