# Code help: Problem with SmartApp & Sunset

**URL:** <https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089>\
**Category:** General Discussion\
**Tags:** archive\_developers\
**Created:** [January 8, 2014, 7:40am UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089 "2014-01-08T07:40:34Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobycth3](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tobycth3/32/112767_2.png) [@tobycth3](https://community.smartthings.com/u/tobycth3)\
**Post date:** [January 8, 2014, 7:40am UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/1 "2014-01-08T07:40:34Z")

</div>

Would anyone mind taking a look at my SmartApp I wrote? I’m having trouble with my if statement that looks at the current time and verifies its equal to or after sunset and before sunrise in my def presencelightshome() statement. Everything else is WAD, it’s just the checking if its past sunset when someone arrives home (presence) that isn’t working.

I keep getting this error: a9549c0c-8dfd-4afc-bc5a-39d444981ccd 4:40:47 PM: error Cannot get property ‘time’ on null object

/\*\*

- Light Control
- 
- Author: [toby@cth3.com](mailto:toby@cth3.com)
- Date: 1/2/14
- 
- Monitors a set of presence detectors and turns off a set of lights when everyone has left.
- When at least one person returns home, turns on a set of lights if it’s after sunset.
- Turns on a set of lights at sunset and turns off a set of lights at a specified time based on mode.  
\*/

preferences {  
section(“Monitor presence for these people”) {  
input “people”, “capability.presenceSensor”, multiple: true  
}

section(“When someone arrives turn on these lights”) {  
input “presencelightson”, “capability.switch”, multiple: true  
}

section(“When everyone has left turn off these lights”) {  
input “presencelightsoff”, “capability.switch”, multiple: true  
}

section(“At sunset turn on these lights”) {  
input “schedulelightson”, “capability.switch”, multiple: true  
}

section(“Turn these lights off according to schedule”) {  
input “schedulelightsoff”, “capability.switch”, multiple: true  
input “schedulemodehome”, “mode”, title: "Home mode"  
input “scheduletimehome”, “time”, title: "Home mode time"  
input “schedulemodeaway”, “mode”, title: "Away mode"  
input “scheuletimeaway”, “time”, title: “Away mode time”  
}

section(“Zip code (for sunrise/sunset)”) {  
input “zip”, “decimal”, required: false  
}

section(“False alarm threshold (defaults to 10 min)”) {  
input “falseAlarmThreshold”, “decimal”, title: “Number of minutes”, required: false  
}

section(“Notifications”) {  
input “sendPushMessage”, “enum”, title: “Send a push notification?”, metadata:[values:[“Yes”,“No”]], required:false  
}  
}

def installed() {  
init()  
}

def updated() {  
unsubscribe()  
init()  
}

def init() {  
subscribe(location)  
subscribe(people, “presence”, presence)  
schedule(scheduletimehome, schedulelightshome)  
schedule(scheduletimeaway, schedulelightsaway)  
checkSun();  
}

def checkSun() {  
def zip = settings.zip as String  
def locale = getWeatherFeature(“geolookup”, zip)  
def timezone = TimeZone.getTimeZone(locale.location.tz\_long)  
def weather = getWeatherFeature(“astronomy”, zip)

def sunrise = weather.moon\_phase.sunrise.hour + “:” + weather.moon\_phase.sunrise.minute  
def sunset = weather.moon\_phase.sunset.hour + “:” + weather.moon\_phase.sunset.minute  
def current = weather.moon\_phase.current\_time.hour + “:” + weather.moon\_phase.current\_time.minute

**def sunsetTime = timeToday(sunset, timezone)  
def sunriseTime = timeToday(sunrise, timezone)**

log.info(“Sunset: ${sunset}”)  
log.info(“Sunrise: ${sunrise}”)  
log.info(“Current: ${current}”)

schedule(timeToday(sunset, timezone), schedulelightssunset)  
schedule(timeTodayAfter(new Date(), “01:00”, timezone), checkSun)  
}

def presence(evt) {  
if(evt.value == “not present”) {  
log.debug(“Checking if everyone is away”)  
if(everyoneIsAway()) {  
log.info(“Starting lights off sequence”)  
def delay = (falseAlarmThreshold != null && falseAlarmThreshold != “”) ? falseAlarmThreshold \* 60 : 10 \* 60  
runIn(delay, “presencelightsaway”)  
}  
}  
else {  
if(anyoneIsHome()) {  
log.info(“Checking if anyone is home”)  
presencelightshome()  
}  
}  
}

**def presencelightshome() {  
log.debug(“Checking if it’s dark out”)  
if (now() \>= sunsetTime.time || now() \< sunriseTime.time) {  
def message = "${app.label} turned on the lights because someone arrived home"  
log.info(message)  
send(message)  
settings.presencelightson.on()  
}  
}**

def presencelightsaway() {  
if(everyoneIsAway()) {  
def message = "${app.label} turned off the lights because everyone left home"  
log.info(message)  
send(message)  
settings.presencelightsoff.off()  
}  
}

def schedulelightssunset() {  
def message = "${app.label} turned on the lights because it is sunset"  
log.info(message)  
send(message)  
settings.schedulelightson.on()  
}

def schedulelightshome() {  
if (location.mode==schedulemodehome) {  
def message = "${app.label} turned off the lights because of schedule ${location.mode}"  
log.info(message)  
send(message)  
settings.schedulelightsoff.off()  
}  
}

def schedulelightsaway() {  
if (location.mode==schedulemodeaway) {  
def message = "${app.label} turned off the lights because of schedule ${location.mode}"  
log.info(message)  
send(message)  
settings.schedulelightsoff.off()  
}  
}

private everyoneIsAway() {  
def result = true  
if(people.findAll { it?.currentPresence == “present” }) {  
result = false  
}  
log.debug(“everyoneIsAway: ${result}”)  
return result  
}

private anyoneIsHome() {  
def result = false  
if(people.findAll { it?.currentPresence == “present” }) {  
result = true  
}  
log.debug(“anyoneIsHome: ${result}”)  
return result  
}

private send(msg) {  
if(sendPushMessage != “No”) {  
log.debug(“Sending push message”)  
sendPush(msg)  
}  
log.debug(msg)  
}

---

<div class="post-metadata">

**Author:** ![Sean](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/sean/32/260_2.png) [@Sean](https://community.smartthings.com/u/Sean)\
**Post date:** [January 8, 2014, 10:12am UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/2 "2014-01-08T10:12:41Z")

</div>

A few folks already created a SmartAPP like this…

> **[imbrianj/nobody\_home](https://github.com/imbrianj/nobody_home)**
>
> If all presence are away, change mode to "Away", and set thermostats to "Away" - and turn off all thermostats and switches - imbrianj/nobody\_home

  

> **[Sastozki/Away-Mode---Mode-change-](https://github.com/Sastozki/Away-Mode---Mode-change-)**
>
> Contribute to Sastozki/Away-Mode---Mode-change- development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![glennfranxman](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/glennfranxman/32/82817_2.png) [@glennfranxman](https://community.smartthings.com/u/glennfranxman)\
**Post date:** [January 8, 2014, 10:41am UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/3 "2014-01-08T10:41:59Z")

</div>

I think the issue is that sunriseTime and sunsetTime are scoped to checkSun.

---

<div class="post-metadata">

**Author:** ![tobycth3](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tobycth3/32/112767_2.png) [@tobycth3](https://community.smartthings.com/u/tobycth3)\
**Post date:** [January 8, 2014, 10:50am UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/4 "2014-01-08T10:50:20Z")

</div>

Any suggestions on the best way to fix it? I use the checkSun function to schedule turning on lights at sunset (and getting when sunset will be). A dirty way would be to put the same checkSun code in the presencelightshome function but that just seems messy to me.

---

<div class="post-metadata">

**Author:** ![stepchen](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@stepchen](https://community.smartthings.com/u/stepchen)\
**Post date:** [January 8, 2014, 12:25pm UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/5 "2014-01-08T12:25:41Z")

</div>

You can save sunsetTime and sunriseTime to ‘state’, i.e.

```auto
def checkSun() {
   ...
   state.sunsetTime = sunsetTime
   ...
}

def presencelightshome() {
   ...
   if (now() >= state.sunsetTime.time ... ) 
   ...
}

```

---

<div class="post-metadata">

**Author:** ![stepchen](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@stepchen](https://community.smartthings.com/u/stepchen)\
**Post date:** [January 8, 2014, 12:40pm UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/6 "2014-01-08T12:40:37Z")

</div>

Also, you may have to unschedule( checkSun ) before scheduling it again in checkSun. I think a schedule call doesn’t have any effect for already-scheduled or running functions. The log will show ‘scheduling…’ but the function won’t get run.

---

<div class="post-metadata">

**Author:** ![tobycth3](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tobycth3/32/112767_2.png) [@tobycth3](https://community.smartthings.com/u/tobycth3)\
**Post date:** [January 8, 2014, 2:22pm UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/7 "2014-01-08T14:22:04Z")

</div>

Thank you very much, I’ll give that a try!

---

<div class="post-metadata">

**Author:** ![tobycth3](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tobycth3/32/112767_2.png) [@tobycth3](https://community.smartthings.com/u/tobycth3)\
**Post date:** [January 8, 2014, 3:46pm UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/8 "2014-01-08T15:46:09Z")

</div>

Unfortunately that didn’t work:

dce9e392-b3fb-4c90-ba61-a0359d25f9f8 4:42:55 PM: error No such property: time for class: java.lang.String

---

<div class="post-metadata">

**Author:** ![stepchen](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@stepchen](https://community.smartthings.com/u/stepchen)\
**Post date:** [January 8, 2014, 9:42pm UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/9 "2014-01-08T21:42:58Z")

</div>

Perhaps all values are serialized to primitive types. Try this instead then

```auto
def checkSun() {
   ...
   state.sunsetTime = sunsetTime.getTime()
   ...
}

def presencelightshome() {
   ...
   if (now() >= state.sunsetTime ... ) 
   ...
}

```

---

<div class="post-metadata">

**Author:** ![tobycth3](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tobycth3/32/112767_2.png) [@tobycth3](https://community.smartthings.com/u/tobycth3)\
**Post date:** [January 10, 2014, 7:47am UTC](https://community.smartthings.com/t/code-help-problem-with-smartapp-sunset/1089/10 "2014-01-10T07:47:35Z")

</div>

Hi C.Chen,  
Thank you for your help. I was able to get the code to work by doing the following:

```auto
 def sunriseTime = timeToday(sunrise, timezone)
  def sunsetTime = timeToday(sunset, timezone)
  
  state.sunriseTime = sunriseTime.time
  state.sunsetTime = sunsetTime.time

  if (now() >= state.sunsetTime || now() < state.sunriseTime)

```
