I have a professional need to become familiar with how ST and the ST community works so I bought some stuff and looked for an opportunity to do something and this is what I found. I was surprised not to find a simple way to implement a dusk-to-dawn motion light in ST that does what non-connected motion lights that you buy at the hardware store do. So, I hijacked this thread, and ended up writing my own solution starting with some code @pstuart started, borrowing from Sunrise/Sunset, taking some suggestions from @kgofswg1973 and adding some of my own code (copied below). I’d like now to share this with others.
Description: Turn lights on at dusk at a dimmed level, brighten when motion is sensed temporarily. Turn off at dawn. Adjustable dim level, bright level, bright time, dawn offset, dusk offset. Dusk and dawn times based on location (either zip code or hub location)
I would be delighted if anyone would like to try this and provide feedback/suggestions. I would like to know if anyone else thinks this should be included as one of the standard SmartApps. I know there are other ways to do this leveraging ‘modes’ and such but I think this is much easier. I’m new to ST myself and I still haven’t been able to get my head completely around modes and how to use them to make my life easier.
Also, I’m not sure how to share this with you other than copy/paste into the post. I selected the ‘share’ option when I set this up but I don’t see it when I navigate the shared apps. I’m not sure what I’m doing wrong here.
I’d like to have an icon for this. I’ve read where @Ben was offering to assign icons to apps. Seems like a light bulb with stars/moon behind would make a good icon for this.
Finally, other than making noise here, I don’t see that there is a process to promote an app from the community into the mainstream. If there is a ‘submission process’ that I just haven’t found, please point me in the right direction. I realize ST has to carefully curate what gets promoted to the mainline but if there is a process, I would like to know how get that started.
EDIT: There is a process. I will explore this once I make any final edits.
Finally (really, this time), if no one cares, I’m OK with that too. I’ve got what I needed from the experience already.
The Code. Dusk-to-Dawn Dimming Motion Light - RC1 [EDIT: this not current, please continue down thread for improved versions]
/**
* Dusk-to-Dawn Dimming Motion Light - RC1
*
* Written by Aaron Herzon and based on code by Patrick Stuart and SmartThings with contributions from the SmartThings community
*
*
* 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 Dimming Motion Light - RC1",
namespace: "AaronZON",
author: "Aaron Herzon",
description: "Turn lights on at dusk at a dimmed level, brighten when motion is sensed temporarily. Turn off at dawn. Adjustable dim level, bright level, bright time, down offest, dusk offset. Dusk and dawn times based on location (either zip code or hub location)",
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")
//todo: replace icon with something appropriate. Maybe a lightbulb with the moon and stars
preferences {
section("Select Motion Sensor(s) you want to Use") {
input "motions", "capability.motionSensor", title: "Motion Detectors (leave blank for just dusk-to-dawn function)", required: false, multiple: true
}
section("Select Dimmers you want to Use") {
input "switches", "capability.switchLevel", title: "Dimmer Switches", required: false, multiple: true
}
section ("Set Bright and Dim Levels and Bright Time") {
input "DimLevelStr", "enum", title: "Dimmed Level %", required: true,
options: ["10","15","20","30","50","75"], defaultValue: "20"
input "BrightLevelStr", "enum", title: "Bright Level %", required: true,
options: ["100","75","50"], defaultValue: "100"
input "DelayMinStr", "enum", title: "Bright time, minutes", required: true,
options: ["1","3","5","10","15","30","60"], defaultValue: "5"
}
section ("Zip code (optional, defaults to location coordinates)...") {
input "zipCode", "text", title: "Enter 5-digit ZIP code", required: false
}
section ("Sunrise offset (optional)...") {
input "sunriseOffsetValue", "text", title: "Offset amount in the format HH:MM", required: false
input "sunriseOffsetDir", "enum", title: "Before or After", required: false, options: ["Before","After"]
}
section ("Sunset offset (optional)...") {
input "sunsetOffsetValue", "text", title: "Offset amount in the format 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() {
state.DimLevel = DimLevelStr as Integer
state.BrightLevel = BrightLevelStr as Integer
state.DelayMin = DelayMinStr as Integer
subscribe(location, "position", locationPositionChange)
subscribe(location, "sunriseTime", sunriseSunsetTimeHandler)
subscribe(location, "sunsetTime", sunriseSunsetTimeHandler)
subscribe(motions, "motion.active", handleMotionEvent)
subscribe(motions, "motion.inactive", handleEndMotionEvent)
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)) { //before midnight, after sunset
sunsetHandler()
log.info "Sun is already down, run sunsetHandler"
}
else
{ if (riseTime.after(now)) { //after midnight, before sunset
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 . . . ."
if (state.sunPosition == "down") {
switches?.setLevel(state.BrightLevel)
state.Level = state.BrightLevel
log.debug ". . . set the dimmers to level $state.BrightLevel"
}
else
{
log.debug ". . . but sun is up, so do nothing"
log.debug state
}
}
def handleEndMotionEvent(evt) {
log.debug "Motion stopped . . . ."
if (state.sunPosition == "down") {
switches?.setLevel(state.BrightLevel) //does nothing unless sun went down during active motion
state.Level = state.BrightLevel
log.debug ". . . set the dimmers to level $state.BrightLevel if not already there"
runIn((state.DelayMin*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(state.DimLevel)
state.Level = state.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
}