Randomly fire a random switch

basically i have one of those LED strips, and i want HA to randomly change the colors.

i have all 15 colors from the IR remote saved as switches for my broadlink.

so: randomly [time] trigger a randomly selected switch [from predefined list of switches]. doable?

i didn’t wanna re-invent the wheel if someone already has this figured out.

Here’s the JINJA2 code to select a random reply in an Alexa response. Perhaps this will get you started down the right path…
See the section titled : GIve alexa some personality:

surely someone has some code to randomly fire a switch. ya know, to simulate someone being at home turning lights on and off.

Maybe this can help get you started,
https://github.com/Danielhiversen/home-assistant_config/blob/master/automation/lighting.yaml#L229

Looks like this thread is a bit old, so to give it a fresh dimension:

I want to create a “vacation_random_event” python script - when fired, it randomly selects an Entity and toggles it. The later the day gets, I will increase the probability the ‘toggle’ becomes ‘turn_off’, so that by midnight everything is off. I do most my scripting in Python, where it’s easier to do this more complex work. I understand I could do this other ways, but I’d like to stick to Python if possible.

All that said, I’m once again confounded by HA’s python shell disallowing generic library Imports. i.e., it’s really easy to import random

but… that’s disallowed by HA (hassio on RPi3 specifically). So I thought I’d do it the old-fashioned way, starting with:

upperbound = 10
with open('/dev/urandom', 'rb') as file:
    rtnbytes = file.read(upperbound)

but… from the log file:

File "/usr/local/lib/python3.6/site-packages/homeassistant/components/python_script.py", line 166, in execute
exec(compiled.code, restricted_globals, local)
File "vacation_random_event.py", line 19, in <module>
NameError: name 'open' is not defined

um… “name ‘open’ is not defined”? Apparently I can’t ‘open’ files? I guess I’ve not tried before, but how is that native python function disallowed?

Anyone see what I’m missing?

Or, conversely - any thoughts on generating a random number in HA/Python script? That’s all I’m really looking for. I suppose I could generate one in JINJA2 and pass in as a variable, but that seems silly given how easy this SHOULD be in Python.

Thanks all - happy thanksgiving!

1 Like

Agree totally… the Python scripting capability is awesome but crippled in the limited functionality . . . I can’t even use other built in functions like max() because that results in an error too.