Have a device always go back off?

This may be a silly question.

I have a peanut plug that I turn on every now and then both by schedule and by remote (manually).

I would like it to always shut itself back off 2 minutes after it gets turned on.

Can Hubitat somehow do that as a rule or do I need to code that in manually (so to speak).

Thank you.

Rule Machine can do it easy.

Do you want any conditions around it, or is it always on, wait 2 minutes, off?

If it's always the same, a trigger will work.

Trigger: switch turns on
Actions: delay 2 minutes, turn off switch

Like this, just with a different delay value:

If you want more logic around it, please explain and we can help implement.

1 Like

I have about 10 of these rules (turn off light after being on for a period) in Rules Engine. Is there any way to collapse them to one rule? Basically if any of these 10 devices is on for longer then x minutes then turn that specific light off

Nope, gotta do them one at a time.

No, but this is a very simple app in Groovy. You can select as many lights as you want, and each will turn off however many seconds after turning on. See below:

definition(
    name: "Lights Off After on",
    namespace: "hubitat",
    author: "Bruce Ravenel",
    description: "Simple turn off after on lighting automation",
    category: "Convenience",
    iconUrl: "",
    iconX2Url: ""
)

preferences {
    section {
		input "lights", "capability.switch", title: "Select lights to turn on then off", multiple: true, required: true
		input "secs", "number", title: "Select number of seconds before off", defaultValue: 1, description: "1"
    }
}

def installed() {
    initialize()
}

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

def initialize() {
	subscribe(lights, "switch.on", onHandler)
}

def onHandler(evt) {
	runIn(secs, lightsOff, [data: [devName: evt.displayName], overwrite: false])
}

def lightsOff(data) {
	lights.each {if(it.displayName == data.devName) it.off()}
}
9 Likes

Thank you for this quick example. Simple functional apps like this are exactly what I need to start wrapping my head around Groovy.

@reachjeffs It's great to have samples from the Groovy Masters :smiley: Can have a lot of fun with this "App template" -- he's shown the minimum, which is always a great place to start.

add:

input name: "direction", type: "bool", title: "on to off?", defaultValue: true

and you can double the handlers and use this app to always go back On.

def initialize() {
    if (direction) {
	subscribe(lights, "switch.on", onHandler)
    } else {
	subscribe(lights, "switch.off", offHandler)
    }
}

def offHandler(evt) {
	runIn(secs, lightsOn, [data: [devName: evt.displayName], overwrite: false])
}

def lightsOn(data) {
	lights.each {if(it.displayName == data.devName) it.on()}
}
3 Likes

This is perfect! Thanks for the code

Can the timer (runIn) be canceled if the switch is manually turned off? To prevent the case where a switch is manually turned off and back on before the original timer expires? Maybe delete any existing timers for that switch before registering a new one. Does runIn return some type of handle which could be used to delete the timer?

If the light is on and an On is sent anyway, did it break something?. :smiley:

Thanks! I will try this when I get back home after the new year!!

1 Like

Where can I find out what the button; "Convert to Triggered Rule" is used for?

Thanks!!

Trigger just says "when this event happens, do something"

Triggered rule gives you some further options. "When this event happens, if some condition is met, do one thing, otherwise do another".

Take a garage door opening, for example. I want to get a notification when this happens.

If I just use a trigger, I can only say "When garage door opens, speak a message on my Google Home".

But a Google Home notification is no good when I'm not home. So if I convert this to a triggered rule, I can add different actions depending on whether I'm home or not:

"When garage door opens, if I'm home then speak a message on my Google Home, otherwise send a Pushover notification to my phone"

Make sense?

1 Like

You can call unschedule(nameOfFunction) to cancel an existing schedule for that particular function.

So, for Bruce's example, you could cancel the runIn() by calling 'unschedule(lightsOff)'. If you call unschedule() without passing any arguments, it will cancel all schedules for that App or Driver.

Here's the documentation about unschedule()

https://docs.hubitat.com/index.php?title=Common_Methods_Object#unschedule

For a completely different way to solve this problem, you can use a device that includes an automatic shutoff timer.

In my case, I use a Z-Wave smart plug to control a hot water recirculation pump. I always want the pump to shut off after 3 minutes. The plug has the option to set an automatic shutdown time. I programmed the plug to automatically turn off after three minutes anytime it's turned on.

This places the turn off logic in the device. This is perhaps not transparent to someone reading your rules later... "Where in these rules do you turn the device off?"...

Good luck.

OK... another question now here.

So I have this Peanut Plug I call the BingBong (long story).

When it gets turned on, I want to shut it off a couple seconds later- kind of like a reset.

So I thought I could do that with a rule.... whenever the Peanut (a "switch") gets turned on... delay 2s and shut it off... like this:

Not working... can someone teach me how to fish a bit here? Thanks all!

What do you mean, "it's not working"? It doesn't shut the plug off? You don't show the restrictions part of the rule. What is displayed in the black bar at the top next to the rules name?

Thanks for helping!

I have entered no restrictions in the restrictions section..

and at the black bar... it says [True] -- so it seems to me it should have run... right?

This should work...I'm not understanding why it wouldn't. Does BingBong update it's on/off state correctly when it's toggled? Is it turned on digitally, or physically?

Thanks. I was wondering that too ... when I go to the device page itself and tell it to go ON... and then refresh the page... it still says it is off. that is probably bad huh?

I still am having trouble understanding the Triggered Rule thing though and wondering if that is what I should be using... "Convert to triggered rule"