Turn lights off after X time of no motion


#1

I’ve got my lights to come on during specific parameters. When the piston runs, it grabs the bulbs current settings (in case they’ve changed via Hue or something) and then turns them on and sets the values

What I want to do though, i say if there has been no motion detected for 10 minutes, turn the lights off
Any idea how I can implement this?

1


#2

Maybe add another IF statement at the end?
Here’s an example: (ignore the variables at the top, it’s just a piston I use to mock up ideas).


#3

I had assumed that would turn them off after 10 minutes, regardless of if motion was detected again within that 10 minutes?


#4

That 10 minute timer should get cancelled if motion changes back to active.
You would need to set the task cancellation policy to “never cancel tasks” to enforce a 10 minute shutdown.


#5

OK thanks i’ll try that.

I have another dilemma though. When the piston runs, I grab the colour values of the lights and levels and then set those.

When I change the lights to “Read” (yellow/orange) in the Hue app, then the piston triggers, it changes them back to whatever they were this morning. Not yellow.

Any ideas?


#6

I don’t believe the piston is subscribing to color/temp changes in the lights… So if it’s changed by the Hue app, it won’t get automatically populated in those variables…

So I just tested the following using 1 hue light and created a global variable called “@hallwayLevel”.
It automatically populated the bulb’s level into @hallwayLevel when I adjusted the level from within the Hue app.

i think there’s a better way to do it though without using global vars…
Just to be clear… when the piston triggers, you want the colors/levels to be whatever they last were when set in the Hue app?


#7

Ah OK, my thought was that when it ran, the variables were set based on the colors? Or am I mistaken?


#8

I believe the piston has to run first in order to set the variables… Those variables are not dynamically updated everytime an attribute changes. The way the piston is written, it looks like the colors will always stay the same.

you want the colors/levels to be whatever they last were when set in the Hue app?


#9

okay, check this out… I think this is what you’re trying to accomplish… This is just a quick example and you’ll need to tweak it for your own use, but it shows an example of capturing attributes local state… Let me know if it makes sense.


#10

@allrak’s tips look rock solid. The only thing I would like to add is that Philip Hue bulbs are a bit peculiar when restoring a previous setting.

The method I use is to capture only the Hue, Saturation and Level, and later in the code, restoring those 3 elements. For some reason, if you capture the color or colorTemp, it will not return to the exact shade of color.


#11

Yeah I’ve noticed that. I think my code is working actually, but it depends if SmartThings has synced the colour on time. When using the colour option, it gives a hex code. Unfortunately my warm lights are currently orange due to this


#12

That is what I mean. If you use the hex code on Philips Hue, it will not go to the color you want. You have to use Hue, Saturation & Level instead of the hex code to get the right shade.


#13

Not sure this will work if just the colours change though?


#14

The only triggers in that last picture is Levels and Switches, so you are right… If you only change the colors, the piston will NOT run in it’s current state.


#15

OK, I’ve been playing around with the various options and I think this works

When the piston is triggers, the variables at the top are set to what the current state of the bulb is

It then sets the bulbs to these colours and levels and turns them on. If theres no motion within 10 minutes, it’ll turn them off


#16

I am not at a computer at the moment, but I see one issue right away…

Each time this piston runs, you are pushing the current hue levels and saturation to a variable. (this is happening up top in your ‘define’ section) A few lines later in your code, (lines 40-68) you are pushing those SAME numbers back to the bulbs. This baffles me. Why would you grab the current numbers, and then immediately push the same numbers back to the devices?

This may be a dumb example, but that is like looking at your watch, taking note of the time displayed, and then setting that watch to the SAME time… It serves no purpose

Why not simply:

IF Switch 3 is off blah blah blah
Then turn on the 4 bulbs