Here is a little example of a Lua script that use a PIR to turn on light by a switch. You can copy the text into your Homecenter 2 as a Lua script. Remember to change the ID for your PIR and switch.
It was one of my first scripts and has served us well. Nowadays the script is extended with the control of a dimmable switch, which has made it possible to set the light to certain levels according to the clock. If you want that kind of feature then throw me a mail!

Script below

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

-- This program use a PIR to turn on light. If there is movement during this period
-- the timer is reset. If the switch for the light is used manually the light will be
-- turned off as it will when the program terminates.
-- As standard the light is on for 50 seconds - but that can be changed in the
-- variable section below.
-- Made in Denmark by Thomas G. Nielsen 2017.

-------- Do not start additional scenes ------------------
if (fibaro:countScenes()>1) then fibaro:abort(); end
--------------------------------------------------------------

------------- Variables. Change to own values---------------------------------------------------

local motion = 38 -- ID for PIR
local switch = 51 -- ID for Switch
local starttimer = 50; -- Time for light to be on in seconds
local timer = (starttimer); -- timer

------------- Do not edit program below this line -----------------------------------------------

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

-- Turn on / off light---------------------------------
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

------------------ Starting loop ----------------------------------------
repeat
        timer=timer-1;
        fibaro:sleep(1000);

-- if movement then restart timer------------------
       if (tonumber(fibaro:getValue(motion, "value"))) > 0 then
            timer=starttimer;
            debug("grey", "I see movement. Timer reset");
       end

-- Turn off light if switch used manually----------
        if (tonumber(fibaro:getValue(switch, "value"))) < 1 then
             debug("red", "Switch used manually - Light off");
             timer=0
        end
until (timer<1)

---------------- Program terminates. Light off ----------------------
fibaro:call(switch, "turnOff");
debug("red", "Light off");

