How to pick a random number between 1-50 and enter it as a parameter

Hi all,

I'm looking for some creativity on how to accomplish something. I need to basically create a rule that when a button is pressed, picks a random number between 1-50 and enters it as a parameter in a custom action. Is this even possible?

My use case is that I'm using an ESP32 with WLED flashed on it as a LED controller for a WS2811 individually addressable LED Strip. I'm using a custom driver that integrates WLED with Hubitat. WLED contains roughly 50 pre-programmed 'effects'. Setting the parameter of the device to any number between 1-50 will select a different effect.

The LED strip is in my daughter's room and I'd like to program a Pico remote button to randomly select an effect by randomly choosing a number.

Any ideas?

1 Like

This thread may help get you started:

3 Likes

Thanks! I think I'm close but now just need to get the variable into the parameter of the device but I'm getting an error.

image

image

but I'm getting this error;

Oops, never mind, forgot the % at the end of the variable name!

It seems to be working! But i'm still getting an error:

The error doesn't seem to bother anything and the LED controller is picking a random effect now with the push of a button. My daughter is going to be so excited! Thanks @672southmain! I'm also a little more familiar with variables now which have been a black box to me mostly!

Sorry to make you struggle without giving an answer you could simply copy, but itā€™s the only way I knew to get you to think and learn.

Sadly, the Hubitat documentation is cr*p, in my opinion.

Hereā€™s a screenshot of the two lines of a rule you need to insert to get the number (1 to 50, inclusive) you want. I added a logging statement so you could try it a few times, but the way to test, really, is to just create the two line Rule that sets Effect, and hit the ā€œRun Actionsā€ button and watch the resulting value of Effect.

No worries, I totally get it! Where do I find the "add 1 to variable" in the list of actions? I can't find it anywhere!

Nevermind, spoke to soon.. found it!

1 Like

Edit: see you found it.

Sorry! Thank you though! I think everything is working great now! Minus that pesky error grovvy error.

One more for good measure

Thatā€™s being generated by the driver. I donā€™t have that device, so I canā€™t know what the driver wants. Iā€™d look at the code, but I donā€™t have the driver. If itā€™s a community driver, open it up, look at line 488, and at the definition of the setEffect routine and what types it expects for arguments.

Does it generate the error if you call it with integers 1 to 50 ?

Oh that makes sense then. Here's a screenshot from that area of the driver code:

If I manually set an effect through the driver, I get the same error in the Log. Perhaps it's expecting and FX Speed, FX Intensity and Color Pallette as well; although it works fine without them as it seems to be using defualt ones built into the effect.

image

Sorry, I just realized the line numbers from the device driver didn't show up in the screenshot. This is line 488: def paletteName = state.palettes.getAt(pal.intValue())

This is probably more something for the guy who wrote the driver but I appreciate you looking into it for me anyways!

Looking at the log, it seems to indicate that the effect is some sort of enum (Halloween, Theater Rainbow, etc.) or list. I can understand why it barfed on 53, if your docs indicate it wants 1 .. 50, but I donā€™t understand the other errors.

Is there an enum or list or map earlier in the driver that is used as the type for fx?

I don't see anything. If you're interested, here's the code on GitHub but don't feel obligated at all to spend time on it:

Glancing at the driver, the error is always when it tries to get the Color Palette name. Try putting a valid palette in as a parameter.

For some reason it's getting an array of 0 instead of a number when you call the function. Technically it shouldn't have even worked. I added the segment index to the setEffect call, see if that fixes this for you.

Thanks! I'll try that new driver soon. For now, things are working great minus that error in the log. Nice to have this awesome LED controller integrated into Hubitat.

Here's my daughter's bed. She's pretty excited.

1 Like