Homey Community Forum

Is it possible to set a variable to a random number from an array?

Hello,
I have a few flows involving sounds and a variable (SoundVar) that determines which sound to play:

  • If SoundVar is 1 then play song one
  • If SoundVar is 2 then play song two

    I have 5 of these flows.
    I can pretty easily calculate a random number to put in SoundVar with {{round(random(1,5))}} when an action happens.

My question is, can you do this if specifying which numbers to randomize ?
Example:
When pressing button 1 I want to use sound 1,2,3,4 and 5.
When pressing button 2, I want to use sound 2,4 and 5 but not 1 and 3.
For my doorbell I want to use sound 1,3 and 4 but not 2 and 5.

Is there a one-liner that one can put into the "calculate variable as … " that can do this?

Edit: I would like to say that this is just a small example of a larger picture and for this few flows, I would probably just create different variables and flows for each device. It is going to be larger and encompass other things than just sounds, meaning that I could use it for other types of flows as well.

It’s in Dutch but it can help what you want to achieve. Google translate is your best friend
Link

Thank you for the answer.
Don’t get me wrong here. This method is viable.
But I feel like it is only to a certain degree. Specifically it is limited by the maximum of 3 in “And/or”.

For others reading: I would create a new variable for each device that would trigger the sounds and have different “playlists” and would then set it up in each flow for the sounds in the “And/or” section.

Example: For the doorbell above (should play 1,3,4), I would make a new variable called “DoorBellSounds”.

  • In the doorbell flow, I would make a card with “Set DoorBellSounds as {{round(random(1,3))}}”
  • In each of the flows for Sound One, Sound Three and Sound Four I would add a card in the “And…” section to trigger it if SoundVar is one number or if DoorBellSounds is another number.

I know that I am being picky here, but the reason for the random with an array of numbers is that this could be solved by a single card if it is possible.

For clarity’s sake I can try to give an example even though I know it is not a real calculation:

It would be something like {{random([1,2,4])}} that would provide a random number of either 1,2, or 4.

It could be like this {{round(random(1-4),0)}} in that case you als have to make a extra flow, as it becomes number 3 start te flow again