on (dim) switch with motion sensor

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

  1. aeon 4 -1 sensor
  2. aeon dimmer micro switch

Conditions:

  1. If the light is on - do nothing
  2. 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.

  1. 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.
  2. 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?)

Did you ever find a satisfactory fix for the switch remembering it’s brightness level? At the present, when my scene turns the light off, I just turn the like to 100% for a second and then shut it off. I don’t like doing this but I don’t know how else to fix it.

I kind of have the same setup, 4-1 sensor with the fibro dimmer and a 3 way retractive switch

Scene is trigged with the PIR being tripped then the luup code checks to see if the light is on, if so it will then check the level of light, if it is less than 20% it will turn the light of (30% if its night time)

have also found it will remember the 30% setting but all you need to do it double click the switch and it will go to 100%

Is this the same for you?

That’s the exact same as what I have been doing. The double click isn’t hard… it’s more of a small hurdle that ticks you off knowing you can’t figure out how to fix it :wink: