Below is my script for my alarm sequence but I seem to be saying and this and this and this.... is this the only way or could this be cleaned up. I have tried a few options to no avail so far. The below does work ok but im sure It could be compacted. As im just starting it would be good to get into the correct habits now!
Code: Select all
-- script_Alarm.lua
function timedifference(s)
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)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
commandArray = {}
if ((otherdevices['Kitchen Motion'] == 'On' or otherdevices['Hallway Motion'] == 'On') and otherdevices['Jims Home'] == 'Off' and otherdevices['Kaths Home'] == 'Off' and otherdevices['Alarm'] == 'Off' and timedifference(otherdevices_lastupdate['Jims Home']) > 600 and timedifference(otherdevices_lastupdate['Kaths Home']) > 600) then
print("Nobody Home and Motion dector Active, Rasing Alarm!")
commandArray['Alarm'] = 'On FOR 10'
end