Conditionally turning on lights in the morning

Posted on
Fri Apr 30, 2021 12:00 pm
BigDaddyNC offline
User avatar
Posts: 15
Joined: Sep 25, 2019
Location: Wake Forest, NC

Conditionally turning on lights in the morning

I have a scheduled item that turns on some lights downstairs in the morning when folks come down before school/work. During most of the year, it is dark outside during the scheduled time -- but from about mid-April to mid-August sunrise is early enough that lights aren't needed in the morning. Using the Customize feature associated with sunrise/sunset didn't give me what I wanted because it always triggered, even if limited to a time window. That works for the "off" command, but not for "on". Using the conditions tab gives me the options based on dark or daylight and I suppose that I could use that, but I wanted the precision of a script. Oh, and Daddy Likes to Script.

Right now, I have the scheduler turning on the lights at 6:15am. I estimate that about fifteen minutes before sunrise, it is bright enough downstairs (it faces east) that lights aren't needed. So, when sunrise - 15 minutes happens before 6:15, I don't want to bother turning on the lights. So right now the "adjusted sunrise" is 6:20 - 15 minutes, or 6:05. That is before scheduled time at 6:15, so the script returns a False value and the lights downstairs will not turn on.

Here is a Python script that you can use in the conditional tab in your schedules to help make that determination:

Code: Select all
# IndigoCheckSunrise.py by B. Barretto, 30 Apr 2021
#
# Script to calculate time diff from sunrise so that we can control the lights in the morning
# What we want is to turn on lights only if the time we are running this (schedule time)
# is after (factor) minutes from sunrise.  Basically, if there is enough light outside
# don't bother turning on the lights in the morning, like fifteen minutes before sunrise.
#
# If factor is -15 minutes and sunrise is at 6am, calculated sunrise is 5:45am.  If the schedule time is 6:15am,
# then schedule time is later, so we do not turn on the lights because by this time there should
# be enough light in the sky.
#
# If sunrise is at 7am, then calculated sunrise is 6:45am.  This is after schedule time, so we turn on the lights
# because it should still be too dark outside.

import datetime

# time in minutes relative to sunrise
factor = -15

# calculate adjusted sunrise with factor above
timeItIsBrightEnough = indigo.server.calculateSunrise(indigo.server.getTime().date()) + datetime.timedelta(minutes=factor)

# we're going to use this multiple times, so calculate once and store
rightFrigginNow = datetime.datetime.now()

# take difference between calculated sunrise time and now, result is data type datetime.timedelta
# A negative value calculated means that it is now before the time it is "bright enough"
timeDelta =  rightFrigginNow - timeItIsBrightEnough

# if timeDelta is negative (script execute time is before adjusted sunrise), return boolean True
isBeforeAdjustedSunrise = timeDelta < datetime.timedelta(minutes=0)

# build message
if isBeforeAdjustedSunrise:
   logMsg = "Turn on the lights"
else:
   logMsg = "Do NOT turn on the lights"

indigo.server.log("Now: " + rightFrigginNow.strftime("%H:%M:%S") + ", Target time: " + str(timeItIsBrightEnough) +
   ", calculated diff: " + str(timeDelta) + ", Before calculated sunrise? " + str(isBeforeAdjustedSunrise) + ", " + logMsg)

return isBeforeAdjustedSunrise


Yes, I over-engineered the crap out of this script, because the logic was kind of contorted and I wanted to make it as clear and simple as possible what I was trying to achieve. You can change the value assigned to the factor variable to determine your "adjusted sunrise". I have it at -15, meaning 15 minutes before sunrise -- positive values are after sunrise, and you can use 0 as well.

I'm sure that there are other ways to script this, but here ya go. The conditions tab in schedules will likely accomplish the same thing using "dark" or "daylight" conditions. Comment out the logging statement if you don't want the noise in your log. May someone else find this useful! Please let me know if anyone else is using this.

My setup:
- Mostly Insteon devices, managed by Indigo since version 1.8
- Alexa-Hue bridge plugin for voice commands
- Samsung SmartThings Hub managing garage door openers, door locks, and thermostats
- Custom scripting to report status from Indigo to SmartThings (and subsequently Alexa)
- Custom device drivers to control Insteon devices through Indigo from SmartThings

Posted on
Fri Apr 30, 2021 1:06 pm
jay (support) offline
Site Admin
User avatar
Posts: 18199
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Conditionally turning on lights in the morning

Nice! Thanks for posting.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Apr 30, 2021 1:28 pm
siclark offline
Posts: 1960
Joined: Jun 13, 2017
Location: UK

Re: Conditionally turning on lights in the morning

Nice. Can’t wait to try this. I’ve struggled with this problem for years

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests

cron