Jump to content
  • 0

Motion activated lights help wanted


Question

Helle Everyone, hope someone can help me out here.

I have this simple scene below running to turn on and off the lights in a room (off after 15 minutes)

It works perfect, however, problems start when turning the light off with the physical light switch (pulse)

If I do that, the light goes out (of course) but this scene keeps running and won't turn on the light if I enter the room again in 5 minutes.

 

Ideal situation (in my opinion) , to kill this scene when the light is switched of but I have no success yet.... hope someone can help me with that.

 

Greeting,

 

Vincent.

 

--[[
%% autostart
%% properties
505 value
%% weather
%% events
%% globals
--]]


if (fibaro:countScenes()>1) then fibaro:abort() end -- Run only one scene

local light_1 = 513 -- ID Lamp 1
--local light_2 = 78 -- ID lamp 2
local sensor_1 = 505 -- Mov. sensor 1
--local sensor_2 = 86 -- Mov. sensor 2
local timer = 15 -- Timer in minutes
local counter = timer


local counter = timer

if 
  
(tonumber(fibaro:getValue(sensor_1, "value")) > 0 )  

then

  
  fibaro:debug("Turn on lights")
  --fibaro:call(light_1, "turnOn"--)
  fibaro:call(light_1, "turnOn")
  --fibaro:call(light_2, "turnOn")


  
  while counter > 0
  do
    counter = counter -1
    fibaro:sleep(60*1000)
    fibaro:debug("Counter = ".. counter)
    if
    tonumber(fibaro:getValue(sensor_1, "value")) > 0 --or tonumber(fibaro:getValue(sensor_2, "value")) > 0
    then
      counter = timer
    end
  end


  
  fibaro:debug("Turn off lights")
  fibaro:call(light_1, "turnOff")
  --fibaro:call(light_2, "turnOff")--
end

Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0

First, You have to dicide what will be the triggering event to reset/start coundown.

I sugest to consider:

-motion sensor (yes Its the main thing we started the scene writing)

