Motion and button activated light with timers (1 min motion, 3 mins button)


#1

1) Give a description of the problem
I’ve created a piston to handle a garden floodlight. I have a motion sensor and a button that can trigger the light to come on.
All behaviour works as expected, except the light does not turn off after 3 minutes when a short button push turns it on.

2) What is the expected behavior?
If there is motion and the light is off then the light should turn on and turn off again after 1 minute of no motion.
Orange ON and spinning circle and trigger time in piston state
If I push the button and the light is off then the light should come on and stay on for 3 minutes then turn off.
Orange ON and spinning circle and button push time in piston state
If I push the button and the light is on then it should turn off
Green OFF and last motion trigger time and date in piston state
If I hold the button and the light is off then the light should turn on and stay on
Red ON in piston state
If I hold the button and the light is on, nothing should happen

3) What is happening/not happening?
Everything works as expected…except if i short press the button. In this situation the light comes on and the piston state is set correctly, but it never turns off again

I suspect its something to do with the Every day at buttonTrigger time line…I set buttonTrigger to timerButton minutes later than $now when the button is pushed and that time i’d like to use as the trigger to perform the turn off part

4) Post a Green Snapshot of the pistonimage

5) Attach any logs (From ST IDE and by turning logging level to Full)
The Every day at buttonTrigger time section is not triggered…


#2

According to your logic, it will only turn off automatically if motiontrigger is true so it won’t turn off automatically when turned on by the button. If that is your desired result, remove the motiontrigger conditional from that if.


#3

There are two triggers for turning off the lights
The one I think you’re looking at is the motion one which works correctly

At the bottom of the piston is a trigger based on buttonTrigger which is a time set when the button is used to turn the light on rather than the motion sensor…this isn’t firing.
My guess is it’s a limitation of the way you can set a time based event…namely having an every day, month or Year part rather than a pure ‘at this datetime’


#4

Ahh. I see your issue. This was a problem discussed on another thread somewhere. I can try to find it later but the bottom line is the every day won’t get reset to the to e you set until the next time the piston executes that trigger. Without an initial value I am not sure if that will ever trigger. If you just always want it on for a set time, just use wait for timerbutton interval and then turn off. That will schedule the task just as you are trying to accomplish.


#5

Yep, that appears to have done the trick, thanks…

Now - because I always like to fiddle…I need to trap events incase someone turns the lights on or off from the ST app or webcore dashboard so I can have the piston reflect the light state properly…i.e. manually on, or off
That’ll give me something to think about today :slight_smile:


#6

So I’ve simplified the piston (at least I think so), trying to separate the triggers from the actual switching…so I don’t have so much repeated code

i can’t seem to fault the logic but a button push now no longer seems to cause the {deviceLight}'s switch changes trigger to fire…

e.g. from logs:

23/01/2019, 11:57:17 +219ms
+201ms ║Switch state change: off
+278ms ║Light Off: Motion: false, Button: true
23/01/2019, 11:54:10 +888ms
+161ms ║Button push
23/01/2019, 10:57:36 +540ms
+191ms ║Switch state change: off
+252ms ║Light Off: Motion: false, Button: false
23/01/2019, 10:57:26 +34ms
+206ms ║Switch state change: on
+291ms ║Light on: Motion: false, Button: false

bottom two are from using dashboard to turn lights on/off, top two are what fires when button is pushed
The light does switch on…so the state does change…but the fact I’m not seeing: Light on: Motion: false, Button: true shows the trigger at the bottom isn’t firing…

It was a bit simpler still, but i’ve added back some logging and changed some toggles to definite turn on/turn offs in conditional statements to try and troubleshoot the fact that that 'if {deviceLight}‘s switch changes’ trigger not firing


#7

I can’t fathom it…I wondered if it was because I also have the switch in another piston monitoring state and power…but that one works reliably…and when i removed it from there it didn’t help this piston…

Then I wondered if there’s something preventing it subscribing to the events as the webcore UI only shows 3 subscribed events…whereas i think there should be at least 5
image


#8

So I created another piston with only the on events from floodlights switch - log to console


Pushed the button while I had two browser windows open…one with the original piston, one with the new short one
the original piston on events section doesn’t fire…the new piston does…

pushing the button from on to off does appear to be firing the event in the original piston…it just makes no sense…

Does anyone have any thoughts?


#9

OK, my own thoughts…

It seems that the deviceList switch action going to on does trigger an event…but when the piston runs to do something about it…it hits the if buttonTrigger is true condition first…processes that and completes…but never reaches the on events from section of code

I guess i have to nest the if button trigger is true inside the on events piece…

–EDIT-- Yep, that appears to work…


#10

Sorry I was out all day so am just seeing all your posts. One thing to keep in mind when writing pistons is no matter what trigger happens, the piston executes from top to bottom. So even if the very last trigger fires, the execution starts for the top. The only exception to that rule is if you use a timer in which case it only executes that portion of the code and will not continue through to anything below.


#11

Thanks for your help @guxdude.
Interestingly, from your comment about the piston executing top to bottom…that’s what I see…but it seems that if it finds a matching condition earlier in the piston it won’t continue out of that condition and reach the event that caused the piston to fire in the first place…
Now I’ve moved the buttonTrigger condition inside the event section (I guess I could have moved if after too) the piston once fired by the on events from is actually reaching and executing that correct piece of code

Here’s my finished piston which works well now


#12

Glad it is working. Not sure why it wasn’t getting to the bottom of your piston. It is supposed to go all the way through unless something above changes the condition before it gets there. Anyway, glad you have it working.