script for auto setting the dim values

Posted on
Sun Apr 11, 2004 9:13 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

script for auto setting the dim values

I wanted my motion detectors to turn on my lights at night however I didn't want them to be on full brightness depending on the time of night. I wrote this attachment script that calculates the dim level based on what the current time is in relation to the sunset and sunrise.

--[url=applescript://com.apple.scripteditor/?action=new&script=using%20terms%20from%20application%20%22Indigo%22%0A%09on%20DimADevice(DeviceName)%0A%09%09tell%20application%20%22Indigo%22%0A%09%09%09set%20EarlyCurrentDate%20to%20(date%20string%20of%20(current%20date))%20%26%20%22%2012%3A00%3A00%20AM%22%0A%09%09%09set%20CurrentDateSunrise%20to%20(calculate%20sunrise%20for%20date%20EarlyCurrentDate)%0A%09%09%09set%20CurrentDateSunset%20to%20(calculate%20sunset%20for%20date%20EarlyCurrentDate)%0A%09%09%09if%20(current%20date)%20%3E%20CurrentDateSunrise%20and%20(current%20date)%20%3C%20CurrentDateSunset%20then%0A%09%09%09%09--It's%20daytime%20so%20lights%20are%20at%20100%25%0A%09%09%09%09set%20DimPercent%20to%20100%0A%09%09%09else%0A%09%09%09%09if%20the%20day%20of%20(current%20date)%20is%20the%20day%20of%20(calculate%20sunset%20for%20(current%20date))%20and%20(current%20date)%20%3E%20(calculate%20sunrise%20for%20(current%20date))%20then%0A%09%09%09%09%09set%20theSunset%20to%20calculate%20sunset%20for%20(current%20date)%0A%09%09%09%09%09set%20theSunrise%20to%20calculate%20sunrise%20for%20((current%20date)%20+%20(1%20*%20days))%0A%09%09%09%09else%0A%09%09%09%09%09set%20theSunset%20to%20calculate%20sunset%20for%20((current%20date)%20-%20(1%20*%20days))%0A%09%09%09%09%09set%20theSunrise%20to%20calculate%20sunrise%20for%20(current%20date)%0A%09%09%09%09end%20if%0A%09%09%09%09set%20Totalsecs%20to%20theSunrise%20-%20theSunset%0A%09%09%09%09set%20CurrentSecs%20to%20theSunrise%20-%20(current%20date)%0A%09%09%09%09set%20DimPercent%20to%20(CurrentSecs%20%2F%20Totalsecs)%20*%20100%0A%09%09%09%09if%20DimPercent%20%3C%2010%20then%20DimPercent%20%3D%2010%0A%09%09%09end%20if%0A%09%09%09if%20(on%20state%20of%20device%20DeviceName)%20is%20false%20and%20(value%20of%20variable%20%22isDaylight%22%20as%20boolean)%20is%20false%20then%20brighten%20DeviceName%20to%20(DimPercent%20as%20integer)%0A%09%09end%20tell%0A%09end%20DimADevice%0Aend%20using%20terms%20from]Click here to open this script in a new Script Editor window[/url].

using terms from application "Indigo"
     on DimADevice(DeviceName)
          tell application "Indigo"
               set EarlyCurrentDate to (date string of (current date)) & " 12:00:00 AM"
               set CurrentDateSunrise to (calculate sunrise for date EarlyCurrentDate)
               set CurrentDateSunset to (calculate sunset for date EarlyCurrentDate)
               if (current date) > CurrentDateSunrise and (current date) < CurrentDateSunset then
                    --It's daytime so lights are at 100%
                    set DimPercent to 100
               else
                    if the day of (current date) is the day of (calculate sunset for (current date)) and (current date) > (calculate sunrise for (current date)) then
                         set theSunset to calculate sunset for (current date)
                         set theSunrise to calculate sunrise for ((current date) + (1 * days))
                    else
                         set theSunset to calculate sunset for ((current date) - (1 * days))
                         set theSunrise to calculate sunrise for (current date)
                    end if
                    set Totalsecs to theSunrise - theSunset
                    set CurrentSecs to theSunrise - (current date)
                    set DimPercent to (CurrentSecs / Totalsecs) * 100
                    if DimPercent < 10 then DimPercent = 10
               end if
               if (on state of device DeviceName) is false and (value of variable "isDaylight" as boolean) is false then brighten DeviceName to (DimPercent as integer)
          end tell
     end DimADevice
end using terms from

Posted on
Mon Apr 12, 2004 9:30 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: script for auto setting the dim values

Morning Greg,

Neat script -- thanks for posting it. Although not as sophisticated as what you've done in that script, I have been thinking about adding the "slow dim" action type to Indigo. This would let you dim or brighten up a device over several minutes.

Matt

Posted on
Mon Apr 12, 2004 11:27 am
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

That would be useful. Then I could set the light to slowly dim up, better then a full blast especially at 3AM.

Posted on
Mon Apr 19, 2004 12:19 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

I've slightly updated the script to make sure that the device is dimmable. If it's not then it just turns the light on. By the way, this seems to be a hit with the family. I think they like getting up in the morning and not be blasted by light in the bathroom!

--[url=applescript://com.apple.scripteditor/?action=new&script=using%20terms%20from%20application%20%22Indigo%22%0A%09on%20DimADevice(DeviceName)%0A%09%09tell%20application%20%22Indigo%22%0A%09%09%09set%20EarlyCurrentDate%20to%20(date%20string%20of%20(current%20date))%20%26%20%22%2012%3A00%3A00%20AM%22%0A%09%09%09set%20CurrentDateSunrise%20to%20(calculate%20sunrise%20for%20date%20EarlyCurrentDate)%0A%09%09%09set%20CurrentDateSunset%20to%20(calculate%20sunset%20for%20date%20EarlyCurrentDate)%0A%09%09%09if%20(current%20date)%20%3E%20CurrentDateSunrise%20and%20(current%20date)%20%3C%20CurrentDateSunset%20then%0A%09%09%09%09--It's%20daytime%20so%20lights%20are%20at%20100%25%0A%09%09%09%09set%20DimPercent%20to%20100%0A%09%09%09else%0A%09%09%09%09if%20the%20day%20of%20(current%20date)%20is%20the%20(day%20of%20(calculate%20sunset%20for%20(current%20date)))%20and%20(current%20date)%20%3E%20(calculate%20sunrise%20for%20(current%20date))%20then%0A%09%09%09%09%09set%20theSunset%20to%20(calculate%20sunset%20for%20(current%20date))%0A%09%09%09%09%09set%20theSunrise%20to%20calculate%20sunrise%20for%20((current%20date)%20+%20(1%20*%20days))%0A%09%09%09%09else%0A%09%09%09%09%09set%20theSunset%20to%20(calculate%20sunset%20for%20((current%20date)%20-%20(1%20*%20days)))%0A%09%09%09%09%09set%20theSunrise%20to%20calculate%20sunrise%20for%20(current%20date)%0A%09%09%09%09end%20if%0A%09%09%09%09set%20Totalsecs%20to%20theSunrise%20-%20theSunset%0A%09%09%09%09set%20CurrentSecs%20to%20theSunrise%20-%20(current%20date)%0A%09%09%09%09set%20DimPercent%20to%20(CurrentSecs%20%2F%20Totalsecs)%20*%20100%0A%09%09%09%09if%20DimPercent%20%3C%2010%20then%20DimPercent%20%3D%2010%0A%09%09%09end%20if%0A%09%09%09log%20DimPercent%0A%09%09%09if%20(on%20state%20of%20device%20DeviceName)%20is%20false%20and%20(value%20of%20variable%20%22isDaylight%22%20as%20boolean)%20is%20false%20and%20(supports%20dimming%20of%20device%20DeviceName)%20is%20true%20then%20brighten%20DeviceName%20to%20(DimPercent%20as%20integer)%0A%09%09%09if%20(on%20state%20of%20device%20DeviceName)%20is%20false%20and%20(value%20of%20variable%20%22isDaylight%22%20as%20boolean)%20is%20false%20and%20(supports%20dimming%20of%20device%20DeviceName)%20is%20false%20then%20turn%20on%20DeviceName%0A%09%09end%20tell%0A%09end%20DimADevice%0Aend%20using%20terms%20from]Click here to open this script in a new Script Editor window[/url].

using terms from application "Indigo"
     on DimADevice(DeviceName)
          tell application "Indigo"
               set EarlyCurrentDate to (date string of (current date)) & " 12:00:00 AM"
               set CurrentDateSunrise to (calculate sunrise for date EarlyCurrentDate)
               set CurrentDateSunset to (calculate sunset for date EarlyCurrentDate)
               if (current date) > CurrentDateSunrise and (current date) < CurrentDateSunset then
                    --It's daytime so lights are at 100%
                    set DimPercent to 100
               else
                    if the day of (current date) is the (day of (calculate sunset for (current date))) and (current date) > (calculate sunrise for (current date)) then
                         set theSunset to (calculate sunset for (current date))
                         set theSunrise to calculate sunrise for ((current date) + (1 * days))
                    else
                         set theSunset to (calculate sunset for ((current date) - (1 * days)))
                         set theSunrise to calculate sunrise for (current date)
                    end if
                    set Totalsecs to theSunrise - theSunset
                    set CurrentSecs to theSunrise - (current date)
                    set DimPercent to (CurrentSecs / Totalsecs) * 100
                    if DimPercent < 10 then DimPercent = 10
               end if
               log DimPercent
               if (on state of device DeviceName) is false and (value of variable "isDaylight" as boolean) is false and (supports dimming of device DeviceName) is true then brighten DeviceName to (DimPercent as integer)
               if (on state of device DeviceName) is false and (value of variable "isDaylight" as boolean) is false and (supports dimming of device DeviceName) is false then turn on DeviceName
          end tell
     end DimADevice
end using terms from

[enter your name here]

-------------------------
[This script was automatically tagged for color coded syntax by Script to Markup Code]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 33 guests