P-a, Sorry for the late response but we were offline for several days due to Irma. Here is some code we use in our parrot rooms to slowly dim the lights from 30 minutes before sunset through 9pm (their bedtime). You can modify the code for any time interval you need, but the core requirements are there. In addition, we wanted to be able to lower or raise the light level in the midst of the code and have the dim ramp use the current level and continue on. One last note - I placed this code in a PLEG action so that it can recover from a Lua restart and continue. I originally had it as a scene but that was an issue because a lot of my coding work occurred while that particular scene was active, resulting in lights frozen at a particular level and no way to ‘restart’ the scene. Please feel free to strip out the SceneWatch code as this is a custom variable that I use to indicate the status of the code as it progresses and for troubleshooting. I left it in place so you could see how things worked.
luup.log("PL_Act: c_BirdRm_Lights_Dim")
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable1"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable2"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable3"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable4"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable5"," ",50)
varDevice1 = 52
varDevice2 = 53
varMax = 70
varMin = 10
varDimStep = (varMax-varMin)/10
varDimLevel1 = varMax
varDimLevel2 = varMax
varLupSS = luup.sunset()
varLuaSST = os.date("*t", varLupSS)
varSunsetTime = varLuaSST.hour * 60 + varLuaSST.min
varSSTHr = math.floor(varSunsetTime/60)
varSSTMin = varSunsetTime-(varSSTHr*60)
varSST = varSSTHr .. ":" .. varSSTMin
varSSoffset = 30
varStartTime = varSunsetTime - varSSoffset
varSTHr = math.floor(varStartTime/60)
varSTMin = varStartTime-(varSTHr*60)
varST = varSTHr .. ":" .. varSTMin
varEndTime = "21:00"
varETHr = tonumber( varEndTime:sub( varEndTime:find("%d+") ) )
varETMin = tonumber(varEndTime:sub(-2))
if varETHr and varETMin then
varEndTime = varETHr * 60 + varETMin
else
luup.log("ERROR: invalid end time")
return false
end
varDelayPeriod = ((varEndTime - varStartTime)*6)
varCurTime = os.date("*t")
varCurrentTime = varCurTime.hour * 60 + varCurTime.min
varHumanTime = os.date("%m/%d/%Y %H:%M:%S")
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName1","Sunset",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable1",varSST,50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName2","Start Time",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable2",varST,50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName3","Dim Delay",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable3",varDelayPeriod/60,50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName4","Dim Step",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable4",varDimStep,50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName5","First Step",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable5",varHumanTime,50)
function DimDelay()
varDimLevel1 = luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",varDevice1)
varDimLevel1 = varDimLevel1 - varDimStep
luup.call_action("urn:upnp-org:serviceId:Dimming1","SetLoadLevelTarget",{ newLoadlevelTarget=varDimLevel1 },varDevice1)
varDimLevel2 = luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",varDevice2)
varDimLevel2 = varDimLevel2 - varDimStep
luup.call_action("urn:upnp-org:serviceId:Dimming1","SetLoadLevelTarget",{ newLoadlevelTarget=varDimLevel2 },varDevice2)
varActionTime = os.date("%m/%d/%Y %H:%M:%S")
luup.log("PL_Act: c_BirdRm_Lights_Dim")
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName5","Last Action",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable5",varActionTime,50)
if (varDimLevel1 >= varMin) or (varDimLevel2 >= varMin) then
luup.call_timer("DimDelay", 1 , varDelayPeriod,"","")
end
end
-- if the current time is in the given interval, run the scene.
if varStartTime <= varCurrentTime and varCurrentTime <= varEndTime then
if (varDimLevel1 > 0 or varDimLevel2 > 0) then
luup.call_timer("DimDelay",1,varDelayPeriod,"","")
return true
end
else
return false
end