<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Event_script_examples</id>
	<title>Event script examples - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.domoticz.com/index.php?action=history&amp;feed=atom&amp;title=Event_script_examples"/>
	<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Event_script_examples&amp;action=history"/>
	<updated>2026-09-19T00:03:12Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.46.0</generator>
	<entry>
		<id>https://wiki.domoticz.com/index.php?title=Event_script_examples&amp;diff=10343&amp;oldid=prev</id>
		<title>Xztraz at 13:36, 10 February 2018</title>
		<link rel="alternate" type="text/html" href="https://wiki.domoticz.com/index.php?title=Event_script_examples&amp;diff=10343&amp;oldid=prev"/>
		<updated>2018-02-10T13:36:17Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Send a warning when the garage door has been open for more than 10 minutes ==&lt;br /&gt;
&lt;br /&gt;
Using time difference in seconds between time that script runs (t1) and the time the door status was last seen. 600 seconds for 10 minutes, one notification is enough so make sure to put in an end value as well (&amp;lt;700) otherwise a notification will be sent every minute when the script is evaluated. &lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- script_time_garagedoor.lua&lt;br /&gt;
t1 = os.time()&lt;br /&gt;
s = otherdevices_lastupdate[&amp;#039;Garagedeur&amp;#039;]&lt;br /&gt;
-- returns a date time like 2013-07-11 17:23:12&lt;br /&gt;
&lt;br /&gt;
year = string.sub(s, 1, 4)&lt;br /&gt;
month = string.sub(s, 6, 7)&lt;br /&gt;
day = string.sub(s, 9, 10)&lt;br /&gt;
hour = string.sub(s, 12, 13)&lt;br /&gt;
minutes = string.sub(s, 15, 16)&lt;br /&gt;
seconds = string.sub(s, 18, 19)&lt;br /&gt;
&lt;br /&gt;
commandArray = {}&lt;br /&gt;
&lt;br /&gt;
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}&lt;br /&gt;
difference = (os.difftime (t1, t2))&lt;br /&gt;
if (otherdevices[&amp;#039;Garagedeur&amp;#039;] == &amp;#039;Open&amp;#039; and difference &amp;gt; 600 and difference &amp;lt; 700) then&lt;br /&gt;
   commandArray[&amp;#039;SendNotification&amp;#039;]=&amp;#039;Garage door alert#The garage door has been open for more than 10 minutes!&amp;#039;&lt;br /&gt;
end &lt;br /&gt;
 &lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
== Simple security ==&lt;br /&gt;
&lt;br /&gt;
Using a kaku/coco keyfob named &amp;quot;Surveillance mode&amp;quot;. Note that it goes to &amp;quot;Group On&amp;quot; when on! My motion sensors are X10, check the motion status on your pirs using: for i, v in pairs(devicechanged) do print(i, v) end&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
commandArray = {}&lt;br /&gt;
 if ((otherdevices[&amp;#039;Surveillance mode&amp;#039;] == &amp;#039;Group On&amp;#039;) and (devicechanged[&amp;#039;PIR 1&amp;#039;] == &amp;#039;Motion&amp;#039; or devicechanged[&amp;#039;PIR 1&amp;#039;] == &amp;#039;Motion&amp;#039;)) then&lt;br /&gt;
    commandArray[&amp;#039;SendNotification&amp;#039;]=&amp;#039;Alert#Motion detected while in surveillance mode!&amp;#039;&lt;br /&gt;
 end&lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Have Domoticz execute Operating System commands ==&lt;br /&gt;
&lt;br /&gt;
Lua supports execution of operating system executables and scripts, actual executing syntax will depend on your operating system.&lt;br /&gt;
&lt;br /&gt;
This simple script sends a &amp;quot;wake on lan&amp;quot; event to a predefined host using the command &amp;quot;etherwake&amp;quot; on linux.&lt;br /&gt;
the argument to etherwake is the mac address of the computer to be woken.&lt;br /&gt;
the purpose of: commandArray[&amp;#039;wakeup-server&amp;#039;] = &amp;#039;Off&amp;#039; is to make sure that the wake on lan event only gets triggered once, since the script lacks any state checking capability&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
commandArray = {}&lt;br /&gt;
  if (otherdevices[&amp;#039;wakeup-server&amp;#039;] == &amp;#039;On&amp;#039;) then&lt;br /&gt;
    os.execute(&amp;#039;/usr/sbin/etherwake 00:1e:d0:31:e0:21&amp;#039;)&lt;br /&gt;
    commandArray[&amp;#039;wakeup-server&amp;#039;] = &amp;#039;Off&amp;#039;&lt;br /&gt;
    print(&amp;#039;wol event sent&amp;#039;)&lt;br /&gt;
  end&lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Presence detection using two motion sensors ==&lt;br /&gt;
&lt;br /&gt;
This is a script is used to determine if anyone is still upstairs, it is a time based script that runs every minute and it uses two motion detectors to determine if there is any movement upstairs, and which direction (up or down) the last person was heading, if there is no motion upstairs, and the last person was heading downstairs it turns off all lights in the group &amp;quot;scene:lights_upstairs&amp;quot; when 30 minutes have passed.&lt;br /&gt;
&lt;br /&gt;
One motion sensor is located on the top floor, and the other one in the staircase and the timedifference between their lastupdated timestamps determines direction.&lt;br /&gt;
&lt;br /&gt;
there is also a manual switch downstairs that turns on the ceiling light on the top floor, and to allow time for someone to make it up the stairs I never turn off the lights if the ceiling light was switched on within the last two minutes (120s), if you turn on the ceiling light upstairs, but never go up, it will automatically turn off the next time the script runs after two minutes.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
 function timedifference (s)&lt;br /&gt;
   year = string.sub(s, 1, 4)&lt;br /&gt;
   month = string.sub(s, 6, 7)&lt;br /&gt;
   day = string.sub(s, 9, 10)&lt;br /&gt;
   hour = string.sub(s, 12, 13)&lt;br /&gt;
   minutes = string.sub(s, 15, 16)&lt;br /&gt;
   seconds = string.sub(s, 18, 19)&lt;br /&gt;
   t1 = os.time()&lt;br /&gt;
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}&lt;br /&gt;
   difference = os.difftime (t1, t2)&lt;br /&gt;
   return difference&lt;br /&gt;
 end&lt;br /&gt;
 commandArray = {}&lt;br /&gt;
 print(&amp;#039;evaluating if someone is upstairs&amp;#039;)&lt;br /&gt;
 if (otherdevices[&amp;#039;topfloor_motion&amp;#039;] == &amp;#039;Off&amp;#039;) and (timedifference(otherdevices_lastupdate[&amp;#039;topfloor_ceiling_lamp&amp;#039;]) &amp;gt; 120) then&lt;br /&gt;
   if (timedifference(otherdevices_lastupdate[&amp;#039;topfloor_motion&amp;#039;]) &amp;gt; 1800) then&lt;br /&gt;
      if timedifference(otherdevices_lastupdate[&amp;#039;top_stairs_motion&amp;#039;]) &amp;lt;    timedifference(otherdevices_lastupdate[&amp;#039;topfloor_motion&amp;#039;]) then&lt;br /&gt;
         commandArray[&amp;#039;Scene:topfloor_lights&amp;#039;]=&amp;#039;Off&amp;#039;&lt;br /&gt;
         print(&amp;#039;turning off, no movement upstairs for more than 30m&amp;#039;)&lt;br /&gt;
      end&lt;br /&gt;
   end&lt;br /&gt;
 end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using Ping command to check the status of a network device==&lt;br /&gt;
This Lua time script will run every minute (if named script_time_ping.lua) and checks the status of a device by sending one IP ping command to its IP address. The result is reported to Domoticz through a (dummy) switch called Ping in the example.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
commandArray = {}&lt;br /&gt;
&lt;br /&gt;
 ping_success=os.execute(&amp;#039;ping -c1 192.168.1.156&amp;#039;)&lt;br /&gt;
 if ping_success then&lt;br /&gt;
   print(&amp;quot;ping success&amp;quot;)&lt;br /&gt;
   commandArray[&amp;#039;Ping&amp;#039;]=&amp;#039;On&amp;#039;&lt;br /&gt;
 else&lt;br /&gt;
   print(&amp;quot;ping fail&amp;quot;)&lt;br /&gt;
   commandArray[&amp;#039;Ping&amp;#039;]=&amp;#039;Off&amp;#039;	&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
When running Domoticz on a windows platform use:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
 ping_success=os.execute(&amp;#039;ping 192.168.1.156 -n 1 -w 100&amp;gt;nul&amp;#039;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Should the device not respond to the first ping -c1 could be changed to -c3 to have it send three pings. However this will &amp;quot;hold up&amp;quot; the script for 3 seconds. Should the test be performed more often then once a minute a shell script can be used which can be found  [http://sourceforge.net/p/domoticz/discussion/domofrs/thread/0e216d1c/?limit=50#bbdc here]. [http://www.domoticz.com/forum/viewtopic.php?f=5&amp;amp;t=174&amp;amp;p=3724&amp;amp;hilit=ping#p907 Source]&lt;br /&gt;
&lt;br /&gt;
==Using Ping &amp;amp; Bluetooth to check the status of a network device==&lt;br /&gt;
&lt;br /&gt;
This Lua script checks the presence of a device by using the ping command. If you want it can also check by Bluetooth. You don&amp;#039;t need to pair your device. It neither has to be detectable.&lt;br /&gt;
The script runs every minute if you name the script &amp;quot;script_time_&amp;#039;&amp;#039;name&amp;#039;&amp;#039;.lua&amp;quot; and put it in domoticz/scripts/lua.&lt;br /&gt;
&lt;br /&gt;
Setup:&lt;br /&gt;
&lt;br /&gt;
1. For every device create a dummy switch in Domoticz&lt;br /&gt;
&lt;br /&gt;
2. For every device create a user variable in Domoticz (set value to 1)&lt;br /&gt;
&lt;br /&gt;
3. Use your own settings in this code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
ping[1]={&amp;#039;192.168.1.1&amp;#039;, &amp;#039;Device 1&amp;#039;, &amp;#039;Teller 1&amp;#039;, &amp;#039;nil&amp;#039;, 5}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
192.168.1.1 = ip-address&lt;br /&gt;
&lt;br /&gt;
Device 1    = Name of dummy switch&lt;br /&gt;
&lt;br /&gt;
Teller 1    = Name of User Variable&lt;br /&gt;
&lt;br /&gt;
nil         = if you just want to use the ping command. Fill in the MAC-address (Bluetooth!) if you also want to use Bluetooth detection&lt;br /&gt;
&lt;br /&gt;
5           = the delay in minutes (in this case if the device doesn&amp;#039;t response 5 times in a row the switch is turned off)&lt;br /&gt;
&lt;br /&gt;
This code has 3 devices but you can expand with as many as you want. Just add more lines.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- Title: script_time_ping.lua&lt;br /&gt;
-- Date:  14-07-2015&lt;br /&gt;
&lt;br /&gt;
commandArray={}&lt;br /&gt;
&lt;br /&gt;
debug=false&lt;br /&gt;
&lt;br /&gt;
prefixe=&amp;quot;(PING) &amp;quot;&lt;br /&gt;
&lt;br /&gt;
local ping={}&lt;br /&gt;
local ping_success&lt;br /&gt;
local bt_success&lt;br /&gt;
&lt;br /&gt;
ping[1]={&amp;#039;192.168.1.1&amp;#039;, &amp;#039;Device 1&amp;#039;, &amp;#039;Teller 1&amp;#039;, &amp;#039;nil&amp;#039;, 5}                 -- android&lt;br /&gt;
ping[2]={&amp;#039;192.168.1.2&amp;#039;, &amp;#039;Device 2&amp;#039;, &amp;#039;Teller 2&amp;#039;, &amp;#039;aa:bb:cc:dd:ee:ff&amp;#039;, 5}   -- iphone&lt;br /&gt;
ping[3]={&amp;#039;192.168.1.3&amp;#039;, &amp;#039;Device 3&amp;#039;, &amp;#039;Teller 3&amp;#039;, &amp;#039;nil&amp;#039;, 5}                 -- android&lt;br /&gt;
&lt;br /&gt;
for ip = 1, #ping do&lt;br /&gt;
  if ping[ip][4] == &amp;#039;nil&amp;#039; then&lt;br /&gt;
    bt_success=false&lt;br /&gt;
    ping_success=os.execute(&amp;#039;ping -c 1 -w 1 &amp;#039;..ping[ip][1])&lt;br /&gt;
  else&lt;br /&gt;
    f = assert (io.popen (&amp;quot;hcitool names &amp;quot;..ping[ip][4]))&lt;br /&gt;
    bt = f:read()&lt;br /&gt;
    if bt==nil then&lt;br /&gt;
      bt_success=false&lt;br /&gt;
    else&lt;br /&gt;
      bt_success=true&lt;br /&gt;
    end&lt;br /&gt;
    f:close()&lt;br /&gt;
  end&lt;br /&gt;
  if ping_success or bt_success then&lt;br /&gt;
    if (debug==true) then&lt;br /&gt;
      print(prefixe..&amp;quot;ping success &amp;quot;..ping[ip][2])&lt;br /&gt;
    end&lt;br /&gt;
    if (otherdevices[ping[ip][2]]==&amp;#039;Off&amp;#039;) then&lt;br /&gt;
      commandArray[ping[ip][2]]=&amp;#039;On&amp;#039;&lt;br /&gt;
    end&lt;br /&gt;
    if (uservariables[ping[ip][3]]) ~= 1 then&lt;br /&gt;
      commandArray[&amp;#039;Variable:&amp;#039;..ping[ip][3]]= tostring(1)&lt;br /&gt;
    end&lt;br /&gt;
  else&lt;br /&gt;
    if (debug==true) then&lt;br /&gt;
      print(prefixe..&amp;quot;ping fail &amp;quot;..ping[ip][2])&lt;br /&gt;
    end&lt;br /&gt;
    if (otherdevices[ping[ip][2]]==&amp;#039;On&amp;#039;) then&lt;br /&gt;
      if (uservariables[ping[ip][3]])==ping[ip][5] then&lt;br /&gt;
        commandArray[ping[ip][2]]=&amp;#039;Off&amp;#039;&lt;br /&gt;
      else&lt;br /&gt;
        commandArray[&amp;#039;Variable:&amp;#039;..ping[ip][3]]= tostring((uservariables[ping[ip][3]]) + 1)&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
--[[User:RasPi2|RasPi2]] 21:49, 28 June 2015 (CEST)&lt;br /&gt;
&lt;br /&gt;
==Motion detector 30 minute time-out (presence detection)==&lt;br /&gt;
I created a script that looks at the status of a motion sensor (Z-Wave) to detect presence. The motion sensor itself will switch back to &amp;#039;Off&amp;#039; when no motion is detected anymore after 4 minutes. This is a bit short, i would like to extend it to a longer period. With the script, Domoticz looks if the motion sensor stays in the &amp;#039;Off&amp;#039; status for more than 30 minutes.&amp;lt;br&amp;gt;&lt;br /&gt;
If this is the case, it will turn the &amp;#039;SomeoneHome&amp;#039; switch to off. When there is motion within the 30 minute period (configurable), the timer is reset and nothing will happen (lights will stay on).&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- script_time_nomotion.lua&lt;br /&gt;
&lt;br /&gt;
local motion_switch = &amp;#039;Motion&amp;#039;&lt;br /&gt;
local nomotion_uservar = &amp;#039;nomotionCounter&amp;#039;&lt;br /&gt;
local status_switch = &amp;#039;SomeoneHome&amp;#039;&lt;br /&gt;
&lt;br /&gt;
commandArray = {}&lt;br /&gt;
&lt;br /&gt;
no_motion_minutes = tonumber(uservariables[nomotion_uservar])&lt;br /&gt;
&lt;br /&gt;
if (otherdevices[motion_switch] == &amp;#039;Off&amp;#039;) then&lt;br /&gt;
	no_motion_minutes = no_motion_minutes + 1&lt;br /&gt;
	--print(&amp;#039;&amp;lt;font color=&amp;quot;red&amp;quot;&amp;gt;No motion has been detected for: &amp;#039; ..no_motion_minutes.. &amp;#039; minutes&amp;lt;/font&amp;gt;&amp;#039;)&lt;br /&gt;
else &lt;br /&gt;
	no_motion_minutes = 0	&lt;br /&gt;
end &lt;br /&gt;
&lt;br /&gt;
commandArray[&amp;#039;Variable:&amp;#039; .. nomotion_uservar] = tostring(no_motion_minutes)&lt;br /&gt;
&lt;br /&gt;
if otherdevices[status_switch] == &amp;#039;On&amp;#039; and no_motion_minutes &amp;gt; 30 then --change the 30 to the amount of minutes you prefer&lt;br /&gt;
 	commandArray[status_switch]=&amp;#039;Off&amp;#039;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The script above needs a uservariable called &amp;#039;nomotionCounter&amp;#039;, type &amp;#039;Integer&amp;#039; and value &amp;#039;0&amp;#039; (zero).&amp;lt;br&amp;gt;&lt;br /&gt;
The script is only for turning &amp;#039;&amp;#039;&amp;#039;off&amp;#039;&amp;#039;&amp;#039; the &amp;#039;SomeoneHome&amp;#039; switch (which i base other events on). For turning that switch on (and letting Domoticz know that someone got home), use the script below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
-- script_device_motion.lua&lt;br /&gt;
local motion_switch = &amp;#039;Motion&amp;#039;&lt;br /&gt;
local status_switch = &amp;#039;SomeoneHome&amp;#039;&lt;br /&gt;
&lt;br /&gt;
commandArray = {}&lt;br /&gt;
&lt;br /&gt;
if devicechanged[motion_switch] then&lt;br /&gt;
	if otherdevices[motion_switch] == &amp;#039;On&amp;#039; and otherdevices[status_switch] == &amp;#039;Off&amp;#039; then&lt;br /&gt;
 	commandArray[status_switch]=&amp;#039;On&amp;#039;&lt;br /&gt;
 	end&lt;br /&gt;
end&lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
--[[User:ThinkPad|ThinkPad]] ([[User talk:ThinkPad|talk]]) 19:33, 23 October 2015 (CEST)&lt;br /&gt;
&lt;br /&gt;
==Advanced motion detection with multiple detectors, switches and configurable timeouts==&lt;br /&gt;
&lt;br /&gt;
To create a more advanced motion detection system this script allows you to combine multiple detectors with multiple switches. Example use-cases are turning on the lights in specific parts of the hallway depending on motion detectors and door magnet sensors.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;First, create a user variable per group you wish to control simultaneously.&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
1. Go to: setup -&amp;gt; more options -&amp;gt; user variables&lt;br /&gt;
&lt;br /&gt;
2. Variable name: the name of the group, I would recommend letters, numbers and underscores only&lt;br /&gt;
&lt;br /&gt;
3. Variable type: integer&lt;br /&gt;
&lt;br /&gt;
4. Variable value: 0&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Second, create a timer script to increment the no-activity counter (the variables above) every minute&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
1. Go to: setup -&amp;gt; more options -&amp;gt; events&lt;br /&gt;
&lt;br /&gt;
2. Fill in any name you wish&lt;br /&gt;
&lt;br /&gt;
3. Instead of &amp;quot;[[Blockly]]&amp;quot; select &amp;quot;Lua&amp;quot;&lt;br /&gt;
&lt;br /&gt;
4. Instead of &amp;quot;All&amp;quot; set &amp;quot;Time&amp;quot;&lt;br /&gt;
&lt;br /&gt;
5. Paste the following script but modify the variables at the top:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
commandArray = {}&lt;br /&gt;
&lt;br /&gt;
local switches = {&lt;br /&gt;
    -- The hallway_floor_0 is the variable name chosen earlier&lt;br /&gt;
    hallway_level_0={&lt;br /&gt;
        -- The detectors below can be anything that turns &amp;quot;On&amp;quot; when there&amp;#039;s activity&lt;br /&gt;
        detectors={&lt;br /&gt;
            &amp;#039;door front&amp;#039;,&lt;br /&gt;
            &amp;#039;motion staircase level 0&amp;#039;&lt;br /&gt;
        },&lt;br /&gt;
        switches={&lt;br /&gt;
            &amp;#039;light level 0&amp;#039;,&lt;br /&gt;
            &amp;#039;light level 1&amp;#039;&lt;br /&gt;
        },&lt;br /&gt;
        timeout=2&lt;br /&gt;
    },&lt;br /&gt;
    hallway_level_1={&lt;br /&gt;
        detectors={&lt;br /&gt;
            &amp;#039;motion staircase level 1&amp;#039;&lt;br /&gt;
        },&lt;br /&gt;
        switches={&lt;br /&gt;
            &amp;#039;light level 1&amp;#039;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
for variable,devices in pairs(switches) do&lt;br /&gt;
    -- Increase the no motion counter&lt;br /&gt;
    local no_motion_minutes = tonumber(uservariables[variable]) + 1&lt;br /&gt;
    local timeout = tonumber(devices[&amp;#039;timeout&amp;#039;] or 5)&lt;br /&gt;
    &lt;br /&gt;
    -- Store the no motion counter in the variable again&lt;br /&gt;
    commandArray[&amp;#039;Variable:&amp;#039; .. variable] = tostring(no_motion_minutes)&lt;br /&gt;
    &lt;br /&gt;
    -- If we have reached the timeout, disable the linked switches&lt;br /&gt;
    if(timeout &amp;lt;= no_motion_minutes) then&lt;br /&gt;
        for i, switch in ipairs(devices[&amp;#039;switches&amp;#039;]) do&lt;br /&gt;
            if(otherdevices[switch] ~= &amp;#039;Off&amp;#039;) then&lt;br /&gt;
                commandArray[switch] = &amp;#039;Off&amp;#039;&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;        &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Third, create a device script to turn the lights on and reset the counters when needed&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
1. Go to: setup -&amp;gt; more options -&amp;gt; events&lt;br /&gt;
&lt;br /&gt;
2. Fill in any name you wish&lt;br /&gt;
&lt;br /&gt;
3. Instead of &amp;quot;[[Blockly]]&amp;quot; select &amp;quot;Lua&amp;quot;&lt;br /&gt;
&lt;br /&gt;
4. Instead of &amp;quot;All&amp;quot; set &amp;quot;Device&amp;quot;&lt;br /&gt;
&lt;br /&gt;
5. Paste the following script but modify the variables at the top:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
commandArray = {}&lt;br /&gt;
&lt;br /&gt;
local switches = {&lt;br /&gt;
    -- The hallway_floor_0 is the variable name chosen earlier&lt;br /&gt;
    hallway_level_0={&lt;br /&gt;
        -- The detectors below can be anything that turns &amp;quot;On&amp;quot; when there&amp;#039;s activity&lt;br /&gt;
        detectors={&lt;br /&gt;
            &amp;#039;door front&amp;#039;,&lt;br /&gt;
            &amp;#039;motion staircase level 0&amp;#039;&lt;br /&gt;
        },&lt;br /&gt;
        switches={&lt;br /&gt;
            &amp;#039;light level 0&amp;#039;,&lt;br /&gt;
            &amp;#039;light level 1&amp;#039;&lt;br /&gt;
        },&lt;br /&gt;
        timeout=2&lt;br /&gt;
    },&lt;br /&gt;
    hallway_level_1={&lt;br /&gt;
        detectors={&lt;br /&gt;
            &amp;#039;motion staircase level 1&amp;#039;&lt;br /&gt;
        },&lt;br /&gt;
        switches={&lt;br /&gt;
            &amp;#039;light level 1&amp;#039;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
-- Loop through all the changed devices sent by the trigger&lt;br /&gt;
for deviceName,deviceValue in pairs(devicechanged) do&lt;br /&gt;
    &lt;br /&gt;
    for variable,devices in pairs(switches) do&lt;br /&gt;
        local status = 0&lt;br /&gt;
    &lt;br /&gt;
        for i, detector in ipairs(devices[&amp;#039;detectors&amp;#039;]) do&lt;br /&gt;
            -- Check if it is one of the detectors and is set to &amp;quot;On&amp;quot;&lt;br /&gt;
            if detector == deviceName and deviceValue == &amp;#039;On&amp;#039; then&lt;br /&gt;
                status = 1&lt;br /&gt;
                break&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
&lt;br /&gt;
        -- If one of the sensors was turned on, enable the light and reset the no motion variable&lt;br /&gt;
        if status == 1 then&lt;br /&gt;
            commandArray[&amp;#039;Variable:&amp;#039; .. variable] = tostring(0)&lt;br /&gt;
            for i, switch in ipairs(devices[&amp;#039;switches&amp;#039;]) do&lt;br /&gt;
                if(otherdevices[switch] == &amp;#039;Off&amp;#039;) then&lt;br /&gt;
                    commandArray[switch] = &amp;#039;On&amp;#039;&lt;br /&gt;
                end&lt;br /&gt;
            end&lt;br /&gt;
        end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
        &lt;br /&gt;
return commandArray&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;        &lt;br /&gt;
&lt;br /&gt;
--[[User:Wolph]] 2016-07-04 16:23:56 (CEST)&lt;br /&gt;
&lt;br /&gt;
== Where to Find More Examples ==&lt;br /&gt;
&lt;br /&gt;
Many other examples of the use of Lua can be found by searching for Lua on the discussion forum and other pages on the wiki for specific situations (i.e. [[MQTT|Setup MQTT support]], [[Interacting with Google Calendar]], [[Smart Lua Scripts]])&lt;br /&gt;
[[Category:Lua]]&lt;/div&gt;</summary>
		<author><name>Xztraz</name></author>
	</entry>
</feed>