Help with 1st piston... Closet light controlled by 2 motion sensors & door sensor


#1

1) Give a description of the problem
My first piston isn’t working as hoped.

2) What is the expected behaviour?
I have a closet light that I want to turn on when motion is detected in the bathroom or the closet. I want the light to turn off after 1 minute if the bathroom door is open. If the bathroom door is closed, do not turn off the closet light.

3) What is happening/not happening?
It works fine when the bathroom door is closed. When in the closet for an extended time (with the door open), the light will turn off (because of the 1 minute timer on the bathroom sensor). How do I “include” the closet sensor as part of the 1 minute timer?

**4) Post a Green Snapshot

5) Attach logs after turning logging level to Full
(PASTE YOUR LOGS HERE THEN HIGHLIGHT ALL OF THE LOGS AND CLICK ON THE </> ICON TO FORMAT THEM CORRECTLY)

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

Welcome Chad!
There are many ways to do this. I’m not sure how your motion sensors are set up and if they can see each other or not. I would try the following in your second IF…

IF Bathroom Sensor's motion stays inactive for 1 minute
and
Closet Sensor's motion is inactive
and 
Bathroom Door Sensor's contact is open
Then
  With Closet Light
    Turn Off
END IF

Also, I’m curious. In your first IF, there is not an orange lightening bolt in the left margin (the orange lightening bolt indicates a trigger). So your first IF is not a trigger. Do your lights turn on as expected? My thought here is that you would need to change your first IF to the following to make it trigger when motion is detected…

IF Bathroom Sensor's motion changes to active
or
Closet Sensor's motion changes to active
  Then
    With Closet Light
      Turn On
END IF

Again, not knowing your motion sensor layout, I would presume that your closet motion sensor will not become active until someone is already in the bathroom triggering that sensor (ie. the closet sensor will not become active by itself). Unless, of course, you are lingering in the closet long enough for the bathroom sensor to go inactive.


#3

Thanks very much for the help. I didn’t understand why the lightning bolt didn’t appear in the first expression myself. I’ve made the changes to the top as suggested.

On the 2nd IF (to turn the light off), both sensors are the older SmartThings motion sensors and are not very sensitive. The bath sensor is located in the corner so it picks up most of the room, however, the closet sensor is located right above the door (pointing down) and only senses when you enter or exist.
I’m afraid if the condition is set to “is inactive”, the light might go off when someone is still in the closet. Looking at the different options, if I change it to: “Closet sensor’s motion DID NOT change in the last 30 seconds” I think this will take care of it.

Thanks again!


#4

So the closet light didn’t turn on after the change so I reverted the first IF back to “motion is active” and it’s working again (the lightning bolts which did appear with “changes to” disappeared after the last change). Strange right?


#5

Do you possibly have any other piston or ST Automation that may be turning on the light?


#6

One observation about the original piston:

The trigger on line 24:
IF Bathroom's motion stays inactive for 1 minute
will trigger the piston at three different events.

(1) IF Bathroom’s motion changes to active
(2) IF Bathroom’s motion changes to inactive
(3) Timer for 1 minute after changing to inactive

This is why the top IF block will still work correctly… Even without a lightning bolt.

(although note that the Closet cannot trigger this piston… IE: no lightning bolt on that one)


#7

I was using an automation prior but that was before I added the door sensor (I had read that someone used 2 different timers depending on if the door was open/closed). After installing the door sensor, I realized that I couldn’t get it to work without using Webcore so that’s what got me to this point (the original automation was deleted back when I gave this piston a go).

WCmore, thanks for explaining the triggers. Unless there is a more efficient piston design, it seems to be working as intended. It might be nice as a backup to have the closet sensor also trigger the light… Is this an easy addition?


#8

Ah yes, I see this now and should have seen it the first time.

And I am not sure why this did not work.
image


#9

Honestly, that is solid code.


#10

Just for completeness as it is the OP’s first piston …

WebCoRE classes conditions as either just ‘conditions’ or as ‘triggers’. A trigger condition is where you are explicitly stating which events should cause your piston to run and then using those events as the basis for a comparison. A condition is where you are expressing an interest in the current value of a device attribute, which is looked up in the SmartThings device object.

When it starts up, the piston walks through your code. If it sees any triggers it will ‘subscribe’ to any device attribute events that effect that trigger. So if you have ‘motion changes to active’, the piston will subscribe to events for the motion attribute of the device in question. If there aren’t any triggers in the piston, the piston will consider ALL the conditions instead. So ‘motion is active’ would again result in a subscription to the motion attribute events, but only if there aren’t any triggers.

When a subscribed event is received, the piston is run from the top as the event handler. The event might state that it involves the ‘Bathroom Sensor’ device, the ‘motion’ attribute and the new value in the event is ‘active’, for example. This piston will run regardless and make up its own mind why it is running and what it needs to do.

When it gets to a trigger, the piston will look to see if it involves the device and attribute that caused the event to run. If it doesn’t, the trigger condition returns false. That is the basis for the claim that two triggers can’t be true at the same time. If there is more than one trigger using the same device attribute it is actually possible for more than one trigger to be true, but generally not. It is nothing to do with needing triggers to happen at the same time for two be true. That just isn’t how it works. It is always a good idea to make sure that the trigger condition associated with the event is evaluated. So if the Bathroom Sensor motion goes active and the piston runs, make sure a trigger using Bathroom Sensor motion gets evaluated. Avoid hiding triggers behind ‘only when’ statements or other nested or grouped conditions that might prevent the trigger being evaluated. The reason for this is that the piston decides for itself when something ‘changes’ based on the events it has seen before.

If the piston reaches a condition, it doesn’t care why the piston is running. It just looks up what the current value of the device attribute is. If the device attribute is used in the event I believe that value is checked (I’d have to check), otherwise it looks it up on SmartThings.

Anyway that is a simplification of the default behaviour of webCoRE. It can be changed. Now back to the original issue …

The ‘stays’ comparison is a bit of a one off. It actually evaluates the comparison like a condition, but it is classed as a trigger (it is called a Timed Trigger in various places). So as the piston is using ‘stays’ it subscribes to the Bathroom Sensor motion because of line 24. That means it will run when the motion changes, which is exactly what line 13 would like it to do. However it does mean that the piston will not run when the Closet Sensor runs.


#11

I changed the first expression again to “changes to active” and it now works. I think I may have selected “changes” prior, forgetting “to”. However, it seems like that should have worked as well so it must have been something else. In any case, the light is now turning on and off as expected.

Thanks for the additional info orangebucket. That helps me wrap my head around how everything works. Next project is the garage lights where I plan to add a 2nd motion sensor to help with presence. Thanks again everyone for your input.


#12

@hoffman_chad, would you care to show us your working piston to help others in the forum?


#13

Just adding a bit of info that may not have been apparent above… The walk-in closet is accessed through the master bath. For this reason the bathroom sensor is what “controls” the off function.


#14

I’m back… So this piston has been in use now for a month and, since the beginning, approximately 2-3 times per week, I’ll find that the closet light is on (didn’t turn off) and, in most cases, it had been on for an extended period of time according to the device history. Since the logs only seem to go back 12 hours or so, I’d end up just checking the history of the 2 sensors and the light to try to figure out what happened. On two occasions, a miss fire by the closet sensor was the cause (the bathroom sensor didn’t detect any motion and, therefore, didn’t start the timer to turn it off).

However, today, I discovered the closet light was on around 3:30. Checking the device history, it went on at 1:58 which coincided with the bathroom sensor detecting motion at that time. The motion stopped at 1:58, however, the light stayed on. Checking the log, there are 3 events from that time:


I copied this piston to control my garage lights with 2 motion sensors and a door sensor and it has been working fine. Any help to figure out what’s going on with the closet light would be greatly appreciated!!


#15

I’m getting tied in a knot but the bottom line seems to be that the light only gets turned off if the Bathroom Door is open but it was closed according to the logs.


#16

Thanks @organgebucket. It looks like that is what happened… The door sensor history shows it opened at 1:59. I didn’t think the piston could end without the light turning off… I don’t want to increase the time the bathroom sensor stays inactive because the light would stay on longer. Is there anything else I can change/add to close this “hole”?