4 * Author: brian@bevey.org
7 * Monitors a set of presence detectors and triggers a mode change when everyone has left.
8 * When everyone has left, sets mode to a new defined mode.
9 * When at least one person returns home, set the mode back to a new defined mode.
10 * When someone is home - or upon entering the home, their mode may change dependent on sunrise / sunset.
15 namespace: "imbrianj",
16 author: "brian@bevey.org",
17 description: "When everyone leaves, change mode. If at least one person home, switch mode based on sun position.",
18 category: "Mode Magic",
19 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
20 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
24 section("When all of these people leave home") {
25 input "people", "capability.presenceSensor", multiple: true
28 section("Change to this mode to...") {
29 input "newAwayMode", "mode", title: "Everyone is away"
30 input "newSunsetMode", "mode", title: "At least one person home and nightfall"
31 input "newSunriseMode", "mode", title: "At least one person home and sunrise"
34 section("Away threshold (defaults to 10 min)") {
35 input "awayThreshold", "decimal", title: "Number of minutes", required: false
38 section("Notifications") {
39 input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
53 subscribe(people, "presence", presence)
54 subscribe(location, "sunrise", setSunrise)
55 subscribe(location, "sunset", setSunset)
57 state.sunMode = location.mode
61 changeSunMode(newSunriseMode)
65 changeSunMode(newSunsetMode)
68 def changeSunMode(newMode) {
69 state.sunMode = newMode
71 if(everyoneIsAway() && (location.mode == newAwayMode)) {
72 log.debug("Mode is away, not evaluating")
75 else if(location.mode != newMode) {
76 def message = "${app.label} changed your mode to '${newMode}'"
78 setLocationMode(newMode)
82 log.debug("Mode is the same, not evaluating")
87 if(evt.value == "not present") {
88 log.debug("Checking if everyone is away")
90 if(everyoneIsAway()) {
91 log.info("Starting ${newAwayMode} sequence")
92 def delay = (awayThreshold != null && awayThreshold != "") ? awayThreshold * 60 : 10 * 60
93 runIn(delay, "setAway")
98 if(location.mode != state.sunMode) {
99 log.debug("Checking if anyone is home")
102 log.info("Starting ${state.sunMode} sequence")
104 changeSunMode(state.sunMode)
109 log.debug("Mode is the same, not evaluating")
115 if(everyoneIsAway()) {
116 if(location.mode != newAwayMode) {
117 def message = "${app.label} changed your mode to '${newAwayMode}' because everyone left home"
120 setLocationMode(newAwayMode)
124 log.debug("Mode is the same, not evaluating")
129 log.info("Somebody returned home before we set to '${newAwayMode}'")
133 private everyoneIsAway() {
136 if(people.findAll { it?.currentPresence == "present" }) {
140 log.debug("everyoneIsAway: ${result}")
145 private anyoneIsHome() {
148 if(people.findAll { it?.currentPresence == "present" }) {
152 log.debug("anyoneIsHome: ${result}")
158 if(sendPushMessage != "No") {
159 log.debug("Sending push message")