I wrote a simple piece of code to allow your lights to behave as a dawn simulator clock. To use this, create a new scene, and paste the code below in to the luup tab. Change the device number(s) at the top of the file to the device number you want to dim up, and optionally change the length of the dawn cycle. The default is 40 minutes. The light will then turn up over that period, following a sigmoid pattern. You can set the start and end level for the dim, so, for example, it can run from 10 to 100%, or it can dim too if you want. You can also set the minimum time between messages to prevent overloading the switch - that defaults to 30 seconds. If you have more than one device that you want to run together, separate their IDs with a comma.
Set a timer for the scene to wake you up each day. Donāt forget that if you want to wake up at 7am and the cycle is set for 40 minutes, set the timer for 6:20am.
Also, if you feel lazy and donāt want the lights to continue, just turn them off and it will stop the cycle.
Anyway, hereās the code. Let me know how it goes for you.
Martin
[code]do
ā the numbers of the devices to control
local device_numbers={50,48}
ā the number of minutes to run the cycle over
local cycle_minutes=45
ā the starting level for the cycle
local start_level=5
ā the ending level for the cycle
local end_level=50
ā minimum time between steps in seconds. To avoid too frequent updates
local minimum_time=30
local step_increment=math.ceil(minimum_time4/(cycle_minutes3))
local seconds_delay=step_increment * math.ceil(cycle_minutes*0.75)
local dimmingUp=true
local onDetected=false
function setLevel(percent)
-- convert the percentage through to a number
local percentNum=tonumber(percent)
-- check if the first light is on - abort if someone has turned it off when rising. Give it a couple of cycles to check
local lul_tmp = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", device_numbers[1])
if (onDetected and dimmingUp and lul_tmp == "0" ) then
luup.log("SUNRISE: device is off, so quitting")
return
end
-- calculate the dim level based on a sinusoidal curve
local dimLevel=start_level+ math.ceil((end_level-start_level)/(1+math.pow(2.71828183,((50-percentNum)*12/100) ) ))
luup.log("SUNRISE: percent="..percent..", dim level="..dimLevel)
local currentLevelNum=0
local currentLevel=""
for i,device_number in pairs(device_numbers) do
currentLevel=luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelStatus", device_number)
currentLevelNum=tonumber(currentLevel)
if( (dimmingUp and currentLevelNum<dimLevel) or (not dimmingUp and currentLevelNum>dimLevel)) then
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = dimLevel}, device_number)
else
if( dimmingUp and currentLevelNum>= end_level) then
-- the light is already beyond the final level, so stop
luup.log("SUNRISE: light already up to "..currentLevelNum.." out of "..end_level..". Quitting")
return
end
if( currentLevelNum<=end_level and not dimmingUp) then
-- the light is already beyond the final level, so stop
luup.log("SUNRISE: light already down to "..currentLevelNum.." out of "..end_level..". Quitting")
return
end
end
if(not onDetected) then
local currentLevel = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", device_number)
if(currentLevel=="1") then
onDetected=true
luup.log("SUNRISE: setting onDetected to true")
end
end
end
-- keep going until the dim level reaches the final level
if( (dimmingUp and dimLevel<end_level) or (not dimmingUp and dimLevel>end_level) ) then
percentNum=percentNum+step_increment;
luup.call_delay('setLevel', seconds_delay , percentNum,true)
end
end
local device_id_list="";
if (end_level<start_level) then
dimmingUp=false
end
for i,device_number in pairs(device_numbers) do
device_id_list=device_id_listā¦device_numberā¦ā,ā
luup.call_action(āurn:upnp-org:serviceId:Dimming1ā, āSetLoadLevelTargetā, {newLoadlevelTarget = start_level}, device_number)
end
luup.log("SUNRISE: starting for devices "..device_id_list.." starting from "..start_level.." going to "..end_level.." in "..(80/step_increment).." steps in "..cycle_minutes.." minutes, stepping every ".. seconds_delay.. " seconds.")
setLevel(10)
end[/code]