Random selection of multiple items

I’ve got a group with 16 items in it. See below.
I now want to select randomly several items, switch them ON, switch them OFF and and switch ON other randomly items.
Any idea/suggestion how I can do this?

For example:

  1. Select 3 random things from GroupA: SelectionItems = Relay04 - Relay09 - Relay11
  2. Set ON: sendcommand( SelectionItems, ON)
  3. Sleep 10 second: createTimer(now.plusSeconds(10))
  4. Set OFF: sendcommand( SelectionItems, OFF)
  5. Select 3 (or 4 if possible?) other things from GroupA: SelectionItems = Relay05 - Relay14 - Relay03 - Relay06
  6. … loop …

Items:

Switch   relay01   "Relay 01"   (GroupA)   {channel="mqtt:topic:test:relay01"}
Switch   relay02   "Relay 02"   (GroupA)   {channel="mqtt:topic:test:relay02"}
Switch   relay03   "Relay 03"   (GroupA)   {channel="mqtt:topic:test:relay03"}
Switch   relay04   "Relay 04"   (GroupA)   {channel="mqtt:topic:test:relay04"}
Switch   relay05   "Relay 05"   (GroupA)   {channel="mqtt:topic:test:relay05"}
Switch   relay06   "Relay 06"   (GroupA)   {channel="mqtt:topic:test:relay06"}
Switch   relay07   "Relay 07"   (GroupA)   {channel="mqtt:topic:test:relay07"}
Switch   relay08   "Relay 08"   (GroupA)   {channel="mqtt:topic:test:relay08"}
Switch   relay09   "Relay 09"   (GroupA)   {channel="mqtt:topic:test:relay09"}
Switch   relay10   "Relay 10"   (GroupA)   {channel="mqtt:topic:test:relay10"}
Switch   relay11   "Relay 11"   (GroupA)   {channel="mqtt:topic:test:relay11"}
Switch   relay12   "Relay 12"   (GroupA)   {channel="mqtt:topic:test:relay12"}
Switch   relay13   "Relay 13"   (GroupA)   {channel="mqtt:topic:test:relay13"}
Switch   relay14   "Relay 14"   (GroupA)   {channel="mqtt:topic:test:relay14"}
Switch   relay15   "Relay 15"   (GroupA)   {channel="mqtt:topic:test:relay15"}
Switch   relay16   "Relay 16"   (GroupA)   {channel="mqtt:topic:test:relay16"}

Just a few ideas out of my head, you have to look for the correct syntax:

  • put all items into a Map(java.util.Map) with the label as key
  • use Math.random() * 16 as IntegerValue to select a random item - something like ‚myMap.get(„Relay „ + IntegerValue(Math.random() * 16)).sendCommand(ON)‘

Ideas sources

Found these already. But for me, these are more ‘random times’?
I’m trying to figure out how I can get random items (more then 1!) from a group…

Sorry if I don’t see the solution in these topics. I’ve got just a limited knowledge of scripting. So most is try and error. :blush:

ps In the end, it’s an exercise for a group of outside Christmas lights (16 different lights), that will ‘never’ use the same pattern. Minute 1 those lights, minute 2 other lights, minute 3 again other lights…

Okay, we only know what you tell us about what you have already tried.

The first link provided selects a random member of a group.
Doing that again will give you another one.

Probably more useful -
The last link gives a randomized list of group members. You may then use that to choose a random number of members from the head of the list.

So far so good:

GroupA.members.sortBy[g|(Math::floor(Math::random*100))] . forEach(
          relayX,i |
          logInfo("TEST001","Test relay is " + relayX)
          )

Gives me a nice randomized list.
A bit more info then I want (fe Type, state…), but it’s a beginning. :wink:

> 2019-11-15 13:34:30.034 [INFO ] [lipse.smarthome.model.script.TEST001] - Test relay is Relay14 (Type=SwitchItem, State=OFF, Label=Relay 14, Category=null, Groups=[GroupA])
> 2019-11-15 13:34:30.035 [INFO ] [lipse.smarthome.model.script.TEST001] - Test relay is Relay15 (Type=SwitchItem, State=OFF, Label=Relay 15, Category=null, Groups=[GroupA])
> 2019-11-15 13:34:30.035 [INFO ] [lipse.smarthome.model.script.TEST001] - Test relay is Relay08 (Type=SwitchItem, State=OFF, Label=Relay 08, Category=null, Groups=[GroupA])
> 2019-11-15 13:34:30.036 [INFO ] [lipse.smarthome.model.script.TEST001] - Test relay is Relay07 (Type=SwitchItem, State=OFF, Label=Relay 07, Category=null, Groups=[GroupA])
> ...

Next step, trying to just get 5 items from the list…

logInfo(“TEST001”,"Test relay is " + relayX.name + " " + relayX.state.toString)

It’s only a string, put what you want in there.

1 Like

I was trying to add [0:5] in the ‘members’ rule, but this doesn’t same to work.
And it seems that this rule won’t be put in a var? Guess because it’s list, and not only 1 value?

Will have another try later on. Getting a bit dizzy, to much screen today. :wink:

Don’t know what that means.
Maybe you want to iterate over your new list, but only the first five members. Perhaps that suggests a different approach.

You can put a list in a var, it’s just another object - but you would need to use methods on that var appropriate to handling a list.