# Turn off lights if no motion or doors open

#1

1) Give a description of the problem
If the doors are closed for 10 mins and there has been no motion in hallway for the past minute it works fine, however if there was motion the piston does not loop back to the beginning and run again as I would like it to

2) What is the expected behaviour?
If the doors are all closed for 10 mins and there has been no motion in the entryway for 1 minute then I want the lights to turn off. If there is motion in the entryway but the doors have remained closed I want the piston to wait until 1 min of no motion, then turn the lights off

3) What is happening/not happening?
If everything is true (door close and there is no motion) the first time it runs perfectly. However if there is motion, then the motion stops for 1 minute and the doors stay closed, it does not turn the lights off. I think the piston is stopping once the second condition is not met.

**4) Post a Green Snapshot of the piston![image|45x37]

i have seconds rather than minutes for testing purposes

#2

``````define
boolean doorClosed = (no value set)
end define

IF ALL of Sensor 1 and Sensor 2's contact stays closed for 10 minutes
Then Set variable {doorClosed) = true   (TCP set to Never)
IF Motion Sensor 1's motion is inactive
Then Wait 1 minute
IF Motion Sensor 1's motion is inactive
Then do stuff
END IF
END IF
END IF

IF ANY of Sensor 1 and Sensor 2's contact changes to open
Then Set variable {doorClosed) = false
END IF

IF Motion Sensor 1's motion stays inactive for 1 minute
and
{doorClosed} = true
Then do stuff
END IF``````

#3

Thanks for your help! I am having trouble figuring out how to input the above
Under ‘then’ I choose ‘add a new statement’ and i can add an if, timer or action
what menu does “set variable” fall under? It doesn’t appear in actions.

#4

Also when I try to input the above, instead I get

boolean doorClosed = /* */

Have I done something wrong or do they mean the same things?

#5

I usually put “`Set variable`” commands inside location blocks, but it can really go inside any block.

`Add a new statement > Add an action > Add a task > Please select a command > Set variable`

#6

Same thing. The (no value set) is seen from the earlier screen

#7

Note, I just edited and added a bit of code to my earlier post. This is to overcome the limitation of motion going inactive before the ten minutes has expired. This improvement covers both scenarios.

Please be extra careful with the nested IFs…

#8

Thanks for you helpful advice! I have constructed my piston based on what you said and the lights do not seem to turn off, do you see where I’ve made a mistake?

#9

Your indenting looks perfect! The only thing that stands out to me is that “TCP to Never” should be on the WITH that contains the WAIT (and perhaps the WITH beneath it).

Also, for testing, I would not go less than one minute on lines 28 & 46 (which should match), and not less than two minutes on line 22 (which should be much longer than the other number)… This is to create a more accurate “real-world” test. Your mileage may vary.

I realize this means each test will take about 3 minutes, but this is a multi-layered piston, and 3 minutes is better than 11, right?

If you are still having issues, please turn logging to Full and share your error log here.
(we’d also need to see a fresh green snapshot with Trace turned on)

#10

For the record, a proper test is two fold:

## Test One executes the first block:

• Open door
• Pause
• Close door
• Walk past motion sensor, and leave the room
• Wait (longer + shorter) duration
• Watch lights turn off

## Test Two executes the third block:

(the changes are in bold)

• Open door
• Pause
• Close door
• Wait the longer duration
• Walk past motion sensor, and leave the room
• Wait the shorter duration + 1 minute (so your sensor can report inactivity)
• Watch lights turn off