Here’s the update with the delay not hard coded. The time is a little harder to implement and I don’t have time to QA that sort of change (since I’m actively using this program through out my house). In the future I can look to do that but you are free to change this code anyway you’d like to add that functionality into it.
/**
* Bedroom Night Lights
*
* Copyright 2014 Craig Lyons
*
* 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: "Bedroom Night Lights",
namespace: "",
author: "Craig Lyons",
description: "Used to control the bedroom nightlights.",
category: "My Apps",
iconUrl: "http://www.philips.co.jp/shared/assets/jp/SmallParagraphImage/ConsumerCare/hue_80x70.jpg",
iconX2Url: "http://www.philips.co.jp/shared/assets/jp/SmallParagraphImage/ConsumerCare/hue_80x70.jpg",
iconX3Url: "http://www.philips.co.jp/shared/assets/jp/SmallParagraphImage/ConsumerCare/hue_80x70.jpg")
preferences {
section("Control these bulbs...") {
input "hues", "capability.colorControl", title: "Which Hue Bulbs?", required:true, multiple:true
}
section("Choose light effects...")
{
input "color", "enum", title: "Hue Color?", required: false, multiple:false, options: [
"On - Custom Color",
"Soft White",
"White",
"Daylight",
"Warm White",
"Red","Green","Blue","Yellow","Orange","Purple","Pink"]
input "lightLevel", "enum", title: "Light Level?", required: true, options: ["10","20","30","40","50","60","70","80","90","100"]
}
section("Choose Motion Sensors...") {
input "motionDectors", "capability.motionSensor", title: "Motion"
}
section("Delay...") {
input "offDelay", "number", title: "How many minutes after motion stops until lights turn off"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(motionDectors, "motion", motionHandler)
}
def motionHandler(evt)
{
log.trace "evt: ${evt}"
if (evt.value == "active")
{
log.trace "active"
def hueColor = 70
def saturation = 100
switch(color) {
case "White":
hueColor = 52
saturation = 19
break;
case "Daylight":
hueColor = 53
saturation = 91
break;
case "Soft White":
hueColor = 23
saturation = 56
break;
case "Warm White":
hueColor = 20
saturation = 80 //83
break;
case "Blue":
hueColor = 70
break;
case "Green":
hueColor = 39
break;
case "Yellow":
hueColor = 25
break;
case "Orange":
hueColor = 10
break;
case "Purple":
hueColor = 75
break;
case "Pink":
hueColor = 83
break;
case "Red":
hueColor = 100
break;
}
if (color != "On - Custom Color")
{
def newValue = [hue: hueColor, saturation: saturation, level: lightLevel as Integer ?: 100]
hues*.setColor(newValue)
log.debug "new value = $newValue"
}
else
{
hues*.on()
}
state.motionStoppedAt = -99
}
else if (evt.value == "inactive")
{
log.trace "inactive"
//runIn(60, turnOfflights)
state.motionStoppedAt = now()
turnOfflights()
}
}
def turnOfflights() {
def elapsed = now() - state.motionStoppedAt
def threshold = (60000 * offDelay)
log.trace "elapsed: ${elapsed}"
log.trace "threshold: ${threshold}"
if (elapsed >= threshold && state.motionStoppedAt != -99)
{
hues*.off()
}
else
{
runIn(60, turnOfflights)
}
}