Brand new to ST, first thing I discovered: modes are tricky. Wrote an app

Hello! I just got a SmartThings hub for Xmas. I’ve had a Hue setup for a while and wanted to get more into home automation. Fun stuff!

The first thing I noticed about ST was that modes are kinda tricky and weird. Like most people, it seems, I expected them to trigger things. Then I found Hello Home phrases, and it started to make more sense.

The one thing I couldn’t figure out was how to automate things based on both presence and time of day. Having looked at the forums a bit now, everyone seems to struggle with the shoehorning of those two related concepts into modes.

So I set out to make a SmartApp to help ST work how I wanted and expected it to.

I probably should have poked around more, because this seems like a pretty popular endeavor in here! In particular, tslagle13’s Magic Home does just about exactly what I set out to do. I was looking to automate phrases on sunrise and sunset, but also wanted to switch from morning to day, and from night to late night.

So anyway, in the past couple of days I wrote a SmartApp (I’m a coder by background) that runs phrases automatically at sunrise/sunset, along with two (optional) other arbitrary times of day, factoring in if anyone is home or not.

I am using it with phrases called Morning, Morning Away, Daytime, Daytime Away, Night, Night Away and Late Night, Late Night Away.

After banging away on it, it seems to do the trick. Haven’t built in the ability to use a zip code other than the hub’s, or the “false alarm” interval that everyone seems to use for presence, but it’s a start! I’m just a couple weeks into ST and a couple days into Groovy, so there’s much to learn, but I’m excited to keep hacking away and trying to contribute.

Anyway, it’s called Autophrases and it’s here on Github:

Thanks,
-cm

2 Likes

Really cool man! Check this out as well! I think you and I wrote the same app lol. Welcome to the community! Cool app:).

Feel free to steal anything from my app if it helps you:)

Thanks! It’s funny, I thought and thought on how to get ST doing what I wanted, went through so many scenarios, then ended up exactly where you did, haha.

Haha yeah. It’s something that needs to be baked in for sure!

1 Like

I’m still looking for a tutorial or guide to explain what modes, scenes and phrases are… Just installed a z-wave thermostat the other day, but I’m constantly setting it manually with the app. Didn’t come across anything on the ST main site or their YouTube channel. Plan on digging into it while at work tomorrow

Have a look here, there are a few articles on what modes are, what Hello Home phrases are, and how to use them.

https://support.smartthings.com/hc/en-us/sections/200174660-Having-Your-Home-Work-For-You

1 Like

Thank you Christian!

Hey guys - I am relatively new to ST also and am also a coder so I was just about to start a project to do what you guys both did since the built in actions don’t quite cut it. I’m glad I spent some time stumbling around in the community to find this. Very cool - many thanks.

Now if I could just figure out how to wire up a web service so I can tie my ST into my computer controlled IP devices. I can’t seem to find an intact example and the documentation isn’t quite up to snuff.

Hey @kewashi, are you looking for an example of exposing endpoints via a SmartApp? Providing better quick starts / examples / tutorials in this area is on my to-do list, but perhaps we can help get you past any roadblocks now.

FWIW, this is a simple web services example that we’ve been working on to better incorporate in our docs:

/**
 *  RESTful Switch
 *
 *  Copyright 2015 james anderson
 *
 *  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.
 *
 */
definition(
    name: "RESTful Switch",
    namespace: "",
    author: "james anderson",
    description: "restful switch",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    oauth: true)


preferences {
  section("Allow External Service to Control These Things...") {
    input "light1", "capability.switch", title: "Pick switch #1", required: false
  }
}
 

// This block defines which functions will fire when you hit certain endpoints.
// the endpoint wil be the API enpoint shown in the IDE, with the "/switch" path appended to it.
// The HTTP method (GET or PUT in this case) will determine which of our methods to call
mappings {
  path("/switch") {
    action: [
      GET: "getSwitch",
      PUT: "setSwitch"
    ]
  }
}

def installed() {
  log.debug "Installed with settings: ${settings}"
}

def updated() {
  log.debug "Updated with settings: ${settings}"
}

// called when a GET request hits the /switch endpoint, as configured in the mappings above.
def getSwitch() {
    light1.currentState("switch")
}

// called when a PUT request hits the /switch endpoint, as configured in the mappings above.
// assumes JSON in the form of {"value" : "on"} or {"value" : "off"} sent in the request body
def setSwitch() {
	// a request object is injected into this method.
    // see http://grails.github.io/grails-doc/2.4.x/ref/Servlet%20API/request.html
    log.debug "The event: " + request.JSON.value
    
    if (request.JSON.value == "on") {
        light1.on()
    }
    else if (request.JSON.value == "off") {
        light1.off()
    }
    else {
        log.error "Invalid value: $request.JSON.value"
    }
}

Of course, you need to get your OATH configured as discussed in the docs here.

Welcome to the group! SmartThings can be great fun for people who enjoy coding, it’s a very flexible developer platform.

From a coder viewpoint:

Mode functions as a global mutable state variable. So you can change it from a lot of different places and then it’s available everywhere. And yes, if you’re not careful it will break your heart, because one process may have changed it before another one checks it. :wink: (And it can be very hard to debug because an error may depend on which process happened to complete first.) Mode can only have one value at a time, but you can add more values if needed.

And you can make mode one of the schedule controllers and change the mode at the end of the action, like changing mode from At Home to Away.

Hello Home Actions are limited option scenes with limited scheduling built in. You can’t set every every parameter for every device from a standard Hello Home Action, but you can set quite a few. You can set some specific triggers, like all presence sensors outside the geofence on Monday and a motion sensor goes off, and have a bunch of different actions occur on different devices at about the same time. Like the door locks, the thermostat setting goes down, and the bedroom lights go off.

From a non coder point of view:

Hello Home Actions are a way of controlling when certain action requests will be sent to the devices in your home, and grouping multiple action requests together so they all happen at about the same time.

The “when” doesn’t just have to be based on time and date–it could be defined by a specific door being closed. Or a switch being flipped. Or when the last person leaves the house on a weekday. So when the last person leaves the house on a Monday, the door might lock, the thermostat adjust to a new setting, and the bedroom lights go off.

Mode is a very powerful way of controlling how specific actions are triggered that isn’t necessarily based on date, time, or another device doing something.

“Vacation” is a typical mode. Instead of saying how things should happen differently based on a specific date, you could set up a Vacation mode and then reuse those settings anytime you were going to be gone from the house overnight.

Different people use modes differently. Some just stick with a basic 3 or 4, like At Home, Away, and People Sleeping. Others use many, like Housecleaner, Kids After School, or Guests Visiting.

You can only have one mode set at a time. This is why some people end up with many modes, like one for Dogwalker during Vacation, one for Kids After School with Guests, etc. SmartThings gives you complete control over how and why you will use modes.

I have a question about this app as well as Magic Home. I was trying to add an offset for sunrise/sunset, which appeared to be pretty simple at first. Just add sunriseOffset and sunsetOffset to getSunriseAndSunset and then add a couple preference sections to input the offset that I want. When I turn on debug the altered sunrise/set times are correct, but it completely breaks sunrise/sunset in Magic Home. I will admit that I have not tried to add that functionality to this app.

Ideas? Thanks.

Thank you very much - I’m sure this will help. I think I can figure out the OATH stuff so this will get me going.

Great app but I would love an option to “do nothing.” Meaning if I’m away at sunrise, the app will simply leave the mode set to away at sunrise. It seems like I have to have the app do something/anything or I can’t save the page… Is there a way to say “just stay in the same mode you’re in?”