SmartThings Community

Turn on a switch but under specific conditions?

(Ian) #1

Hi all!

New SmartThings user here. Wanted to run an idea by everybody to see if the idea was even possible. I have a Sighthound camera system, the SmartThings hub, and some GE Z-wave switches. Again, just starting out with HA.

Using IFTTT, I can turn on a SmartThings switch when a Sighthound rule is triggered. Very easy stuff and it works great. BUT… (always a “but” right?)

…I was hoping to play around with virtual switches and SmartApps to add some more functionally to the above HA. The issues I have with the above is:

  • The light will come on even during the day (no point to that)
  • The light stays on until manually turned off. Even if I can automate turning off the light, what if the light was already manually on? If already manually on, it should have to be manually turned off.

So what I would like it to do is, turn on the switch when the rule is triggered but…
- Only turn on the light at night (Sunset to Sunrise)…
- But only if the light is already off
(basically, if it is during the day or if the light is already on, do nothing)
- And if the light is turned on via this App, auto turn off the light after X minutes
(basically, if the light was already on, don’t auto-off it but auto-off the light if it was turned on via this Sighthound trigger)

I am not even sure if all the above would be possible with SmartThings(?) I came up with the below flow but I am sure my logic is wrong (or there is a better way to do it):

  • Using IFTTT = If Sighthound rule is triggered, then turn on SmartThings “virtual switch 01”

  • “Virtual switch 01” checks to see if it is between sunset and sunrise:

    • if during the day, do nothing (exit out)
    • if at night, run “already on?” SmartApp
  • The “already on?” SmartApp:

    • If already on, do nothing (exit out)
    • If off, call Virtual switch 02 (or maybe another SmartApp? See below)
  • “Virtual switch 02”

    • turns on real switch(es)
  • A “lights auto off” smart app is associated to “virtual switch 02” that would turn off the real switch after X minutes.

Thoughts? Am I going down the correct path or am I way off?



Have you checked out CoRE? It’s a pretty flexible app that can do a lot of things, it just needs a little getting used to. There’s a help thread also if you get stuck creating/setting up your first pistion.

(Ian) #3

Thanks @eibyer! I did not know about that. Will check it out.


Sounds like everything CoRE would be able to implement. However, I don’t believe it has app integration which would allow your “- And if the light is turned on via this App, auto turn off the light after X minutes”. I only use it for a few things so someone that knows it more could answer that part. You could use your virtual switch as the “app”


You’re welcome. This is the help thread if you get stuck…


In CoRE you can specify the interaction with the light, physical or digital (not sure of exact term), using that you can distinguish if the light was turned on manually or if an app/smartapp did it.

(Ian) #7

CoRE looks REALLY cool but a bit over my head (for now). Will totally look into it though.

Just for my own learning, I think I got some of it to work (with help from a post I found by @bravenel - thanks!!)

Basically, I have the Sighthound rule trigger a virtual switch that checks the physical (real) switch if it is on or off.

If the physical switch is on, then the virtual switch is turned off (getting ready for next time it is triggered) but it does not turn off the physical switch.

If the physical switch if off, then the virtual switch will turn on the physical switch, wait X amount of minutes, then turn the virtual switch off (which turns off the physical switch).

Curious if what I did is “best practice” for coding with SmartThings. For example, I have it looking for state and have two “if” statements in the same def. It works but is that the best way to do it?

Thanks again everybody for the help!

