Room Lighting with Variable Periods

I had a lot of difficulty getting room lighting with variable periods to work but eventually did and, having not found too much written about this on the forums, thought I would share what finally worked for me. I wanted a “subMode”; one particular lamp to work differently during the first 30 minutes of entering my bedtime mode. I could have done this with another mode or separate room lighting instances (with delays) or other combinations of hub variables and RL instances, but for such a minor tweak in behavior affecting only 1 lamp, periods with Hub Variables as the start times seemed like a made-to-order solution and I stubbornly stuck with it until I got it to work.

There are two realizations required to make this work, especially in light of edge cases; 1. As has been touched upon elsewhere in these forums, periods are determined by scheduled start times but there is no notion of a schedule or being “between periods”.This becomes especially apparent when you use variables. For example, if your periods are set to start at 8:00am and 9:30pm but at 9:35pm, you change the 9:30 period2 start time to 10:00pm, you will not automatically re-enter period1 (even though it is now 9:35pm and your start times are 8:00am and 10:00pm). 2. I got a lot of non-understandable (buggy) behavior if my variables had both date and time; everything seemed to start working much more deterministically when I made sure that they had only times and no date.

The reason I need variables is that my bedtime mode is not entered at a specific time but rather when I turn off the TV after a certain time at night. I want this to trigger a “Sleep” Period to start 30 minutes later, but before then, I want lights controlled by a “PreSleep” period. I have two hub variables, PreSleepStartTime and PreSleepEndTime. The starttime can normally be set to something arbitrarily early (but well after “sleep”/darkness times), e.g. 9:00am. The end time can shift around, from 9:45pm to midnight or later. If after setting it, you just leave it, you run the risk of prematurely entering “Sleep” period the following day. So, it is important to set it back to something arbitrarily late (e.g. 1:00am) after it has been triggered. I did this by setting it back to 1:00am immediately after setting it to 30 minutes from now but with a delay of 31 minutes (thus,once we enter period2, it resets for tomorrow).

I alluded to the main edge case above. If I enter my sleep period but then turn my TV back on and exit my BedTime Mode, I would like the process to start all over again when I re-enter bedtime mode. The key is that I needed to set period1 to start “now” instead of 9:00 am. So, to do this, I set it to “now” + 1 minute and then I set it back to 9:00am with a delay of 2 minutes (just to make sure that tomorrow, I am definitely in “preSleep” period, no matter how early I start my Sleep period. There are a variety of ways I could have detected this edge case but I won’t get into which I chose.

So, to summarize; 1. Use time-only variables with no dates and 2. When variable period start times change causing you to change the period you should be in, manipulate the variables to ensure you re-enter what the desired current period and reset that variable back to its default with a delay set to occur right after you re-enter the desired current period. It sounds complicated, but it’s actually fairly simple once you realize what’s necessary but still admittedly and annoyingly complicated for an edge case. However, in my case, it’s a realistic one and I tripped up against it almost immediately while manipulating the variables for testing.

One other thing that I discovered is almost certainly a bug that only manifested with period-based Room Lighting instances. My room lighting has a condition to only activate for bedtime mode. Unfortunately, the only way RL lets you do that is with an exclusion list of all the modes other than my bedtime mode. When I added a new mode, I remembered that I had to go add it to the exclusion list but it did not appear on the list of modes to choose from! I had to change my RL to not use periods before it would appear and then when I added periods back, I had to fix all the periods to check/uncheck the activate/off boxes that I want for each period.