OK, I had some problems today that, apparently, are just problems when simulating in the IDE. Yikes. Anyway, things went a lot better when I started working on the metal here at home. Here’s what I’ve done:
Taking cues from @madtnotn, I’ve made the dim level and bright levels adjustable. Rather than making them entries, I made them selectable from a list of what seems like (to me) reasonable options. Also, I made the delay (bright time) adjustable. I set up the input as minutes and multiplied the value x 60 in the code where I schedule the end of bright time with a runIn()
I also handled the issue of certain lights (at least, mine) that will not start at a low dim level but operate just fine there once lit. At sunset, the lights turn on a 30%, then 3 seconds later I run the handler I use for the end of the delay which sets it back to the selected dim setting. This happens only once at sunset.
What’s not done: I have not implemented @kgofswg1973 suggestion to key the delay after the motion stops. I think I know how I want to do that and will when I get a chance. Also, I would like the delay (bright time) setting to be from a list but I haven’t been able to get that to work. Some issue with data type handling but I don’t have the skills right now to really understand this.
Here is the code:
/**
* Dusk-to-Dawn Motion-Dimming Light - AaronZON - Dev Branch
*
* Copyright 2014 Aaron Herzon
*
* 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: "Dusk-to-Dawn Light - AaronZON - Dev Branch",
namespace: "AaronZON",
author: "Aaron Herzon",
description: "Dusk to dawn light with dim/bright motion sensing ",
category: "Safety & Security",
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("Select Motion Sensor(s) you want to Use") {
input "motions", "capability.motionSensor", title: "Motions", required: false, multiple: true
}
section("Select Dimmers you want to Use") {
input "switches", "capability.switchLevel", title: "Switches", required: false, multiple: true
}
section ("Set Bright and Dim Levels") {
input "DimLevel", "enum", title: "Dimmed Level", required: true, options: [10,15,20,30,50,75]
input "BrightLevel", "enum", title: "Bright Level", required: true, options: [100,75,50]
input "DelaySec", "number", title: "Bright time, minutes", required: true //todo: figure out how to make this a selectable list
}
section ("Zip code (optional, defaults to location coordinates)...") {
input "zipCode", "text", required: false
}
section ("Sunrise offset (optional)...") {
input "sunriseOffsetValue", "text", title: "HH:MM", required: false
input "sunriseOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
}
section ("Sunset offset (optional)...") {
input "sunsetOffsetValue", "text", title: "HH:MM", required: false
input "sunsetOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
}
}
def installed() {
log.debug "Installed with settings: ${settings} DelayMin: $DelayMin"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings} DelayMin: $DelayMin"
unsubscribe()
//unschedule handled in astroCheck method
initialize()
}
def initialize() {
subscribe(location, "position", locationPositionChange)
subscribe(location, "sunriseTime", sunriseSunsetTimeHandler)
subscribe(location, "sunsetTime", sunriseSunsetTimeHandler)
subscribe(motions, "motion.active", handleMotionEvent)
initialSunPosition()
astroCheck()
}
def locationPositionChange(evt) {
log.trace "locationChange()"
astroCheck()
}
def sunriseSunsetTimeHandler(evt) {
log.trace "sunriseSunsetTimeHandler()"
astroCheck()
}
def initialSunPosition() {
//Determine if sun is down at time of initializtion and run sunsetHandler() if so
def s = getSunriseAndSunset(zipCode: zipCode, sunriseOffset: sunriseOffset, sunsetOffset: sunsetOffset)
def now = new Date()
def riseTime = s.sunrise
def setTime = s.sunset
log.debug "riseTime: $riseTime"
log.debug "setTime: $setTime"
log.debug "Now: $now"
if(setTime.before(now)) {
sunsetHandler()
log.info "Sun is already down, run sunsetHandler"
}
else
{ if (riseTime.after(now)) {
sunsetHandler()
log.info "Sun is already down, run sunsetHandler"
}
}
}
def astroCheck() {
//query sunset and sunrise times with offsets applied, schedule handlers for sun events
//this method lifted from Sunrise/Sunset with some mods and error corrections
def s = getSunriseAndSunset(zipCode: zipCode, sunriseOffset: sunriseOffset, sunsetOffset: sunsetOffset)
def now = new Date()
def riseTime = s.sunrise
def setTime = s.sunset
log.debug "riseTime: $riseTime"
log.debug "setTime: $setTime"
log.debug "Now: $now"
if (state.riseTime != riseTime.time) {
state.riseTime = riseTime.time
unschedule("sunriseHandler")
if(riseTime.before(now)) {
state.riseTime = riseTime.next()
}
log.info "scheduling sunrise handler for $state.riseTime"
//todo: resolve issue with date formated as Epoch sometimes in log
schedule(state.riseTime, sunriseHandler)
}
if (state.setTime != setTime.time) {
state.setTime = setTime.time
unschedule("sunsetHandler")
if(setTime.before(now)) {
state.setTime = setTime.next()
}
log.info "scheduling sunset handler for $state.setTime"
//todo: resolve issue with date formated as Epoch sometimes in log
schedule(state.setTime, sunsetHandler)
}
}
def sunriseHandler() {
log.info "Executing sunrise handler"
switches?.setLevel(0)
state.sunPosition = "up"
}
def sunsetHandler() {
log.info "Executing sunset handler"
switches?.setLevel(30) //starts at 30 (arbitrary) to make sure lights start (my LEDs won't start at below 20% but run fine)
state.sunPosition = "down"
runIn(3, dimOrOffafterBright) //after initial light-off, runs handler to set to selected dim level after 3 seconds
}
def handleMotionEvent(evt) {
log.debug "Motion detected . . . ."
log.debug state
if (state.sunPosition == "down") {
switches?.setLevel(BrightLevel)
state.Level = BrightLevel
log.debug ". . . set the switches to level $BrightLevel"
runIn((DelaySec*60), dimOrOffafterBright) //delay is number of minutes entered in preferences x 60 to get seconds
}
else
{
log.debug ". . . but sun is up, so do nothing"
log.debug state
}
}
def dimOrOffafterBright() {
//Handles the case where the sun comes up during bright time
log.debug "Bright delay is complete, decide to turn off or dim based on sun position and offsets"
if (state.sunPosition == "down") {
switches?.setLevel(DimLevel)
state.Level = DimLevel
log.debug "Return to dimmed states since sun is down"
log.debug state
}
else
{
switches?.setLevel(0)
state.Level = 0
log.debug "Turned off lights since sun came up during bright time"
log.debug state
}
}
private getLabel() {
app.label ?: "SmartThings"
}
private getSunriseOffset() {
sunriseOffsetValue ? (sunriseOffsetDir == "Before" ? "-$sunriseOffsetValue" : sunriseOffsetValue) : null
}
private getSunsetOffset() {
sunsetOffsetValue ? (sunsetOffsetDir == "Before" ? "-$sunsetOffsetValue" : sunsetOffsetValue) : null
}