Light on with door sensor and off if no motion


#1

1) Give a description of the problem
When the door to the basement opens, i want the basement lights to turn on. However, If the kids leave the door open, I want the lights to shut off after a few minutes.

2) What is the expected behaviour?

  • if door opens, turn light on
  • if door closes, turn light off
  • if motion in basement turn light on
  • turn light off 5 minutes after motion stops, regardless of door contact state
  • if door is open, and there is no motion for 5 minutes in basement, turn light off

3) What is happening/not happening?
I cant get lights to shut off. Especially if there is no motion to begin with.

4) Post a Green Snapshot of the pistonimage
I’ve been through so many versions of the piston, i don’t have one that is even close to working.


#2

You might try something like this:

if door contact changes to open
then
   turn light on
end if
if motion changes to active
then
   turn light on
end if
if door changes to closed
  and
  motion sensor is inactive
then
  turn light off
endif
if motion sensor stays inactive for 5 minutes
then
  turn light off
endif

It’s a lot of triggers for one piston but relatively straightforward.


#3

hi @guxdude

learning question:

Which one is better OR no difference?

  if door contact changes to open
    then
       turn light on
    end if
  if motion changes to active
    then
       turn light on
    end if

OR

if door contact changes to open
OR
if motion changes to active
    then 
    Turn the light ON

#4

@ike2018 Good question. I debated about combining those two into one but thought separating the triggers into separate ifs might be safer. Perhaps @WCmore has an opinion.


#5

If the door is open at the time, then the second example will run about 10ms faster, LOL

… but if I am considering using an ELSE block, (or want more granular control), then I usually break it down individually. (either indented, or independent)


#6

This is what I cam up with. I forgot to add one additional scenario. If someone uses the switch to turn the light on, and keep the door open, the light should turn off after x seconds if there is no motion. This piston seems to work. Is there any room for improvement?


#7

I think your second if can be problematic. If someone goes inside and the door closes, the light might turn off depending on the timing between motion and door closing triggers. That is why I separated the light off into the separate if statements:

if door changes to closed
  and
  motion sensor is inactive
then
  turn light off
endif
if motion sensor stays inactive for 5 minutes
then
  turn light off
endif

The first here only turns the light off if no one is in the room when the door closes. The second turns it off when there is no motion detected.

You may or may not get good results with the ‘physically changes’ as many device handlers don’t actually support it. You mileage may vary.


#8

Anyway to combine the last two if statement so it reads

if contact 1 sensor changes to closed
OR
switch 2 changes to on
AND (the and should apply to either of the prior two conditions)
motion sensor 1 stays inactive

then turn off switch 2


#9

There are some other threads about keeping lights on if the switch is turned on vs detected motion. You might want to search that up. It usually requires the use of variables in some way to make it work.


#10

Thanks. I understand what you’re referring to regarding keeping the light on when physically switched on. I’m not looking to do that. I was wondering if anyone thought there was a way to combine the last two if statements. I initially thought of using an or and and in the same statement, but that wont work. The piston seems to be working fine. I was simply checking to see if anyone had any ideas on streamlining the piston.


#11

You can use the group and group the two ‘and’ conditions together.


#12

Something that could be a problem, is that not all DTHs support indicating if a switch is physically changed. Even though webcore will let you check for a physical change, it doesn’t necessarily report that correctly. Depends on the switch you use.


#13

Thank you. I’ve never used the group feature before.