24 Comments

  1. If switch located in the room where located PIR you will have some discomfort with manually turned off lights 🙂
    Add sleep function for waiting PIR relax after switching lights manually OFF

    1. Author

      Hi 10Der. You are propably right. I seldom use the manual switch and have not thought of movement as you turn off the light.

      1. Hello, I have a motion sensor and a switch dimmer installed in my bathroom. Now I would like too have a lua scene what turns the light 100% on during the day and at 11pm to 7am 30% on. Can you help me? Is it possible to create a scene like this?
        Thank you.
        Best regards
        And happy new year!

        1. Author

          Yes. That is totally possible. But how to do it is a matter of the system you have. I mostly use a Fibaro HomeCenter2. Take a look at my program for turning on/off light on movement.you can modify the program to your liking if you have a system like mine.

          1. Hi thanks for quick answer.
            Yes I have the Fibaro home center 2
            I see already our program but I don’t understand nothing how I change for me? With different times etc etc?
            Thanks more one time

          2. Hi thanks for quick answer.
            Yes I have the Fibaro home center 2
            I see already our program but I don’t understand nothing how I change for me? With different times etc etc?
            Thanks more one time

        2. Use something like

          local currenttime = os.date(‘*t’);
          local currenthour = currenttime[‘hour’];
          if (currenthour >= 22 ) then
          fibaro:call(switch, “setValue”, “20”);
          end

  2. Hi,

    I had the same problem as you described in your post “Use PIR to turn on light”. Actually I’m using a dimmer2 module, so I’m interested in the code you have to operate a dimmer2 in combination with a PIR> Could you forward me that code please.

    BTW cool webpage you have!

    regards
    Jules

    1. Author

      Hi Jules,

      There is code in the article. Aren’t it useful for you?

  3. Nice code, works great.

    An option to turn on the light based on movement and lux value will be great. Turn on the lights if lux value of pir is under xx lux.
    Or to turn on the lights only when between sunset and sunrise.

    Nice blog !

  4. I have bought a fibaro home center2 motion sensor and dimmer2 . My thought was to come in to the bathroom and the motion sensor should start the dimmer 2 up to 100% light. The light off after 3 min.
    You have anything I can USE ?
    I have startet with Magic scenes and graphic blocks, but not LUA yet, but very interested. Norway.

  5. Hi
    Tank you !!
    I used the Kia script.
    It Works but I get : fatal unknown exception:/opt/fibaro/scenes/13.lua:33:
    attempt to compare nil with number.

    1. You must have a typo somewhere. The Lua code has been firmly tested and works!

  6. I trye to find the problem !
    I use fibaro – motion sensor and
    -dimmer2

  7. This is an awesome little script and works well the way you have created it. But my question is this. I have 2 block scenes setup so that if the time of day is sunset, set variable, “auto lights” to night and another scene for sunrise to set, “auto lights” variable to sunrise. how could I add this in to this script? Thanks, Hal

    1. Author

      Hi Hal

      I have no experience with block scenes.

  8. Hi,

    found your nice piece of script and copied it directly to my HC2 scene.
    Unfortunately got the error message “unexpected symbol near char(194) in line 29
    Checked the typing, but did not find the cause. Little help, please.
    I would also appreciate the code with dimmable switch
    Thanks, Mati

  9. Realy like this scene. How can i put in turn on at 20% when use a dimmer2?

    1. Author

      Use fibaro:call(switch,”setValue”,”20″) in line 34

  10. Hi I’m looking for a solution with PIR and a switch. Need to use the switch to turn-off the light and need some timeout to get out of the room. DO you have a script

  11. Hello,

    I am using your Script and it is working great. In the headline you metion something about an extended script. This is very interesting, would it be possible to get a copy of that script?

    1. Author

      –[[
      %% autostart
      %% properties
      264 value
      277 value
      51 value
      %% globals
      264 value
      277 value
      51 value
      –]]

      — Start ikke en dobbeltscene
      if (fibaro:countScenes(66)>1) then fibaro:abort(66); end

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

      — VARIABLER (bevægelsessensor skal under %% properties være tilført herover)
      local motion = 264 — ID for bevægelsessensor
      local motionToo = 277 — ID for bevægelsessensor 2
      local switch = 51 — ID for relækontakt
      local currenttime = os.date(‘*t’); — Klokkeslet
      local currenthour = currenttime[‘hour’]; — Klokkeslet heltime

      — TÆND OG SLUK LYS
      — Hvis det er sen aften, så tænd med 20% styrke
      if (currenthour >= 22 ) then
      fibaro:call(switch, “setValue”, “20”);
      debug(“grey”, “20%”);
      starttimer = 15
      — Hvis det er tidlig aften, så tænd med 40%
      elseif (currenthour >= 18) then
      fibaro:call(switch, “setValue”, “40”);
      debug(“grey”, “40%”);
      starttimer = 50
      — Hvis det er sen eftermiddag, så tænd med 60%
      elseif (currenthour >= 16) then
      fibaro:call(switch, “setValue”, “60”);
      debug(“grey”, “60%”);
      starttimer = 50
      — Hvis det er dag, så tænd med 80%
      elseif (currenthour >= 6) then
      fibaro:call(switch, “setValue”, “80”);
      debug(“grey”, “80%”);
      starttimer = 20
      — Hvis det er midnat, så tænd med 3% styrke
      else
      fibaro:call(switch, “setValue”, “3”);
      debug(“grey”, “3%”);
      starttimer = 15
      end

      debug(“green”, “Tænder lys”);
      timer = (starttimer);

      — løkke startes
      repeat
      timer=timer-1;
      fibaro:sleep(1000);

      — Genstarte tid, hvis der er bevægelse inden for forsinkelsen
      if (tonumber(fibaro:getValue(motionToo, “value”)) > 0) or (tonumber(fibaro:getValue(motion, “value”)) > 0) then
      timer=starttimer;
      debug(“grey”, “Nulstil tid, bevægelse erkendt”);
      end

      — Slukker for lyset, når kontakt benyttes
      if (tonumber(fibaro:getValue(switch, “value”))) < 1 then debug("red", "Manuelt tryk observeret - løkke afbrydes"); timer=0 fibaro:sleep(7*1000); end debug("green", timer); until (timer<1) -- Slukker for lys, når tiden er gået fibaro:call(switch, "turnOff");

Leave a Reply

Your email address will not be published. Required fields are marked *