Dawn Simulator Clock

if the outlet has dimming capabilites i don’t see why not. And of course the lamp has to be dimmable.

The code had many syntax errors due to forum display formatting
Here it is fixed:-

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_time/(cycle_minutes))
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
2 Likes

Thanks ElCid. The formatting in my original message seems to have not survived the transition to the new forums.