Detect recent mode change?

(Jordan Thurston) #1

Is there anyway to detect a recent mode change?

I’m working on an app that will call a HAM Bridge Command. But I want to to kick off when my front door opens, but only if the mode has changed to ‘Home’ within the past 10 minutes. This will prevent it from kicking off after I have been home for hours. Does that make sense? I currently am doing this by using a delay and a variable, but I feel like there has to be a easier more efficient way to monitor a recent mode change.

Here is what I have so far:

     *  Welcome Home!
     *  Copyright 2014 Jordan Thurston
     *  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: "Welcome Home!",
        namespace: "",
        author: "Jordan Thurston",
        description: "HAM Bridge Command 'Welcome Home'",
        category: "My Apps",
        iconUrl: "",
        iconX2Url: "")
preferences {
	section("Run when Home changes to") {
		input "setMode", "mode", title: "Mode?"
	section("And then door opens..."){
		input "contact1", "capability.contactSensor", title: "Where?"
    section("Send this command to HAM Bridge"){
		input "HAMBcommand", "text", title: "Command to send", required: true
	section("Server address and port number"){
		input "server", "text", title: "Server IP", description: "Your HAM Bridger Server IP", required: true
		input "port", "number", title: "Port", description: "Port Number", required: true

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


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


def initialize() {
	subscribe (location, modeChangeHandler)
    subscribe(contact1, "", contactOpenHandler)
    state.welcomed = false

def modeChangeHandler(evt) {
	log.trace "New mode is $evt.mode"
	if (evt.mode == setMode) {
    	state.welcomed = false
		welcomeTimeOut(delay: 600)

def contactOpenHandler(evt) {
	log.trace "$evt.value: $evt, $settings, $location.mode, $state.welcomed"
    if (location.mode == setMode && state.welcomed == false)
		log.debug "Mode is $location.mode, $contact1 was opened, sending $HAMBcommand"

def welcomeTimeOut() {
	log.debug "Timeout, welcome = true"
	state.welcomed = true

def sendHttp() {
	def ip = "${settings.server}:${settings.port}"
	def deviceNetworkId = "1234"
	sendHubCommand(new physicalgraph.device.HubAction("""GET /?${settings.HAMBcommand} HTTP/1.1\r\nHOST: $ip\r\n\r\n""", physicalgraph.device.Protocol.LAN, "${deviceNetworkId}"))
    state.welcomed = true
    log.debug "Welcome = True"
	sendNotificationEvent("Welcome Home!")


(Brian Steere) #2

I’m not aware of there being a better way to do it. There might be a possibility of looking at past events, but I haven’t done that.