In my search for errors/fixes I found a similar script and now Im thinking I might change my old one for something more like the new one.
But, before I do this.
Ill ask the experts, witch one is the "best" script ?
The old
Code: Select all
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 = {}
for i, v in pairs(otherdevices) do
h = tonumber((os.date('%H')))
if (h >= 6 and h < 22) then
timeon = 300
else
timeon = 30
end
tc = tostring(i)
v = i:sub(1,9)
if (v == 'SensorGan') then
difference = timedifference(otherdevices_lastupdate[tc])
if (difference > timeon and difference < (timeon + 60)) and otherdevices['LysAuto'] == 'On' then
tempdiff = tostring(difference)
tempmessage = " Lights OFF - after at least" .. timeon .. " - actually - " .. tempdiff .. "seconds"
print(tempmessage)
commandArray['LysGang'] = 'Off'
end
end
end
return commandArray
And the new:
Code: Select all
function timedifference (s)
ret = os.difftime (os.time(), 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), min=string.sub(s, 15, 16), sec=string.sub(s, 18, 19)}) return ret
end
waitTime = 300 -- 5 minutes, time to leave devices on without movement
commandArray = {}
--
-- loop through all the devices
--
for deviceName,deviceValue in pairs(otherdevices) do
-- Time based Bathroom Light/Fan/PIR/Humidity rules
if (deviceValue == "On") and (timedifference(otherdevices_lastupdate[deviceName]) > waitTime) then
if (string.sub(deviceName,-3) == 'Fan') then
-- If a fan has been on longer than the specified time, turn it off
print ("Active device time check on '"..deviceName.."', last seen "..timedifference(otherdevices_lastupdate[deviceName]).." seconds ago")
commandArray[deviceName]='Off'
end
if (string.sub(deviceName,-10) == 'Heat Lamps') then
-- If Heat Lamps has been on longer than the specified time, turn it off if the related PIR has not seen movement recently
local pirDev = string.sub(deviceName,1,string.len(deviceName)-10)..'PIR'
if (otherdevices[pirDev] ~= nil) then
print ("Active device time check on '"..deviceName.."', last movement "..timedifference(otherdevices_lastupdate[pirDev]).." seconds ago")
if (timedifference(otherdevices_lastupdate[pirDev]) > waitTime) then
commandArray[deviceName]='Off'
end
end
end
if (string.sub(deviceName,-5) == 'Light') then
local pirDev = string.sub(deviceName,1,string.len(deviceName)-5)..'PIR'
if (otherdevices[pirDev] ~= nil) then
print ("Active device time check on '"..deviceName.."', last movement "..timedifference(otherdevices_lastupdate[pirDev]).." seconds ago")
if (timedifference(otherdevices_lastupdate[pirDev]) > waitTime*2) then
commandArray[deviceName]='Off'
end
end
end
end
end
