Trying to write Nap App

I would like to write a basic app that allows me to set it to ‘nap mode’, which would turn off selected lights and switches for X amount of time and then have the lights and switches come back on or at least change the mode back to “home”.

I am a noob when it comes to code and I have been struggling to find a way to have a delay on the lights/switches, I was able to figure out a delay for turning them off but not for turning them on.

Can anybody help me by writing something that I can work with or atleast point me in the right direction?

Thanks in advance.

You could create a Hello Home Action that sets switches, temps etc then you can call it automatically at a time or manually. This solves the first half of your question.

The 2nd half would be more tricky, if the nap ends at the same time everyday you can set another hello home action to perform actions at a set time too.

@DigitalM0nkey
It really wouldnt be too hard to write in a SmartApp. It would just schedule turn on times. I am actually working on a physical alarm clock project with ST. Perhaps Ill wrap a nap time in with it.

Could you tell me more about what you would like the app to do?

@DigitalM0nkey

Give this a shot as a starting point. It asks for minutes and switches. It turns them off immediately when youve updated the app. Then it does a runIn for that many minutes multiplied by 60. Bear in mind that runIn is not reliable over 60 minutes. You would need to use schedule

http://www.docwisdom.com/taking-nap-smartthings/

/**
 *  Nap Time
 *
 *  Copyright 2014 Brian Critchlow
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Nap Time",
    namespace: "docwisdom",
    author: "Brian Critchlow",
    description: "Turns off selected switches for a duration of time and turns them back on after the nap. ",
    category: "Health & Wellness",
    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("Nap for:"){
		input "time1", "number", title: "Minutes?", required: true
	}
    section("And control:"){
		input "switches", "capability.switch", title: "These lights", multiple: true
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
    initialize()
}

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

}

def scheduleCheck()
{	
	log.debug "lights on"
	switches.on()    
	unschedule("scheduleCheck") 
}

def initialize() {
    log.debug "lights off"
	switches.off()
	def seconds = time1 * 60
    log.debug "scheduled to run in ${seconds} seconds"
    runIn(seconds, "scheduleCheck")
}

@docwisdom
I think this is exactly what I am looking for. I’ll try it out and let you know. I can’t believe you whipped it up so quick. :smile: Thank you.

Also thank you @mattjfrank for your suggestions.

@docwisdom
You are definitely on the right track and It worked fairly well. However, I did come across a small issue that I’m sure you would have no problems correcting.
I have a motion sensor in the room (and cats) and I found that due to other settings that if motion is detected it turns the lights back on again (interrupting my nap) . Perhaps, it is possible to tell it go into a nap ‘mode’ for x amount of minutes rather than select which lights and switches.

@DigitalM0nkey its just a matter of adding in a mode preference, and doing setLocationMode(completionMode) when the scheduled function runs.

/**
 *  Nap Time
 *
 *  Copyright 2014 Brian Critchlow
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
definition(
    name: "Nap Time",
    namespace: "docwisdom",
    author: "Brian Critchlow",
    description: "Turns off selected switches for a duration of time and turns them back on after the nap. ",
    category: "Health & Wellness",
    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("Nap for:"){
		input "time1", "number", title: "Minutes?", required: true
	}
    section("And control:"){
		input "switches", "capability.switch", title: "These lights", multiple: true
	}
    section("Change mode on completion:") {
		input(name: "completionMode", type: "mode", title: "To", description: null, required: false)
	}

}

def installed() {
	log.debug "Installed with settings: ${settings}"
    initialize()
}

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

}

def scheduleCheck()
{	
	log.debug "mode changed to ${completionMode}"
	if (completionMode) {
		setLocationMode(completionMode)
	}   
	unschedule("scheduleCheck") 
}

def initialize() {
    log.debug "lights off"
	switches.off()
	def seconds = time1 * 60
    log.debug "scheduled to run in ${seconds} seconds"
    runIn(seconds, "scheduleCheck")
}

@docwisdom It changes the mode now to ‘Nap Time’ which fixed the issue with the motion sensor. However, it does not change the mode back after the nap, which unfortunately results in the lights staying off. Also I noticed that in my Hello Home events log, nothing gets listed.

Regardless, I want to thank you for all the work you put into this, it has given me something that I can start to work with and play around with (but mostly try and learn with).

If you find that you have some spare time to have a look at these issues I would of course be grateful but as I mentioned earlier I am just so happy that I have something to work with.

Interesting…the code shouldnt change your mode at the beginning of the nap, it should only be changing at the end of the nap…
It should turn off the switches you selected manually, then when the nap is over it should change the mode to something like “Awake” that would turn your items back on. If you want to add another line for a nap mode, just duplicate the input line for mode and give it another name, add that mode to the initialize() function.

Its possible to give Hello Home commands instead of changing the underlying mode. Its up to you.

Just change

input(name: "completionMode", type: "mode", title: "Change ${location.name} Mode To", description: null, required: false)

and

if (completionMode) {
	setLocationMode(completionMode)
}

to

input(name: "completionPhrase", type: "enum", title: "Execute The Phrase", description: null, required: false, multiple: false, options: location.helloHome.getPhrases().label)

and

if (completionPhrase) {
	location.helloHome.execute(completionPhrase)
}

I robbed all of this code from Gentle Wake Up BTW