There must be an easier way to do a cancelable auto on and off light based on arrival

I wanted my garage light to come on as I drove up then automatically turn off after 8 minutes but also cancel the timer if we shut it off ourselves as we walked in. I struggled my way to this mess of a code and it is finally working but it just seems like there should be a much simpler way to do this. Did I overlook a much easier implementation?

1 Like

Why cancel the rule if you turn the lights out? I have a rule in my bedroom that's similar: Lights are triggered on via motion. I can either turn them off using a switch on my way out, or they will go out after a given length of time. But if I turn them off, I don't bother to cancel the rule's actions. The worst that will happen is that the rule will turn off a light that's already off. No harm, no foul. I do use private boolean to keep the lights on if there's further motion. Here's my rule, in case it's helpful. It might look weird because I turn on different lights based on the mode.

The easiest way would have been the motion lighting app.

https://docs.hubitat.com/index.php?title=Motion_Lighting_Apps

I guess I was thinking of that extremely rare case that someone might walk back to the garage to do something and the light would get turned off on them. This becomes more of a problem the longer your delay time is. Mostly I just wanted to program something very thorough to learn how to do it.

I haven't tried that one, I'll have to install it. The description makes it sound like its for use with motion sensors.

Sorry. I thought you were using it with a motion sensor. However, this simple code will allow you to convert a switch closing to motion. Easily modified to be anything you want to motion.

metadata {
definition (name: "Virtual motion with Switch", namespace: "cw", author: "cwwilson08") {
capability "Sensor"
capability "Motion Sensor"
capability "Switch"
}
}

def on() {
sendEvent(name: "motion", value: "active")
sendEvent(name: "switch", value: "on")
runIn(12, off)
}

def off() {
sendEvent(name: "motion", value: "inactive")
sendEvent(name: "switch", value: "off")
}

def installed() {
}

I’m in a car on mobile and can’t paste the code properly. Use the code from this link instead.

Thanks! Good to know.

1 Like