-
Notifications
You must be signed in to change notification settings - Fork 3
/
motionv2.lua
45 lines (43 loc) · 1.56 KB
/
motionv2.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
-- ##### Configuration start here ####
-- Define morning hour
morning = 6
-- Define night hour
night = 22
-- Define motion sensor
local motion_sensor = 'Xiaomi Motion Sensor Entree'
-- Define light bulb
local light_bulb = 'YeelightWhite'
-- To turn debug on set to true
debug=false
-- #### Configuration stop here ####
s = otherdevices_lastupdate[motion_sensor]
time = os.date("*t")
t1 = os.time()
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
commandArray = {}
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = (os.difftime (t1, t2))
if (devicechanged[motion_sensor] == 'On') then
if (debug) then print("Motion on") end
if (otherdevices[light_bulb] == 'Off') then
if (time.hour < morning or time.hour > night) then
if (debug) then print('its nighttime') end
commandArray[1]={[light_bulb]='On'}
commandArray[2]={[light_bulb]='Set Level 1'}
else
if (debug) then print('its daytime') end
commandArray[1]={[light_bulb]='On'}
commandArray[2]={[light_bulb]='Set Level 40'}
end
end
end
if (otherdevices[light_bulb] ~= 'Off' and otherdevices[motion_sensor] == 'Off' and difference > 30) then
if (debug) then print('Turning ' .. light_bulb .. ' Off after '..difference..'.') end
commandArray[light_bulb]='Off'
end
return commandArray