-1

--this script will turn of the light at the second living room when there is x min no movement detected on the Motion Sensor.

--The script does run and it sometime works, like once or twice a day. but it has to work always, I can't figure out why it is not working.

t1 = os.time()
s = otherdevices_lastupdate['Motion']

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))
print(difference)

if (otherdevices['Motion'] == 'On' and difference > 60 and difference < 200) then
    commandArray['Light']= 'Off'
    print('2 minutes no movement, turn off Light 2th Living Room')
end 

return commandArray
  • By "sometimes works" do you mean that it fails with some error in the script? If yes, what's the error? If no, the problem is likely elsewhere as the script may be not launched by the engine or the movement is not properly detected. – Paul Kulchenko May 16 '15 at 15:20
  • Hey Paul, Thnx for the reply. There are no errors, and the script always run every minute when I check the log file, because it prints always the (difference). This script is only for turning the lights off when there is nog movement. I use a scene to turn it on when there is movement detection which always works good. – Nick de Kale May 17 '15 at 00:31

2 Answers2

1

If the script runs successfully but it doesn't turn the light off even if it's expected to do so, then there are not many possible reasons.

Either otherdevices['Motion'] is not 'On' (the check is case sensitive) or the difference is out of the expected range of 60..200 seconds.

dlask
  • 8,776
  • 1
  • 26
  • 30
1

Solution #1:

...
if (otherdevices['Light']=='On' and otherdevices['Motion']~='On' and difference > 120) then
    CommandArray['Light']='Off'
}

Solution #2 (better):

configure the Motion device so it activates the Light for 120 seconds, so it will be turned off automatically when Motion sensor stops toggling.

MeS Fet
  • 11
  • 1