Can Rule Machine dynamically reference a variable name like textToSpeak%Randnum%?

I currently do this with a large If Then Block, but it is a pain to change the number of items to randomly pick from for Alexa to speak. It would be much easier codee to be able to dynamically reference a variable name. Is there a way to do this? Maybe I am not thinking of this in the correct manner?

e use case is just I like for Alexa to tell me how many days to big events that are coming up as part of my nightly routine and I have it randomly pick an event and tell my "there are x days until y event"

Thank you!

Mike

If you have set a variable to a number or string value, Rule Machine should be able to send a notification that makes use of the variable value.

To have Rule Machine send the notification "there are x days until y event" you would type

"there are %variable-x% days until %variable-y% event"

Essentially, enclose the variable name in %’s and rule machine substitutes the current variable value automatically when sending the notification.

2 Likes

OP might wanna check out the RM5.1 docs on using %var% for strings.

I’m doing that now but I don’t think there is a way for the actual rule to change based on a variable value.

That's kinda where my first (deleted) answer was going, as I figured you meant "logic branching" or "indirect reference". But in RM, where there's a will, there's always a way!

Maybe show us a screenshot of how your rule currently works??

1 Like

Not sure if this helps for your use case, but people have used RM's token function to pick random strings out of a list.

2 Likes

SOrry for the delay and I appreciate the help. I was on vacation and my wife made me put away the computer for a bit.


Here is how I am doing this today and I don't like it as it is difficult to adjust and not very sleek.

Mike

Had a slimmed-down rule ready to show you but it crashed Rule Machine, so I'll have to recreate it again tomorrow.

Try this on for size... my goal was to eliminate the IF-THEN tree and ugly hard-coding of the date strings, by putting the occasions into a compact, easier-to-maintain list to pull from.

The actions extract a random event from the list, then parse the result into two components (What and When).

From there, you can do all the computational stuff you need.

Note the random() seed must equal the number of events, since it generates 0, 1, … , N-1, ideal for 0-indexing through this list. (It will never output N; for this reason, I think your rule will never evaluate to 5.) Just update Step 1 as your list grows.

Here's how I set up the variables:

The rule works as intended. SFAIK RM doesn't offer a way to remove Time from the finished DateTime var, but that won't affect your calculations.

Warning: If you mistakenly set the random() seed higher than N (the actual count of list items), RM will stop (and Log an error) at Step 2 whenever the index goes out of bounds. Worse still, if you mistype anything in Step 5, expect the rule to crash irretrievably, and you'll have to start over. (For this reason, I keep Export'ed copies of touchy rules.)

Thank you! This is a better way. I appreciate it.

1 Like