Robin, doesn’t your program have the potential to leave a light on all night long? If the last Random Toggle for a device before 10:30 pm turns a light on, won’t it will stay on all night until the program resumes the following day at sunset?
I used some of Brandon Pines similar code in dct7c (below) to toggle lights on and off. I “overlaid” this WC logic on top of ST Smart Lighting which turns on some living room lights an hour before sunset. WC starts toggling after the lights have been turned by ST, so I know the first toggle turns them off. A second toggle will turn them on, so it is possible that a light will be on at the end of the execution time window.
To prevent a light from staying on all night, I added a “cleanup” chunk of code at the end (after the expiration of the last possible wait period) that walks through all the WC random controlled devices and turns them all off about a minute apart. It’s just a little bit of insurance to avoid lights on all night long.