Bear with me, this is a complex one, but I’m hoping to change it into a “Share your projects” one.
I was trying to help a fellow user (@easwaran83) recently and the topic turned into a question like “how do I go through a list of items randomly without repeating any item”?
I don’t (yet) have a use case for this but can see it becoming of use at some stage (e.g. picking up a random item from an inventory until the inventory is finished and needs replenishing)
so this is what I started with in my mind:
- an input_select to store the inventory list
- an input_select with numbers (0 to inventory length) sorted in random order which would become the location of the next inventory item to select (similar to lookup in excel)
- a counter to keep track of where we are in the list of random numbers
- an automation to initialise the input_select list of numbers
- a script to pick up the next inventory item and increase the counter
Straight away I struggled with a few things:
- programmatically populate an input_select with numbers. For some reason I always end up with all my numbers as an array on the first row of the input_select.
Here is an example code I’ve used. What am I doing wrong?
inventory_set_random_list:
alias: Set inventory Pickup List
sequence:
- service: input_select.set_options
data_template:
entity_id: input_select.inventory_position
options: >-
{%- set mylist = '28 85 45 17 99 48 38 73 13 27 11' -%}
{{mylist.split(" ")}}
Here is the result:
- shuffle the input_select list in random order. I’ve created a list of 11 unique numbers sorted in random order. If I had a list of say 500 numbers, how would I sort them in random order? Alternatively I can pull a random list via a scrape sensor, but I still need to be able to “load” it to my input_select…
Any ideas?