Sunrise/Sunset SmartApp stopped working

I started using the “Sunrise/Sunset” SmartApp written by SmartThings that’s in the official templates. I used it for 3-4 weeks with no issues but about 4-5 days ago, it stopped triggering for both sunrise and sunset. Any idea what happened? I even rebooted my hub just in case, but no changes. See the code below.

Thanks,

Alan

/**

  • Copyright 2015 SmartThings
  • 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.
  • Sunrise, Sunset
  • Author: SmartThings
  • Date: 2013-04-30
    */
    definition(
    name: “Sunrise/Sunset”,
    namespace: “smartthings”,
    author: “SmartThings”,
    description: “Changes mode and controls lights based on local sunrise and sunset times.”,
    category: “Mode Magic”,
    iconUrl: “https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine.png”,
    iconX2Url: “https://s3.amazonaws.com/smartapp-icons/ModeMagic/rise-and-shine@2x.png
    )

preferences {
section (“At sunrise…”) {
input “sunriseMode”, “mode”, title: “Change mode to?”, required: false
input “sunriseOn”, “capability.switch”, title: “Turn on?”, required: false, multiple: true
input “sunriseOff”, “capability.switch”, title: “Turn off?”, required: false, multiple: true
}
section (“At sunset…”) {
input “sunsetMode”, “mode”, title: “Change mode to?”, required: false
input “sunsetOn”, “capability.switch”, title: “Turn on?”, required: false, multiple: true
input “sunsetOff”, “capability.switch”, title: “Turn off?”, required: false, multiple: true
}
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”]
}
section (“Zip code (optional, defaults to location coordinates)…”) {
input “zipCode”, “text”, required: false
}
section( “Notifications” ) {
input(“recipients”, “contact”, title: “Send notifications to”) {
input “sendPushMessage”, “enum”, title: “Send a push notification?”, options: [“Yes”, “No”], required: false
input “phoneNumber”, “phone”, title: “Send a text message?”, required: false
}
}

}

def installed() {
initialize()
}

def updated() {
unsubscribe()
//unschedule handled in astroCheck method
initialize()
}

def initialize() {
subscribe(location, “position”, locationPositionChange)
subscribe(location, “sunriseTime”, sunriseSunsetTimeHandler)
subscribe(location, “sunsetTime”, sunriseSunsetTimeHandler)

astroCheck()

}

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

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

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) {
	unschedule("sunriseHandler")

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

	state.riseTime = riseTime.time

	log.info "scheduling sunrise handler for $riseTime"
	schedule(riseTime, sunriseHandler)
}

if (state.setTime != setTime.time) {
	unschedule("sunsetHandler")

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

	state.setTime = setTime.time

	log.info "scheduling sunset handler for $setTime"
    schedule(setTime, sunsetHandler)
}

}

def sunriseHandler() {
log.info "Executing sunrise handler"
if (sunriseOn) {
sunriseOn.on()
}
if (sunriseOff) {
sunriseOff.off()
}
changeMode(sunriseMode)
}

def sunsetHandler() {
log.info "Executing sunset handler"
if (sunsetOn) {
sunsetOn.on()
}
if (sunsetOff) {
sunsetOff.off()
}
changeMode(sunsetMode)
}

def changeMode(newMode) {
if (newMode && location.mode != newMode) {
if (location.modes?.find{it.name == newMode}) {
setLocationMode(newMode)
send “${label} has changed the mode to ‘${newMode}’”
}
else {
send “${label} tried to change to undefined mode ‘${newMode}’”
}
}
}

private send(msg) {
if (location.contactBookEnabled) {
log.debug(“sending notifications to: ${recipients?.size()}”)
sendNotificationToContacts(msg, recipients)
}
else {
if (sendPushMessage != “No”) {
log.debug(“sending push message”)
sendPush(msg)
}

    if (phoneNumber) {
        log.debug("sending text message")
        sendSms(phoneNumber, msg)
    }
}

log.debug msg

}

private getLabel() {
app.label ?: “SmartThings”
}

private getSunriseOffset() {
sunriseOffsetValue ? (sunriseOffsetDir == “Before” ? “-$sunriseOffsetValue” : sunriseOffsetValue) : null
}

private getSunsetOffset() {
sunsetOffsetValue ? (sunsetOffsetDir == “Before” ? “-$sunsetOffsetValue” : sunsetOffsetValue) : null
}

Why are you using this SmartApp instead of Smart Lighting? I think you’ll find Smart Lighting to work much better for you.

1 Like

John,

I want to use some baseline code which I could then modify. Smart Lights was not in the new SmartApp templates. Where can I find the code for that?

Thanks,

Alan

Hi @TexasFlyer,

You won’t find Smart Lighting as a template unfortunately. That’s probably considered “core” ST, like Smart Home Monitor, so it’s unlikely that code will end up in a template.

Out of curiosity, have you tried Smart Lighting to see if it will do what you need? If it doesn’t, I bet CoRE will. I’ve pretty much done away with most of my own SmartApps now that Smart Lighting, SHM, and especially CoRE can do them.

Check to see if your sunrise/sunset times have been updating properly:

Log in to ide.smartthings.com, click Locations, click List Events, then click the “From Location” filter.

You should be looking at something like this:

If you see that you got a sunriseTime and a sunsetTime for today, then I’ll need to confirm if something changed on our end.

If you don’t see that, however, go back to the Location level and click “List SmartApps”.

Scroll down and find “Weather Station” and hit “Update”:

Then go back to see if you pulled tomorrows sunrise/sunset times.

Cheers!

Chris,

I checked and found sunset, sunsetTime, sunrise, and sunriseTime triggered today. See the below:

Hi,
I’m having a problem with sunrise/sunset, i’m using the Smart lighting app at its most basic but it doesnt fire on sunrise or sunset. I dont have any sunrise/sunset events in the log and when i click UPDATE against weather station it doesnt do anything.

any ideas?

I’m on HUB2 and it seems to be quite buggy, i get lots of errors in the android gui when i try and save things, maybe it needs a full reset?

UPDATE: I reset the hub (removed location) and started over again, i now have sunrise and sunset events

1 Like