Hello,
Let me start off by saying Im a newbie. Here is a script that will turn on your lights (dim to 35%) for 30 sec if it detects motion.
This is the scenerio. At night you want to go grab a glass of water . All lights are off. You walk out to your hallway and the lights are Iluminated to 35 % (enough to light the way and not bright enough to blind you) you go to the kitchen same thing happens. If you flip the switch the light goes on 100%.
I am using a
- aeon 4 -1 sensor
- aeon dimmer micro switch
Conditions:
- If the light is on - do nothing
- Only works during night time (aeon light sensor detects amount of light).
Any and every suggestions comments are welcome. Like I said Im a newbie, this is actually my 1 lua script and still have a lot of doubts of how everything is handled in vera (variables, interrupts , etc). I am still not sure if I am indenting or ending the if and functions correctly, also I dont know if the “return true” is necessary or even correctly used.
Please feel free to use it and make tips on how to improve it.
In the scene setup all I did was add new scene , added a motion trigger and added this script.
[code]–SCRIPT
local DELAY = 30 – time the light remains on
local LOW_LEVEL = 250 – the light level threshold for night
local LIGHT_LEVEL = 35 – Dimmer load level threshold
local DEVICE_NO_SENS = 36 – the light sensor device number
local DEVICE_NO_DIM = 53 – the dimmer device number
local LS_SID = “urn:micasaverde-com:serviceId:LightSensor1” – the LightSensor service ID
local DIM_SID = “urn:upnp-org:serviceId:Dimming1” – the Dimmer service ID
local currentLevel = luup.variable_get (LS_SID, “CurrentLevel”, DEVICE_NO_SENS) or 0
local lightLevel = luup.variable_get(DIM_SID, “LoadLevelStatus”, DEVICE_NO_DIM) or 0
currentLevel = tonumber(currentLevel)
lightLevel = tonumber(lightLevel)
if (currentLevel <= LOW_LEVEL and lightLevel < LIGHT_LEVEL) then
luup.call_action(DIM_SID,“SetLoadLevelTarget”,{ newLoadlevelTarget=LIGHT_LEVEL }, DEVICE_NO_DIM)
luup.call_delay( ‘switch_off’, DELAY) – Call the switch off function after DELAY seconds
function switch_off()
lightLevel = luup.variable_get(DIM_SID, “LoadLevelStatus”, DEVICE_NO_DIM) or 0
lightLevel = tonumber(lightLevel)
if (lightLevel == LIGHT_LEVEL) then
luup.call_action(DIM_SID,“SetLoadLevelTarget”,{ newLoadlevelTarget=“100” },DEVICE_NO_DIM)
luup.sleep(100)
luup.call_action(DIM_SID,“SetLoadLevelTarget”,{ newLoadlevelTarget=“0” },DEVICE_NO_DIM)
return true
else
return false
end
end
else
return false
end
– END SCRIPT[/code]
I added the luup.call_action to target level 100 before turning it of so that if the switch is flipped it will go to 100 instead of 35.
This only works after the script has finished. I am still looking for a way to make it work while the script is in the delay.
Known problems.
- One problem I am having is. If I flip the switch it will turn on to the last known brightness level, which after running this is 35%. I want it to turn on full 100. I have a couple of workarounds but nothing satisfactory.
- I need more testing but I am having trouble when I created 2 scenes with this same script. One for the hallway one for the kitchen. Both run but the first one to run will not turn off. (if I change the name of the function switch_off on one of them it works correctly, why is this?)