Hello Actions in REST Endpoint

I’m following the the Creating a REST SmartApp Endpoint tutorial and I’d like to add modes to the endpoint. Is this even possible? And if so, how I would even start? I was looking at the switches in the example app, but I can’t figure out how to get Hello, Home Actions to list.

Create yourself a virtual switch in the IDE and Enjoy!!

/**
 *  Big Switch for Hello Home Phrases
 *
 *  Copyright 2014 Tim Slagle
 *
 *  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: "Big Switch for Hello Home Phrases",
    namespace: "tslagle13",
    author: "Tim Slagle",
    description: "Uses a virtual/or physical switch to run hello home phrases.",
    category: "Convenience",
    iconUrl: "http://icons.iconarchive.com/icons/icons8/windows-8/512/User-Interface-Switch-On-icon.png",
    iconX2Url: "http://icons.iconarchive.com/icons/icons8/windows-8/512/User-Interface-Switch-On-icon.png"
)


preferences {
	page(name: "selectPhrases")
    
    page( name:"Settings", title:"Settings", uninstall:true, install:true ) {
    section(title: "More options", hidden: hideOptionsSection(), hideable: true) {
			
			def timeLabel = timeIntervalLabel()

			href "timeIntervalInput", title: "Only during a certain time", description: timeLabel ?: "Tap to set", state: timeLabel ? "complete" : null

			input "days", "enum", title: "Only on certain days of the week", multiple: true, required: false,
				options: ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

			input "modes", "mode", title: "Only when mode is", multiple: true, required: false
		}
  }
}



def selectPhrases() {
	def configured = (settings.HHPhraseOff && settings.HHPhraseOn)
    dynamicPage(name: "selectPhrases", title: "Configure", nextPage:"Settings", uninstall: true) {		
		section("When this switch is turned on or off") {
			input "master", "capability.switch", title: "Where?"
		}
        def phrases = location.helloHome?.getPhrases()*.label
		if (phrases) {
        	phrases.sort()
		section("Run These Hello Home Phrases When...") {
			log.trace phrases
			input "HHPhraseOn", "enum", title: "The Switch Turns On", required: true, options: phrases, refreshAfterSelection:true
			input "HHPhraseOff", "enum", title: "The Switch Turns Off", required: false, options: phrases, refreshAfterSelection:true

		}
		}
    }
}    


def installed(){
subscribe(master, "switch.on", onHandler)
subscribe(master, "switch.off", offHandler)
}

def updated(){
unsubscribe()
subscribe(master, "switch.on", onHandler)
subscribe(master, "switch.off", offHandler)
}

def onHandler(evt) {
if(allOk){
log.debug evt.value
log.info("Running Light On Event")
sendNotificationEvent("Performing \"${HHPhraseOn}\" because ${master} turned on.")
location.helloHome.execute(settings.HHPhraseOn)
}
}

def offHandler(evt) {
if(allOk){
log.debug evt.value
log.info("Running Light Off Event")
sendNotificationEvent("Performing \"${HHPhraseOff}\" because ${master} turned off.")
location.helloHome.execute(settings.HHPhraseOff)
}
}

private getAllOk() {
	modeOk && daysOk && timeOk
}

private getModeOk() {
	def result = !modes || modes.contains(location.mode)
	log.trace "modeOk = $result"
	result
}

private getDaysOk() {
	def result = true
	if (days) {
		def df = new java.text.SimpleDateFormat("EEEE")
		if (location.timeZone) {
			df.setTimeZone(location.timeZone)
		}
		else {
			df.setTimeZone(TimeZone.getTimeZone("America/New_York"))
		}
		def day = df.format(new Date())
		result = days.contains(day)
	}
	log.trace "daysOk = $result"
	result
}

private getTimeOk() {
	def result = true
	if (starting && ending) {
		def currTime = now()
		def start = timeToday(starting).time
		def stop = timeToday(ending).time
		result = start < stop ? currTime >= start && currTime <= stop : currTime <= stop || currTime >= start
	}
	log.trace "timeOk = $result"
	result
}

private hhmm(time, fmt = "h:mm a")
{
	def t = timeToday(time, location.timeZone)
	def f = new java.text.SimpleDateFormat(fmt)
	f.setTimeZone(location.timeZone ?: timeZone(time))
	f.format(t)
}

private hideOptionsSection() {
	(starting || ending || days || modes) ? false : true
}

private timeIntervalLabel() {
	(starting && ending) ? hhmm(starting) + "-" + hhmm(ending, "h:mm a z") : ""
}

Okay, I’ll see if this works. Basically this is my work flow:

  • Device Type: Virtual On/Off Button
  • New Devices: Button references the new virtual on/off button
  • SmartApp: Big Switch for Hello Home Phrases that references the new button and my Hello phrase.

Now every time I turn on the button, my Hello Home phrase actuates.

Man that was a workaround, thanks @tslagle13.

1 Like

That’s one way of doing it. Although you can execute “Hello Home” actions directly from your endpoint handler (or from any code for that matter) if you don’t want to bother with installing virtual buttons.

Do you happen to have an example of an endpoint that has Hello Actions?

Sure. First you enumerate installed actions:

def actions = location.helloHome?.getPhrases()*.label

Then you execute them:

location.helloHome.execute(“Some HH Action”)

You can map actions to the endpoint ‘params’ by name or by id.

1 Like

I like the virtual button because when I turn it on it sets up a scene and when I turn it off it turns off that scene.

And with the end point there isn’t a good way to call that hello home action from within the app you always have to call it by a variable. Depends on what you want it for :). Plus I have a ton of other things that change based on virtual switches like my foscams. In some cases I also call 3 or 4 hello home phrases at once. So my virtual switches are a integral part of my automation. I havent touched a light switch in a very long time.

That’s the beauty of the platform. 101 ways to do things, you pick what works best for your situation.

1 Like

@tslagle13, I’d add a line to your Big Switch for Hello Home Phrases to add the ability to rename the SmartApp.

...
    input "modes", "mode", title: "Only when mode is", multiple: true, required: false
    label title: "Assign a name", required: false
}
...

Thanks buddy :smile:

No worries.

Blah blah