Power Allowance Code?


(Chrisb) #1

I’d like to start dabbling with writes some simple SmartApps. I haven’t done any real programming in MANY years so while I have some back ground in concepts and the ideas, practical application is very weak for me. In the past I’ve learned best by looking at example code and modifying it to my needs and learning based on what I know that app does.

So the first thing I wanted to do was make an app that could turn off a switch specified seconds after it was turned on. I see the power allowance program in the Green Living category which turns off a switch after specified minutes so figured this is a perfect app to copy and then modify. But I don’t see the code in the examples area. Am I just missing it? Does anyone have the code that they could copy here to get me up and running?

Thanks!


(Dan Lieberman) #2

@chrisb – I’m not sure why that didn’t make it into the list of examples. Here’s the code:

/**
 *  Power Allowance
 *
 *  Author: SmartThings
 */

preferences {
	section("When a switch turns on...") {
		input "theSwitch", "capability.switch"
	}
	section("Turn it off how many minutes later?") {
		input "minutesLater", "number", title: "When?"
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribe(theSwitch, "switch.on", switchOnHandler, [filterEvents: false])
}

def updated() {
	log.debug "Updated with settings: ${settings}"

	unsubscribe()
	subscribe(theSwitch, "switch.on", switchOnHandler, [filterEvents: false])
}

def switchOnHandler(evt) {
	log.debug "Switch ${theSwitch} turned: ${evt.value}"
	def delay = minutesLater * 60 * 1000
	log.debug "Turning off in ${minutesLater} minutes (${delay}ms)"
	theSwitch.off(delay: delay)
}

The main thing to look at is the delay keyword. It takes the delay in milliseconds, so for Power Allowance, it multiples the delay value by 1000 to get to seconds, and 60 to get to minutes. In your case since you just want seconds, you’d only multiply it by 1000.

I hope that’s helpful!


(Chrisb) #3

Thanks much man! Very helpful. Got it modified and working. How do I submit something for approval now?


(Dan Lieberman) #4

@chrisb – We’re still working on the process for the submission, review, and publishing of user-generated SmartApps. In the meantime, you can use Publish -> For Me to make it available for installation in your own environment (they show up in the My Apps category in the mobile app), and you can share your code with other developers by going back into the app settings and clicking the “Is Selected” checkbox.


(Mike Import) #5

@chrisb - Did you ever select the option to share with other developers? I need this exact solution for using an Evolve LFM20 to open a garage door. I would like the switch to open again about a second after I close it. If you have chosen to share the modified code, where can I find it? I’m a newbie to this forum.


(Chrisb) #6

@mimport,

I think a better option for you would be to change your device type to a “Z-wave Virtual Momentary Contact Switch.” If I’m not mistaken this would make it so that any time you turn on this device it turns off again after a second.


(Mike Import) #7

@chrisb - Thanks for the quick response. Pardon my really newbie ignorance, but exactly how do I change the device type to a “Z-wave Virtual Momentary Contact Switch?” I am using a Samsung S3 to control my Smart Things. I tried researching the topic but I am stuck at changing the device type.


(Chrisb) #8

@mimport,

This is done in the ide. Navigate to: https://graph.api.smartthings.com/

You need to register for access.

After doing this and signing in, click on the “My Devices” link in the menu across the top. Find the device in the list and click on it to select it. Click on the “edit” button at the bottom.

Click on the drop down arrow in the “type” box and find the entry for Virtual Momentary Contact Switch. Finally, select “update” button at the bottom.

That should do it!


(Mike Import) #9

Wow! Thank you! This was exactly what I needed. I just learned a whole lot! Great!