Keeping a light on after Dark

(Chrisb) #1

This little light of mine… I’m going to let it shine! This little light of mine…

Someone asked for this app recently and a had a few moments so whipped it up. Thought I’d share it in case anyone else has a similar need. I call it Little Light as it sorta reminds me of the old Bible School song. The idea was to keep a light on after dark, even if someone turns it off accidentally.

You specify a switch and after sunset it watches to see if the light/switch gets turned off. If it does, it turns it back on a given number of minutes later. I also added the option to turn the switch on at Sunset and off at Sunrise… but I haven’t tested those features yet, so if someone could test drive it for me and let me know if it works, that’d be great.

The program is in the Shared Apps, Safety & Security section. Called Little Light. Here’s the code as well:

 *  LittleLight
 *  Copyright 2014 Chris B
 *  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:
 *  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.
    name: "LittleLight",
    namespace: "sirhcb",
    author: "Chris B",
    description: "This Little Light of Mine: Turn on a light after sunset if it's been accidentally turned off.",
    category: "Safety & Security",
    iconUrl: "",
    iconX2Url: "",
    iconX3Url: "")

preferences {
     section("When this switch is turned off..."){
		input "switch1", "capability.switch", title: "Which?"
	 section("Turn it back on after how many minutes?") {
        input ('time', 'number', title: 'Enter 0 to turn on immedately')
     section("But only after dark for this area.") {
    	input ('zip', 'number', title: 'Zip Code')
     section("Automatically turn....") {
     	input ('onAtSet', 'bool', title: 'On at Sunset?')
        input ('offAtRise', 'bool', title: 'Off at Sunrise?')

def installed() {
	initialize()				// Run the initialization procedure

def updated() {
	unsubscribe()				// Unsubscribe to any subscriptions
//	unschedule()				// Normally we'd also unschedule becuase astrocheck is a schedule procedure, but unschedule handled in astroCheck method/procedure already
	initialize()				// Run the initialization procedure

def initialize() {
	subscribe(switch1, "", switchOffHandler)

	subscribe(location, "position", locationPositionChange)
	subscribe(location, "sunriseTime", sunriseSunsetTimeHandler)
	subscribe(location, "sunsetTime", sunriseSunsetTimeHandler)

def switchOffHandler(evt) {

	def data = getWeatherFeature('astronomy', as String)
    def sunsetTime = data.moon_phase.sunset.hour + ':' + data.moon_phase.sunset.minute
    def sunriseTime = data.moon_phase.sunrise.hour + ':' + data.moon_phase.sunrise.minute
    def currentTime = data.moon_phase.current_time.hour + ':' + data.moon_phase.current_time.minute
    def localData = getWeatherFeature('geolookup', as String)
    def timezone = TimeZone.getTimeZone(localData.location.tz_long)
	log.debug( "Sunset today is at $sunsetTime" )
	log.debug( "Sunrise today is at $sunriseTime" )

	log.debug ("$evt.value: $evt, $settings")
 	def startTime = timeToday(sunsetTime, timezone)
    def endTime = timeToday(sunriseTime, timezone) 
	if (now() < startTime.time && now() > endTime.time) 
    	log.debug ("Too much light, not turning back on" )
    else {       
    	log.trace ("Turning back on soon...")
        def delay = time * 60  /* runIn uses seconds, so multiply time (minutes) * 60 (for seconds)  */
        runIn (delay, switchOn)

def switchOn (){

def locationPositionChange(evt) {
	log.trace "locationChange()"

def sunriseSunsetTimeHandler(evt) {
	log.trace "sunriseSunsetTimeHandler()"

def astroCheck() {
	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"

	if (state.riseTime != riseTime.time) {

		if(riseTime.before(now)) {
			riseTime =

		state.riseTime = riseTime.time "scheduling sunrise handler for $riseTime"
		schedule(riseTime, sunriseHandler)

	if (state.setTime != setTime.time) {

	    if(setTime.before(now)) {
	        setTime =

		state.setTime = setTime.time "scheduling sunset handler for $setTime"
	    schedule(setTime, sunsetHandler)

def sunriseHandler() { "Executing sunrise handler"
	if (offAtRise) {
		runIn (60,					// A slight delay in put in to prevent a possible loop of the switch turning off and then back on repeatedly.

def sunsetHandler() { "Executing sunset handler"
	if (onAtSet) {