Change Mode Based on Illuminance And/Or Sunset/Sunrise And/Or Time of Day

This app can change mode based on illuminance or other criteria. But really illuyminance is the best way as I want our lights to come on during thunderstorms during the day, which regular sunset/sunrise can’t do.

(And don’t get me started on the stupidity of Modes… :slight_smile: )


  • Change Mode Based on Illuminance And/Or Sunset/Sunrise And/Or Time of Day
  • Copyright 2015 Anders Heie
  • Based on Smartthings “Sunrise, Sunset” application
  • Based on Arno Arnaud “Bright When Dark And/Or Bright After Sunset” application
  • 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: “Change Mode Based on Illuminance And/Or Sunset/Sunrise And/Or Time of Day”,
namespace: “pope”,
author: “Anders Heie”,
description: “Change the mode based on the illuminanceof or more illuminance detectors, or sunset/sunrise for your location, or time of day.”,
category: “Mode Magic”,
iconUrl: “”,
iconX2Url: “”,
iconX3Url: “”)

preferences {
section(“Change mode to”)
input “newMode”, “mode”, title: “Mode”, required: true
section(“Using any of these light sensors…”)
input “lightSensors”, “capability.illuminanceMeasurement”,title: “Light Sensors”, multiple: true, required: false
input “luxLevelUp”, “number”, title: “Illuminance Rising Above (0-999)”, required: false
input “luxLevelDown”, “number”, title: “Illuminance falling below (1-1000)”, required: false
section(“Force change at this time every day”) {
input “startTime”, “time”, title: “Time of Day”, required: false
section (“Sunset/Sunrise”) {
input “sunriseOn”, “bool”, title: “Change mode at Sunrise?”, required: true, default: false
input “sunsetOn”, “bool”, title: “Change mode at Sunset?”, required: true, default: 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”]
section (“Zip code (optional, defaults to location coordinates)…”) {
input “zipCode”, “text”, required: false
section (“Turn on and off these switches when mode changes”) {
input “switchesOn”, “capability.switch”, title: “Turn on?”, required: false, multiple: true
input “switchesOff”, “capability.switch”, title: “Turn off?”, required: false, multiple: true

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() {

def updated() {
//unschedule handled in astroCheck method

def initialize() {
subscribe(location, “position”, locationPositionChange)
subscribe(location, “sunriseTime”, sunriseSunsetTimeHandler)
subscribe(location, “sunsetTime”, sunriseSunsetTimeHandler)
subscribe(lightSensors, “illuminance”, illuminanceHandler, [filterEvents: false])
if (startTime != null && startTime != “”) {
log.debug “Scheduling mode change to run at $startTime”
// Schedules recurring event at this time (Ignores the Date)
schedule(startTime, “scheduledStart”)

def scheduledStart() {
log.trace "We reached startTime time-of-day, triggering mode change"

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

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 (sunriseOn) {
changeMode(newMode, “Sunrise”)

def sunsetHandler() { "Executing sunset handler"
if (sunsetOn) {
changeMode(newMode, “Sunset”)

def changeMode(newMode, message) {
log.debug "changeMode($newMode, $message)"
if (newMode && location.mode != newMode) {
if (location.modes?.find{ == newMode}) {
if(switchesOn) {
log.debug “switchesOn.on()”
if(switchesOff) {
log.debug “”
send "${label} has changed the mode to ‘${newMode}’ because of " + message
else {
send "${label} tried to change to undefined mode ‘${newMode}’ because of " + message

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”)

    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

def illuminanceHandler(evt)
log.debug "$ $evt.value, "
if (evt.integerValue > ((luxLevelUp != null && luxLevelUp != “”) ? luxLevelUp : 1000))
log.debug "Switch to $newMode because illuminance is superior to luxLevelUp lux…"
changeMode(newMode, “Illuminance of $evt.integerValue > $luxLevelUp”)
else if (evt.integerValue < ((luxLevelDown != null && luxLevelDown != “”) ? luxLevelDown : 0))
log.debug "Switch to $newMode because illuminance is less than luxLevelDown lux…"
changeMode(newMode, “Illuminance of $evt.integerValue < $luxLevelDown”)

1 Like

This app gives me the following error:

startup failed: script_app_metadata_4c7ebf55_4b36_4c07_a250_399a6b0034d7: 15: unexpected token: Mode @ line 15, column 15. name: “Change Mode Based on Illuminance And/Or Sunset/Sunrise And/Or Time of Day”, ^ 1 error

The original of this code had invalid quote marks - replaced all “ with " and all ” with " and I was able to use the code. The original quote marks are slanted ones - for U.S. usage, you will need standard non-slanted quote marks.