Mode Switch device/app

George, are you still out there? I tried using your code. I can see the mode change status if I change it via a piston or IDE but it’s not working the other way. When I click a mode nothing happens. No log, no error no mode change. I was wondering if you could help me out?

Helper

/**

  • mode_switch_helper v1 for mode_switch virtual device
  • Copyright 2016 gkl_sf
  • 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: “Mode Switch Helper”,
namespace: “gkl-sf”,
author: “gkl_sf”,
description: “Mode switch helper”,
category: “Mode Magic”,
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”)

preferences {
section {
input “modeSwitcher”, “capability.actuator”
}
}

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

initialize()

}

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

unsubscribe()
initialize()

}

def initialize() {
subscribe(modeSwitcher, “modeChange”, deviceModeChanged)
subscribe(location, “mode”, locationModeChanged)
}

def deviceModeChanged(evt) { //device sent new mode to helper
log.debug “current system mode is {location.mode}, device requests mode change to {evt.value}”
if (location.mode != evt.value) {
setLocationMode(evt.value)
}
}

def locationModeChanged(evt) { //send mode change to device
def currentDeviceMode = modeSwitcher.currentValue(“modeCurrent”)
log.debug “current device mode is {currentDeviceMode}, system mode changed to {evt.value}”
if (currentDeviceMode != evt.value) {
modeSwitcher.locationModeChanged(evt.value)
}
}

Device Handler

/*

  • mode_switch virtual device v1, requires mode_switch_helper smartapp
  • Copyright 2016 gkl_sf
  • 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.

*/

metadata {
definition (name: “Mode Switch”, namespace: “gkl-sf”, author: “gkl_sf”) {
capability “Actuator” //capability without built-in commands

    //these are needed for tile actions
    command "Morning"
    command "LateNight"
    command "Sleep"       
    
    //this is for the helper app to inform the device that system mode was changed
    command "locationModeChanged", ["string"]
    
    attribute "modeCurrent", "string"
    attribute "modeChange", "string"
    
	fingerprint inClusters: "0x91"
    
}

tiles(scale: 2) {

    valueTile("modeCurrent", "device.modeCurrent", decoration: "flat", width: 6, height: 1) {
		state "default", label:'${currentValue}', backgroundColor:"#999999"
    }

    standardTile("Morning", "device.Morning", decoration: "flat", width: 2, height: 2) {
		state "inactive", label:'Morning', action:"modeMorning", icon:"st.Home.home2", backgroundColor:"#ffffff"
		state "active", label:'Morning', action:"modeMorning", icon:"st.Home.home2", backgroundColor:"#dcdcdc"
    } 
    
    standardTile("LateNight", "device.LateNight", decoration: "flat", width: 2, height: 2) {
		state "inactive", label:'Late Night', action:"modeLateNight", icon:"st.nest.nest-away", backgroundColor:"#ffffff"
		state "active", label:'Late Night', action:"modeLateNight", icon:"st.nest.nest-away", backgroundColor:"#dcdcdc"
    }

    standardTile("Sleep", "device.Sleep", decoration: "flat", width: 2, height: 2) {
		state "inactive", label:'Sleep', action:"modeSleep", icon:"st.Weather.weather4", backgroundColor:"#ffffff"
		state "active", label:'Sleep', action:"modeSleep", icon:"st.Weather.weather4", backgroundColor:"#dcdcdc"
    }
    
	main(["modeCurrent"]) //modeCurrent is primary so that list of Things in mobile app shows the current mode
	details(["modeCurrent", "Morning", "LateNight", "Sleep"])
}     

}

//received new mode from helper
def locationModeChanged(newMode) {
log.debug “received system mode change to ${newMode}”
def events =
events << sendEvent(name: “modeCurrent”, value: newMode, displayed: false)
events << sendEvent(name: newMode, value: “active”, displayed: false)
if (newMode != “Morning”) events << sendEvent(name: “Morning”, value: “inactive”, displayed: false)
if (newMode != “LateNight”) events << sendEvent(name: “LateNight”, value: “inactive”, displayed: false)
if (newMode != “Sleep”) events << sendEvent(name: “Sleep”, value: “inactive”, displayed: false)
events
}

//send events for new mode to helper

def modeMorning() {
sendChangedMode(“Morning”)
}

def modeLateNight() {
sendChangedMode(“LateNight”)
}

def modeSleep() {
sendChangedMode(“Sleep”)
}

private sendChangedMode(newMode) {
log.debug “send new mode to helper, current system mode {location.mode}, new mode {newMode}”
if (newMode != location.mode) {
sendEvent(name: “modeChange”, value: newMode, descriptionText: “Change Mode to ${newMode}”, isStateChange: true)
}
}