preferences {
    section("Physical Switch") { 
        input "physicalswitch", "capability.switch", 
		title: "Physical Switch", 
        required: true
    section("Virtual Stand-in 1") {
    	input "standin1", "capability.switch",
        title: "Stand In Virtual Switch 1",
        required: true
	section("Auto turn it off in how many minutes?") {
		input "minutesLater", "number",
        title: "Auto Off?",
        required: true

def installed() {
    subscribe(standin1, "switch.on", switchOnHandler)
    subscribe(standin1, "", switchOffHandler)
def updated() {
    subscribe(standin1, "switch.on", switchOnHandler)
    subscribe(standin1, "", switchOffHandler)

def switchOnHandler(evt) {
    state.wasOn = physicalswitch.currentValue("switch") == "on"
    state.wasOff = physicalswitch.currentValue("switch") == "off"
            def delay = minutesLater * 60
            runIn(delay, switchOffHandler2)

def switchOffHandler(evt) {

def switchOffHandler2(evt) {


I just did something very close to this.

IFTTT to turn on a virtual switch if my front door Nest Cam detects motion.

Smart Lighting SmartApp turns off virtual switch if it’s turned on.

CoRE to turn on the porch lights if the virtual switch in turned on AND it’s 15 minutes after sunset and before sunrise AND the porch light is not already on. CoRE then turns off front porch light after a few minutes.

Even though Smart Lighting turns off the virtual switch immediately, CoRE still sees the turn on and fires.

Now, what I’d really like is to be able to make the initial trigger based on motion in certain zones (you can set zones with Nest) so the light only turns on when someone approaches the door and not for every vehicle that drives by but that’s an IFTTT/Nest limitation.


@Nezmo I don’t know your full configuration but couldn’t you use a virtual momentary button instead of a switch? It would cut out the Smart Lighting step and should still fire your CoRE piston.


Hmmm, I’ll take a look at that, thanks.

To the original poster, sorry for hijacking your thread.

(Ian) #11

No problem at all. I am assuming I’ll go the CoRE route at some point.


I thought CoRE was a bit over my head too but it really only took me about 30 minutes to setup. The directions in the wiki are perfect


If your familiar with setting up IFTTT rules, then setting up CoRE rules will be very similar.

(Ian) #14

So the last part of this was to have my SmartApp only run at night. This part was a struggle (was trying to make my own Routines, Modes, and SmartApp for this) until I found a SmartApp already made for this purpose! Won’t go into what I tried to do (reinvent the wheel) just will go into what I am doing now.

First, I deleted the default routines and made 4 of my own called:

Away Day
Home Day
Away Night
Home Night

Also deleted the default “Modes” and created 4 with the same names. Took me forever to figure out that I had to use the website to do this as it is now missing from the iOS App.

Each Routine is configured to change to the Mode of the same name. Other then that, the routines are (currently) not configured for anything else as the next part takes care of when they should run.

In the SmartApp Marketplace, I found a SmartApp called “Routine Director”. I then configured Routine Director to monitor my phone (will monitor my wife’s soon), set the four routines to match the SmartApp, set the false alarm threshold, and my ZIP code for sunrise/sunset.

I then went back to my switch SmartApp (from the post above) and configured it to run only during “Away Night” and “Home Night”. It works great! I see it triggered during the day but the light does not come on. If triggered at night, the entire SmartApp runs and the lights work as expected.

The only issue I have is if someone triggers the virtual switch during the day, my SmartApp does not run so the VS never gets turned back off. Ends up that the VS is on all day but the Physical switch is off. Probably a few way to solve this - just haven’t had to time to think thru them yet (project for next weekend).

Thanks again for all the help and suggestions! Also going to check out CoRE now that I have a better understanding on how SmartThings works.


Try changing your virtual switch to a momentary button. This way it will always receive the on command

(Ian) #16

Thanks @rsimp00 . I’ll read up on that and give it a shot.

(Ian) #17

So the momentary button looks like it would work but I am afraid of continuity issues (if there even could be any?) so I went a different route. I made an additional SmartApp that only runs during the day (the other SmartApp only runs at night). All this does is turn off the virtual SW if the virtual SW comes on.

Set this SmartApp to only run during “Away Day” and “Home Day”

preferences {
    section("Virtual Stand-in 1") {
    	input "standin1", "capability.switch",
        title: "Stand In Virtual Switch 1",
        required: true

def installed() {
    subscribe(standin1, "switch.on", switchOnHandler)
def updated() {
    subscribe(standin1, "switch.on", switchOnHandler)

def switchOnHandler(evt) {
    state.wasOn = standin1.currentValue("switch") == "on"

(Wejammin14) #18

Hi i’m new to all of this and am looking for some help.

I have a media room with a motion sensor that will turn on the light to 30% when you walk in, but when i’m watching a movie and the light is dimmed down to 10% and someone gets up the lights go back to 30%. I’m looking for a way to create a rule or add to my existing rule where i can say if the media room light is on and motion is detected do nothing.

Can someone please help or point me in the right direction?