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
/**
*/
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
/*
*/
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)
}
}