Set up rules as subroutines

Being an old school programmer who doesn't want to get into writing and maintaining code, I would like to have the ability to pass a device name and/or a hub variable to a single rule that would perform the actions on the specified devices or variable.

A previous post suggested this idea. How to reuse one rule for multiple similar scenarios - #15 by Jo6620

My use case is for window shades. I have rules for each shade that opens or closes the shade and another that moves the shade to a mid position. Each rule does exactly the same thing to a different device. For just 9 shades I have 27 rules.
If I could pass the device name to a single rule that performs the actions on the selected device, it would mean only 3 rules.

I have a single rule that uses these sub-rules to control the shades based on time of day and sun position. Using good rule names let's me read the rule like pseudo-code. That makes it much easier for me to read the rule and also it makes sure that I use the same logic for each device type.

Yes this could all be done in an app, but this way everything is in Rule Machine and I don't have to write apps.

The trick is that Hub Variables (Globals) can't store references to devices. - So if your hell bent to do this in RM, and do this all in one rule, your going to end up with device selections based on a string variable, that represents the device name.

If (HV_Device_Name = "Shade A") then Set Position Shade A (the actual device)
If (HV_Device_Name = "Shade B") then Set Position Shade B
If (HV_Device_Name = "Shade C") then Set Position Shade C
etc.

Every reference to a device output is going to end up with 9 if's for each of your devices.

There are also concerns on how to separate the triggers to fire this rule (are the all "OR"ed together) and then another block of if's to figure out the "real" triggering device.

Finally, then concurrency and race conditions will become problematic.
Basically, there aren't procedures with parameters in RM (there are effectively procedures, but passing devices as parameters is the "sticky wicket" here.)

So I really see three possible workable approaches:

  1. Separate rules in RM (which you said you don't want) - This is what I personally would do
  2. Write an App with Parent / Child devices, which is basically the solution to the thread you linked. (Likely the best answer..)
  3. Look at WebCore, which apparently CAN pass devices references as variables - I no WebCore guy, so I can't help there, and I still think concurrency may be problematic, but it may be worth a look.

But doing this with RM, and a single rule is going to result in one long/ugly rule (with lots of conditionals for each device) that's likely going to be problematic when multiple shades need to be active concurrently.

Now if they changed RM to be more procedural, and have a call stack, etc - But I don't really see that happening, - Apps, written in a "real language" (if you want to call Groovy that), with functions, procedures, etc. is going to make this "single set of logic" approach much easier.

The current RM is not well suited to what your asking for.
Good Luck.