App that toggles a switch on/off when something is opened?

I have a ceiling fan that I want to turn on when I open a door, then turn off if I open it again. Is there a smartapp already out with this capability?

Yes, smart lighting

20 characters

As @sidjohn1 said, Smart Lighting (sounds odd cause it’s a fan switch, but yes).

For my clarity, you only care about automating the fan only when the door opens and not when it closes? Smart Lighting will do what you want, but you’ll have to set up more than one automation.

The more I think about this the more I’m trying to see how that would work though. It seems like multiple automations to turn on and off based on just a door opening could combat each other.

In other words, when the door opens, ST will run both automations and they’d counter each other’s actions. You’d have to add conditions in Smart Lighting, or write your own SmartApp to check for device states and what actions to take based on that.

This is a sequence where you have to capture the previous state. First open turns on the fan. Second open turns off the fan. I don’t believe this can be done with the smart lighting app.

Pretty sure @bravenel has some logic to do something like this with some of his advanced motion sensor stuff. In terms of serial processing it’s very similar to “first motion turns on the light for 15 minutes unless the light is already on.” It may require adding a virtual switch to store the state.

Too bad Smart Lights doesn’t offer “Toggle” as one of its actions. Because that’s really all that would be needed. Open the door, if the fan is off, turn it on; or, if it’s on, turn it off.

2 Likes

Here is the little app you need. About as simple as they get!

/**
 *  Toggle When Open
 *
 */
definition(
    name: "Toggle When Open",
    namespace: "bravenel",
    author: "Bruce Ravenel",
    description: "Toggle a switch when a contact opens",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")


preferences {
	section("Select Contact and Switch") {
	    input "myContact", "capability.contactSensor", title: "Choose your contact sensor", required: true, multiple: false
            input "mySwitch", "capability.switch", title: "Choose your switch", required: true, multiple: false
	}
}

def installed() {
	initialize()
}

def updated() {
	unsubscribe()
	initialize()
}

def initialize() {
	subscribe(myContact, "contact", contactHandler)
}

def contactHandler(evt) {
	if(evt.value == "open") if(mySwitch.currentSwitch == "off") mySwitch.on() else mySwitch.off()
}
4 Likes

Hmmm now can you take that code and make it so that it does this only after sunset? That would be cool

Well, sure, but before that, a question for you: Do you have a mode that begins at sunset (like “Night”). If you do, you can just use the built-in mode restriction of the app to do it. At the bottom on the install page it says, “Set for specific mode(s)”. By selecting “Night”, it would only run during that mode. That’s way easier than what you’re asking for!

It is possible to add time-of-day restrictions to just about any app. But consider this: this little app is about 8 or 10 lines of code. Adding time-of-day restrictions would add about 100 lines of code. A lot of complexity when just using a mode restriction works quite well.

1 Like

Thanks. This app does the trick.

2 Likes

Works great for my downstairs bathroom :). Only thing I’d like to add is that the light turns off after 20 minutes every time, just in case.

You should check out Rule Machine. It will do this for you…

1 Like

Awesome!! I’m still learning and that link is just what I needed.

I’m new here so bare with me. This is the closest topic I saw that seemed relevant. I just want a motion sensor to toggle a hall light on when I start down it and the next sensor to toggle it off when I get to the end. Then the reverse when I go back the other way. I am using smart lighting but the only choice I see is on or off.
This would be good for entering or leaving a room as well, light on/light off.
Also, I see some people helping with code for writing their own app. I have no idea about that so if you could link a post on how to do that it would be great.

Does it have to be exactly when you get to the end? The usual way to do this is to have the light come on when motion is detected and then have the light go off again after a set period of inactivity. That way it doesn’t matter which end of the hallway you start at. In fact many people will mount the sensor on the ceiling for just this use case.

If all you want is to have the light go off after no motion has been detected for x minutes, you can do that in the official smart lights feature.

If you really need it to be turned off exactly when you reach the end of the hallway in each direction, you are going to require two sensors and it’s going to be a much more complicated set up.

Hello,
My plan was to use two sensors, but I think I like your idea for my purpose better. It is a long hallway so I wonder if one motion sensor would cover the area. If I put it in the middle I might be in the dark until it is triggered. I will try it, thanks.

1 Like

If you need to use two sensors, you can, and then you can link them into one zone and trigger off based on inactivity in the zone. Still easier than trying to treat each sensor separately. :sunglasses: