OK. Here’s the code. It ran on the simulator and is running at my house.
I have NOT tested:
- Letting ST figure out the location (I entered the zip code in preferences).
- Sunrise and sunset offsets
I’ve noted some todo items in comments in the code
Description: This is a dusk to dawn light that uses sunrise and sunset information from ST and allows offsets. During sun=down, the dimmer is set to 20% (hard coded but I will make this a settable preference at some point). When motion is sensed, dimmer goes to 100% for 5 minutes (to also be a settable preference eventually) then returns to 20% (or 0% if the sun happened to come up in the meantime).
I used code borrowed from @pstuart (from this thread) and from the published SmartApp ‘Sunrise/Sunset’ along with some stuff I did myself.
If you try this, let me know how it works for you and if you see any issue/opportunities.
/**
* Dusk-to-Dawn Motion-Dimming Light - AaronZON
*
* 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",
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 ("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}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
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(20) //todo: make this a preferences input
state.sunPosition = "down"
}
def handleMotionEvent(evt) {
log.debug "Motion detected . . . ."
log.debug state
if (state.sunPosition == "down") {
switches?.setLevel(100)
state.Level = "99"
log.debug ". . . set the switches to level 100"
log.debug state
runIn(300, dimOrOffafterBright) //todo: make bright time a preference setting
}
else
{
log.debug ". . . but sun is up, so do nothing"
}
}
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(20) //todo: Make dim level selectable in preferences
state.Level = "20"
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
}