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!