Simulated Sunrise

Posted on
Sat Mar 09, 2019 7:52 am
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Simulated Sunrise

Any way to convert this to Python?

It is used to simulate sunrise ( slowly fading up lights ) along with a seperateiTunes Fade-up script for an "alarm clock" in the morning.

Code: Select all
tell application "IndigoServer"
   set deviceName to "Master Bedroom Keypad - B"
   set maximumLightPercentage to 60
   set minutesToSunrise to 30
   
   -- This math is importaint as it sets the number of repeats below
   set raiseLightsBy to round (maximumLightPercentage / minutesToSunrise / 2)
   
   log "Starting to raise the lights by " & raiseLightsBy & "% every 30 seconds over the next " & minutesToSunrise & " minutes to achive a final brightness of " & maximumLightPercentage
   
   repeat minutesToSunrise * 2 times
      -- Turn up the brightness of the device one increment
      brighten deviceName by raiseLightsBy
      
      -- Set a variable with the current brightness of our device so we can log it below
      set currentBrightness to brightness of device deviceName as integer
      
      log "Sunrise Simulator raised " & deviceName & " to " & currentBrightness & "%"
      
      -- Since the parameter minutesToSunrise is in minutes, delay one minute
      -- but loop every 30 seconds to give it a more even increase in
      -- brightening. Note the "minutesToSunrise / 2" and "repeat minutesToSunrise * 2 times"
      -- above to account for the half minute increment.
      delay 30
   end repeat
   
   -- In case there is a rounding error above, set it to the target percentage
   brighten deviceName to maximumLightPercentage
end tell


Thanks!

--
Korey

Posted on
Sat Mar 09, 2019 11:58 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: Simulated Sunrise

This is the direct translation to python, but if you put this into a script it will timeout.
Max time a script can run is 10 secs and here it requires several minutes.
--- If you run this every day your system should have serious troubles having applescript running that long .

Code: Select all
import time
deviceName = "Master Bedroom Keypad - B"
maximumLightPercentage = 60
minutesToSunrise = 30
   
#   This math is importaint as it sets the number of repeats below
raiseLightsBy = int(maximumLightPercentage / minutesToSunrise / 2)
   
indigo.server.log("Starting to raise the lights by " +str(raiseLightsBy)+"% every 30 seconds over the next "+str(minutesToSunrise)+ " minutes to achive a final brightness of " +str(maximumLightPercentage) )
   
dev = indigo.devices[deviceName]
for ii in range(minutesToSunrise * 2):
#   Turn up the brightness of the device one increment
   currentBightness = dev.brightness
   indigo.dimmer.brighten(deviceName, by=raiseLightsBy, delay=4)
 
#   Set a variable with the current brightness of our device so we can log it below
   currentBightness = dev.brightness
   indigo.server.log("Sunrise Simulator raised " + deviceName + " to " +str(currentBrightness)+ "%"

#   Since the parameter minutesToSunrise is in minutes, delay one minute
#   but loop every 30 seconds to give it a more even increase in
#   brightening. Note the "minutesToSunrise / 2" and "repeat minutesToSunrise * 2 times"
#   above to account for the half minute increment.
   time.sleep(30)

#   In case there is a rounding error above, set it to the target percentage
indigo.dimmer.setBrightness(deviceName ,maximumLightPercentage)

So we have to go a different path.

add an action group
1. make a small action script
Code: Select all
if indigo.devices["Master Bedroom Keypad - B"].brightness ==100:
     ###  cancel this action group ...  have not found how to invoke cancel action group.  ##
    return
maximumLightPercentage = 60
minutesToSunrise = 30
raiseLightsBy = int(maximumLightPercentage / minutesToSunrise / 2)
indigo.dimmer.brighten("Master Bedroom Keypad - B", by=raiseLightsBy, delay=4)

1.1 add 2. action in that action group to call itself with delay of 2 minutes

2. create a schedule to run at sunrise - 30 minutes
call above action

something like that . and there are likely many other ways to do this ..


Karl

Posted on
Sat Mar 09, 2019 12:00 pm
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Simulated Sunrise

Thanks Karl, I'll have a go at it! :D

--
Korey

Posted on
Sat Mar 09, 2019 12:18 pm
jay (support) offline
Site Admin
User avatar
Posts: 18219
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Simulated Sunrise

Untested, but should be close (you'll definitely need to run this as an external script since it does a lot of sleeping):

Code: Select all
import time

device = indigo.devices[IDOFDEVICE] # the id of "Master Bedroom Keypad - B"
maximum_light_percentage = 60
minutes_to_sunrise = 30

# This math is importaint as it sets the number of repeats below
raise_lights_by = int(round(maximum_light_percentage / minutes_to_sunrise / 2))
indigo.server.log(
    "Starting to raise the lights by {}% every 30 seconds over the next {} minutes to achive a final brightness of {}".format(
        raise_lights_by, minutes_to_sunrise, maximum_light_percentage
    )
)

for i in range(minutes_to_sunrise * 2):
    # Turn up the brightness of the device one increment
    indigo.device.brighten(device, by=raise_lights_by)
   
    # Copying directly from the script, but since this value is only used in logging
    # and it requires a round trip to the server, one optimization would be to skip it
    # and not log the actual value. You could just log by how much it was raised (raise_lights_by)
    device.refreshFromServer()
    indigo.server.log("Sunrise Simulator raised {} to {}%".format(device.name, device.brightness))
    # Here's an alternate log line you could use that would still give some information, but would
    # allow you to skip the round-trip to the server caused by the device.refreshFromServer() call above
    #indigo.server.log("Sunrise Simulator raised {} by {}%".format(device.name, raise_lights_by))
   
    # Since the parameter minutesToSunrise is in minutes, delay one minute
    # but loop every 30 seconds to give it a more even increase in
    # brightening. Note the "minutesToSunrise / 2" and "repeat minutesToSunrise * 2 times"
    # above to account for the half minute increment.
    time.sleep(30)

# In case there is a rounding error above, set it to the target percentage
indigo.device.setBrightness(device, value=maximum_light_percentage)


It's pretty much a direct port of your script, but I did point out one optimization in the comments.

[EDIT] LOL, karl beat me to it.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Mar 09, 2019 4:15 pm
durosity offline
User avatar
Posts: 4320
Joined: May 10, 2012
Location: Newcastle Upon Tyne, Ye Ol' England.

Re: Simulated Sunrise

Mebbe this would be a good idea for a plugin! I’d have a crack at it but time isn’t my friend at the moment. I’d have thought it’d be fairly simple for an experienced developer though..

Computer says no.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests