I agree. I have a piston using up to four conditional IF’s inside a trigger ‘IF’ without issue. Works every time - and only when it should (as per the driving trigger).
How to override motion sensor light?
This mirrors a scenario that’s about to happen for me. My wife requested a light go on in a vestibule whenever someone opens a door in that area. Makes sense, except that the light going on at 2am wakes whoever else is asleep… and some of us see very well in the dark.
So I ordered a trio of zigbee buttons, one to be mounted on the wall in each of the rooms that lead to the vestibule. I will press the button, and it will disable the light from going on.
This thread has given me some thoughts on how to do it. I’m tempted to leave the motion/light in Smart Lighting so it operates locally (I think), and have the press of one of these buttons do a few things.
The buttons should be set up for momentary operation.
Pressing one of them at any time turns on a simulated switch.
The simulated switch state is part of the Smart Lighting, which has an available condition that says “if switch _____ is (on/off)”. So that sim switch would normally be off, and the motion sensor would trigger the light on. If I press the button, it turns on the sim switch… and the light would not go on with motion.
A piston would be triggered by that sim switch being turned on. Its job would be to count five minutes, and then turn the sim switch back off. So the light once again gets turned on by motion.
As long as the buttons are out of sight of the motion sensor, you could skip the SimSwitch entirely. Maybe something like this:
Piston 1 (new piston)
IF
Any of Buttons A, B or C changes to on
Then
Set @override = true <-- Global Boolean Variable
END IF
Piston 2 (slight modification of your ‘Motion Sensor’ piston)
IF
Vestibule's motion changes to active
and
@override is false <-- NEW LINE
Then
Turn on light (as normal)
END IF
IF
Vestibule's motion stays inactive for X minutes
Then
Turn off light (as normal)
Set @override = false <-- NEW LINE
END IF
The only time the above code would fail is if someone pressed the button, but then avoided the motion sensor. (Although it would be easy to code in an extra line to account for this, or it will fix itself as soon as someone walks by)
This is what I have right now and it’s not working. I manually switched the light to be on but the light still turns off after no motion detected.
I don’t know how what @WCmore suggested works line 35 (he is the pro so, go with his way)
The only thing is, I believe when a piston is triggered, if you have defined variable, even if it changed the last time piston ran, it will go back to original status you defined.
if you say
variable = TRUE
Execute this that
and then
piston do this do that
Set variable to FALSE
it will remain false UNTIL the piston is executed again.
So try not assigning anything on line 17
I apologize for the confusion. I should know better than to try to help two people in the same thread simultaneously.
Instead of re-creating the wheel, here is a great piston that does exactly what you want @2Charlie
@MCmore, thanks for the link. I’ll check it out. However, in the mean time, I think the logic in the IF statement from line 32 to 44 is incorrect. So, maybe something like this?
So I followed this post here and I saw this edit.
Does this mean we have to interact with the SmartThings app if we need to turn off the light?
If I am not mistaken, it can be done via the app, thru Alexa, or by turning off the wall switch.
(you might want to ask specifics on that page though. @bangali is always helpful)
Just a small note.
The 1st piston on that page is for a dumb switch and smart bulb combo
The 2nd piston on that page is for a smart switch and dumb bulb combo
I use a slightly modified version of the 2nd piston (smart switch with dumb bulbs) and you are correct about the ability to turn the light off with Alexa, the ST app or the wall switch.
At this point my code above is not working. The kitchen light does turn on when detect motion but did not turn off after 2 minutes.
Okay, a couple of basic questions since I’m new.
- The execution is from top to bottom, correct?
- If the condition is met, does it quit or continue executing until the end of the code?
- Is the value of the variable only last for through the current execution?
Hi @2Charlie
1 - Yes but…
2 - Depends.
If your code after condition stops everything then it could stop. (cancel all depending tasks etc)
But normally it will go al the way to the end.
3 - Depends.
if you defined a variable, everytime piston runs it will go back to that…
lets say:
local variables:
variable {teststatus} = false
(piston does lots of things here and at the end you say)
set variable {teststatus} = true
END
unless this piston runs AGAIN, your variable will always remain TRUE
Global variables :
You define them outside of the piston. So, running the piston by it self will never change the value of a global variable. Only what ever YOU say in the piston will.
Just to clarify a tiny bit… If the variable is not defined at the very top, but is written to in the code somewhere, it will remain indefinitely until it is overwritten.
For example, if you run this piston once, the SMS will error (null variable). A moment later, the variable will be set correctly. If you wait a year and then run the piston again, the variable you set a year ago will come thru your SMS message
execute
Send SMS = {variable}
Set variable = "some data"
end execute
No. When the piston runs again, it keeps the old data in the variable until the moment your code writes new data to it
What you describe happens only if you force in a variable in the top ‘define’ section.
Another way to say this:
When a variable is set, it is permanent, and will not ever change.
There are three exceptions:
- If you manually change the variable (great for testing new code)
- If the ‘define’ section is left blank, it will only change if you place code somewhere in your piston, the piston is somehow executed, and the conditions are true for that block. (my preferred method)
- If the piston has the variable defined in the top section, each time the piston runs, it will ‘reset’ to that variable. (the only time I hard code in the define section is when I do not change it later in the code)
This is what I have right now and the does not turn off after the motion sensor has changed to inactive.
According to your piston, it should be two minutes after the sensor reports no activity. Depending on your motion sensor, this could be a few minutes longer than 2 minutes. (most sensors report inactivity between 6 seconds and 6 minutes after the last activity)
If you have totally left the room for over 8 minutes (6+2) , and the light is still on, then I would check your variable in the piston to make sure {lightStatus} is true.
How do I check the value of the variable? I removed the variable in the second piston and the light does turn off. So, it’s has something to do with the value for sure. I tried the Test but I wasn’t sure how the debug work.