Need Help with Dimmer Switch and Motion Sensor

I need some help and advice on how to perform the following: I want my upstairs hallway lights to turn to 20% whenever motion is detected from the motion sensor and to turn off after 30 seconds. The problem is then whenever the next person flips the switch on the wall the lights are still at 20%. I have tried setting a scene to set the lights to 100% and 1 sec later to turn them off but then you get a flash of light in the middle of the night. I have tried using the Smart Switch Plugin but it didn’t work correctly and I have also tried looking into SetOnEffectLevel to set the lights to 100% but not actually turning them on but it doesn’t appear to work with my Aeon Labs micro switch. Can anyone help me figure out the best options or if there is any luup code to help perform this? Thank You

I tried creating a scene that has a trigger of the motion sensor being tripped and this luup code. I am new to luup so I dont know if this is the best way to solve my problem or not. The funny thing is if the motion sensor trips the scene the light level never gets set to 100%. If I manually run the scene everything works great and the 100ms doesn’t give the lights enough time to ramp up to 100% so the don’t flash at full power before they are turned off.

luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “20”}, 44)
luup.sleep(30000)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, 44)
luup.sleep(100)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “0”}, 44)

Using a luup.sleep of 30 seconds in a scene can cause problems. Vera is likely to consider your scene has stalled and restart the Luup engine.

The following modified version of your code uses luup.call_delay(…) to get a call-back to the function resetLLT after 30 seconds. This is the recommended way to handle delays in Luup code.

[code]luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “20”}, 44)
luup.call_delay(“resetLLT”,30,“”)

function resetLLT()
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, 44)
luup.sleep(100)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “0”}, 44)
end[/code]

Thank You so much RexBeckett that worked perfectly! Can you help me add in the following two statements? One would be to only have this run during the night so how would I write the if statement to skip it all if its day? The second would be how do I write the if statement that if the lights are already turned on (by the light switch for example) and motion is detected that it just skips setting it to 20% and skips over everything? Thanks again so much!

Your now getting into pleg territory. It might be easier to do some of those checks with pleg.

  • Garrett

@Garrett is right: I see PLEG in your future…

Right now you can do what you want like this:

[code]local status = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,44)
if (not luup.is_night()) or (status == “1”) then return false end
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “20”}, 44)
luup.call_delay(“resetLLT”,30,“”)

function resetLLT()
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, 44)
luup.sleep(100)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “0”}, 44)
end[/code]

Did you see this thread?

Thank You so much RexBeckett that final code worked perfectly!