-device itself status (turned on/off by switch, or by :setValue()

-manual running (means reset/start new counter with running the scene from other scene (for example, door sensor scene may include pushing the light on (in some cases))

 

8 hours ago, Vinisz said:

fibaro:sleep(60*1000)
if tonumber(fibaro:getValue(sensor_1, "value")) > 0 then

What if sensor was briched and unbriched during this 60 seconds? After sleep() Value will be 0? And this way - motion will NOT reset the counter as it should

 

Now about this:

8 hours ago, Vinisz said:

if (fibaro:countScenes()>1) then fibaro:abort() end -- Run only one scene

Look what will happen: motion even triggered the scene, it started counting. And now we need to reset the counter with manual scene running. This will be also ignored and no counter reset happen.

 

Consider also, if u include light device value in triggers, changing device state inside scene loop will also cause new scene instance.

 

 

Edited by jack.daniels
Link to post
Share on other sites
  • 0

This works for me! If you want longer time with light on then edit the variable starttimer. The time is in seconds.

 

Regards

 

 

 

--[[ 
%% properties 
38 value
51 value
%% globals 
--]]


if (fibaro:countScenes()>1) then fibaro:abort(); end

 

function debug(color, message)
    fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
end

 

-- VARIABELS
local scene = 66 -- ID This scene
local motion = 38 -- ID for PIR
local switch = 51 -- ID Contact
local starttimer = 50; -- Time for delay
local timer = (starttimer); -- Timer in scene

 

-- Turn on and OFF
if (tonumber(fibaro:getValue(motion, "value")) > 0 ) and (tonumber(fibaro:getValue(switch, "value")) < 1) then -- Movement?

    fibaro:call(switch, "turnOn");

    debug("green", "Light on");

end
  
-- Start loop   
 repeat 
 timer=timer-1; 
 fibaro:sleep(1000);
    
     -- If movement start time again
     if (tonumber(fibaro:getValue(motion, "value"))) > 0 then 
         timer=starttimer; 
         debug("grey", "Movement. Time started again");
     end 
 
    -- Turn off if switch is used
    if (tonumber(fibaro:getValue(switch, "value"))) < 1 then 
        debug("red", "Manual push on switch - program terminated");
        timer=0
    end 
 until (timer<1) 

 

-- Turn off light 
fibaro:call(switch, "turnOff");
debug("red", "Light off");

Edited by Thomasn
Link to post
Share on other sites
  • 0

Hi,

 

This works good for me. You can use door switch and PIR  to trigger the light. 53-motion sensor, 77 door sensor in my case

 

--[[
%% properties
53 value

77 value
95 value
%% globals
--]]
------------------------------------
------------ Settings --------------
------------------------------------
 
-- Sensor IDs, separated with commas
local sensors = {
    53, 77
}
-- Light IDs, separated with commas
local lights = {
    95
}
local lux = 55; -- Lux sensor ID
local delay = 90; -- delay in seconds, how long after LAST DETECTED MOTION should the lights stay on
local scene = 33; -- ID of THIS SCENE 
local minLevel = 20; -- Lux level above which the scene WILL NOT trigger 
local dimLevel = 25; -- Night time light level for dimmers
local night = 0; -- Global variable that determines if it's currently night or day. Use [local night = 0] to disable
local debug = true; -- Change to true for debugging
 
------------------------------------
---- Do not edit past this point ---
------------------------------------
 
local instances = tonumber(fibaro:countScenes());
local trigger = fibaro:getSourceTrigger();
local time = tonumber(os.time());
local level = 25;
 
if (night and night > 0) then
    level = dimLevel;
end
 
    local function inArray(needle, haystack)
    for i,n in ipairs(haystack) do
        if (tonumber(needle)==tonumber(n)) then
            return true;
        end
    end
    return false;
end
 
local function getSensorStatus()
    for i,n in ipairs(sensors) do
        if (tonumber(fibaro:getValue(n, "value")) > 0) then
            return 1;
        end
    end
    return 0;
end
 
local function getLastBreach()
    local breach = 0;
    for i,n in ipairs(sensors) do
        local nBreach = tonumber(fibaro:getValue(n, "lastBreached"));
        if(nBreach > breach) then
            breach = nBreach;
        end
    end
    return breach;
end
 
local lastBreached = getLastBreach();
 
local function getLightStatus()
    for i,n in ipairs(lights) do
        if (tonumber(fibaro:getValue(n, "value")) > 0) then
            return 1;
        end
    end
    return 0;
end
 
local lightStatus = getLightStatus();
 
local function setLightStatus(command)
    for i,n in ipairs(lights) do
        if (tonumber(fibaro:getValue(n, "value"))~=tonumber(command)) then
            if (debug) then fibaro:debug("Setting device "..n.." to "..command); end
            local type = fibaro:getType(n);
            if (type == "com.fibaro.FGD212") then
                fibaro:call(n, "setValue", command);
            elseif (command > 1) then
                fibaro:call(n, "turnOn");
            else
                fibaro:call(n, "turnOff");
            end
 
        end
    end
end
 
local function keepLightOn()
    lastBreached = getLastBreach();
    time = os.time();
    if (debug) then fibaro:debug("Last breach:"..(time-lastBreached).."sec ago"); end
 
    if (getSensorStatus() == 1) then
        return true;
    end
 
    if ((time-lastBreached)>=delay) then
        return false;
    end
    return true;
end
 
local function checkLightLevel()
    if (lux == 0 or minLevel == 0) then
        return true;
    end
    local lightLevel = tonumber(fibaro:getValue(lux, "value"));
    if (lightLevel < minLevel) then
 
        return true;
    end
 
    return false;
end
 
if ((fibaro:getSourceTriggerType() == 'property') and (inArray(trigger['deviceID'], lights))) then
    if ((time-lastBreached) > 1 and lightStatus == 0) then
        if (debug) then fibaro:debug("Manual override detected. Terminating scenes."); end
        fibaro:debug(fibaro:killScenes(scene));
    else
        fibaro:abort();
    end
 
elseif ( (fibaro:getSourceTriggerType() == 'property') and getSensorStatus() == 1 and checkLightLevel() and getLightStatus() == 0) then
    if (instances > 1) then fibaro:abort(); end
 
    while (keepLightOn()) do
        if (getLightStatus() == 0) then
            setLightStatus(level);
        end
        fibaro:sleep(1000);
    end
 
    if (debug) then fibaro:debug("Reached delay threshold without motion. Turning off the light."); end
    setLightStatus(0);
 
else
    if (debug) then fibaro:debug("No matching conditions. Doing nothing"); end
    fibaro:abort();
end

Edited by silvinnio
Link to post
Share on other sites
  • 0
On 20-12-2017 at 12:20 PM, Vinisz said:

Helle Everyone, hope someone can help me out here.

I have this simple scene below running to turn on and off the lights in a room (off after 15 minutes)

It works perfect, however, problems start when turning the light off with the physical light switch (pulse)

If I do that, the light goes out (of course) but this scene keeps running and won't turn on the light if I enter the room again in 5 minutes.

 

Ideal situation (in my opinion) , to kill this scene when the light is switched of but I have no success yet.... hope someone can help me with that.

 

Greeting,

 

Vincent.

 

--[[
%% autostart
%% properties
505 value
%% weather
%% events
%% globals
--]]


if (fibaro:countScenes()>1) then fibaro:abort() end -- Run only one scene

local light_1 = 513 -- ID Lamp 1
--local light_2 = 78 -- ID lamp 2
local sensor_1 = 505 -- Mov. sensor 1
--local sensor_2 = 86 -- Mov. sensor 2
local timer = 15 -- Timer in minutes
local counter = timer


local counter = timer

if 
  
(tonumber(fibaro:getValue(sensor_1, "value")) > 0 )  

then

  
  fibaro:debug("Turn on lights")
  --fibaro:call(light_1, "turnOn"--)
  fibaro:call(light_1, "turnOn")
  --fibaro:call(light_2, "turnOn")


  
  while counter > 0
  do
    counter = counter -1
    fibaro:sleep(60*1000)
    fibaro:debug("Counter = ".. counter)
    if
    tonumber(fibaro:getValue(sensor_1, "value")) > 0 --or tonumber(fibaro:getValue(sensor_2, "value")) > 0
    then
      counter = timer
    end
  end


  
  fibaro:debug("Turn off lights")
  fibaro:call(light_1, "turnOff")
  --fibaro:call(light_2, "turnOff")--
end

Maybe I'm missing something but the following scene works perfectly for me to switch the lights on very fast, not creating extra instances of the scene (%% killOtherinstances).  Every time the sensor changes status, wether it is activated or changes to 'Safe' will trigger a new run of the scene (killing all previous ones first) and will switch off all lights after there has been no motion detected on any of the three motion sensors for 2 minutes (120000 milliseconds). Only complicating factor here is that there is an extra condition: if the variable "Licht" is set to "Licht", meaning that there is 'normal daylight, then only three lights in a very dark part of the hallway are switched on by motion. When the same variable is set to "Donker", meaning that it is dark outside, all eleven lights in the hallway are switched on. 

 

--[[
%% killOtherInstances
%% properties
2304 value
2019 value
2315 value
%% weather
%% events
%% globals
--]]

local startSource = fibaro:getSourceTrigger();

if (
 
  fibaro:getGlobalValue("Licht") == "Licht"
or
startSource["type"] == "other"
)
then
    fibaro:call(2484, "setValue", "70");
    fibaro:call(2486, "setValue", "70");
    fibaro:call(2505, "setValue", "70");
      fibaro:sleep(120000);
      fibaro:call(2505, "turnOff");
      fibaro:call(2486, "turnOff");
      fibaro:call(2484, "turnOff");
else

if 
    
  ( fibaro:getGlobalValue("Licht") == "Donker")

then
    fibaro:call(2484, "setValue", "70");
    fibaro:call(2486, "setValue", "70");
    fibaro:call(2505, "setValue", "70");
    fibaro:call(2457, "turnOn");
    fibaro:call(2413, "setValue", "70");
    fibaro:call(2670, "setValue", "70");
      fibaro:call(2672, "setValue", "70");
      fibaro:call(2417, "setValue", "70");
      fibaro:call(2407, "setValue", "70");
    fibaro:call(2509, "setValue", "70");
      fibaro:call(2507, "setValue", "70");
    fibaro:setGlobal("Overlooplicht", "Aan");
    fibaro:sleep(120000);
      fibaro:call(2505, "turnOff");
      fibaro:call(2486, "turnOff");
      fibaro:call(2484, "turnOff");
    fibaro:call(2484, "turnOff");
    fibaro:call(2486, "turnOff");
    fibaro:call(2505, "turnOff");
      fibaro:call(2457, "turnOff");
    fibaro:call(2413, "turnOff");
    fibaro:call(2670, "turnOff");
      fibaro:call(2672, "turnOff");
      fibaro:call(2417, "turnOff");
      fibaro:call(2407, "turnOff");
    fibaro:call(2509, "turnOff");
      fibaro:call(2507, "turnOff");
    fibaro:setGlobal("Overlooplicht", "Uit");
end 
end
  

Edited by wienog
Link to post
Share on other sites
  • 0
On 20/12/2017 at 8:26 PM, Thomasn said:

This works for me! If you want longer time with light on then edit the variable starttimer. The time is in seconds.

 

Regards

 

 

 

--[[ 
%% properties 
38 value
51 value
%% globals 
--]]


if (fibaro:countScenes()>1) then fibaro:abort(); end

 

function debug(color, message)
    fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
end

 

-- VARIABELS
local scene = 66 -- ID This scene
local motion = 38 -- ID for PIR
local switch = 51 -- ID Contact
local starttimer = 50; -- Time for delay
local timer = (starttimer); -- Timer in scene

 

-- Turn on and OFF
if (tonumber(fibaro:getValue(motion, "value")) > 0 ) and (tonumber(fibaro:getValue(switch, "value")) < 1) then -- Movement?

    fibaro:call(switch, "turnOn");

    debug("green", "Light on");

end
  
-- Start loop   
 repeat 
 timer=timer-1; 
 fibaro:sleep(1000);
    
     -- If movement start time again
     if (tonumber(fibaro:getValue(motion, "value"))) > 0 then 
         timer=starttimer; 
         debug("grey", "Movement. Time started again");
     end 
 
    -- Turn off if switch is used
    if (tonumber(fibaro:getValue(switch, "value"))) < 1 then 
        debug("red", "Manual push on switch - program terminated");
        timer=0
    end 
 until (timer<1) 

 

-- Turn off light 
fibaro:call(switch, "turnOff");
debug("red", "Light off");

Light turns off after timer but the light never turns on

 

On 20/12/2017 at 8:26 PM, Thomasn said:

This works for me! If you want longer time with light on then edit the variable starttimer. The time is in seconds.

 

Regards

 

 

 

--[[ 
%% properties 
38 value
51 value
%% globals 
--]]


if (fibaro:countScenes()>1) then fibaro:abort(); end

 

function debug(color, message)
    fibaro:debug(string.format('<%s style="color:%s;">%s</%s>', "span", color, message, "span")); 
end

 

-- VARIABELS
local scene = 66 -- ID This scene
local motion = 38 -- ID for PIR
local switch = 51 -- ID Contact
local starttimer = 50; -- Time for delay
local timer = (starttimer); -- Timer in scene

 

-- Turn on and OFF
if (tonumber(fibaro:getValue(motion, "value")) > 0 ) and (tonumber(fibaro:getValue(switch, "value")) < 1) then -- Movement?

    fibaro:call(switch, "turnOn");

    debug("green", "Light on");

end
  
-- Start loop   
 repeat 
 timer=timer-1; 
 fibaro:sleep(1000);
    
     -- If movement start time again
     if (tonumber(fibaro:getValue(motion, "value"))) > 0 then 
         timer=starttimer; 
         debug("grey", "Movement. Time started again");
     end 
 
    -- Turn off if switch is used
    if (tonumber(fibaro:getValue(switch, "value"))) < 1 then 
        debug("red", "Manual push on switch - program terminated");
        timer=0
    end 
 until (timer<1) 

 

-- Turn off light 
fibaro:call(switch, "turnOff");
debug("red", "Light off");

Light turns off fine but could never get the light to turn on when motion is detected. is it worth taking into account lux (light) and only utilise this scene if it is dark?

Link to post
Share on other sites
  • 0

Strange as the program should work. I have used it for over a year with no problems.  Have you changed the ID under properties in the start of the program to your values - and in the variable area? If so, then check if your PIR works as intended. Is it included in the Homecenter and does it see movement?

 

 

 

Edited by Thomasn
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...