How to turn off hue bulbs with virtual switch?

I created a virtual switch and I can turn on my Hue scene with it, but I can’t get the bulbs to turn off. I thought I could do something like this, but it’s not working.

def offHandler(evt) {
    log.trace "offHandler($evt.name: $evt.value)"
    if (state.theSwitch == "off") {
      log.trace "switch state is ($evt.value)"
      group1.setColor([level: 0])
      group2.setColor([level: 0])
      group3.setColor([level: 0])
    }
}

Any advice?

Here is my device type:

/**
 *  Virtual Switch
 *
 *  Copyright 2014 Juan Risso
 *
 *  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: "Virtual Switch", namespace: "juano2310", author: "Juan Risso") {
        capability "Switch"
        capability "Refresh"        
    }

	// simulator metadata
	simulator {
	}

	// UI tile definitions
	tiles {
		standardTile("button", "device.switch", width: 2, height: 2, canChangeIcon: false) {
			state "off", label: 'Off', action: "switch.on", icon: "st.Kids.kid10", backgroundColor: "#ffffff", nextState: "on"
			state "on", label: 'On', action: "switch.off", icon: "st.Kids.kid10", backgroundColor: "#79b821", nextState: "off"
		}
		standardTile("refresh", "device.switch", inactiveLabel: false, decoration: "flat") {
			state "default", label:'', action:"refresh.refresh", icon:"st.secondary.refresh"
		}        
		main "button"
		details(["button", "refresh"])
	}
}

def parse(String description) {
}

def on() {
	sendEvent(name: "switch", value: "on")
}

def off() {
	sendEvent(name: "switch", value: "off")
}
1 Like

Please post all of your code for your smartapp. It’s highly likely that the issue is in the code for the smartapp rather than the code for the device type – for instance, is your smartapp code is actually changing the state.theSwitch variable to “off”?

1 Like

Here is the code I am using, but it’s not mine. I saw he had a way to turn of switches already, so I just commented out the motion options I didn’t need and it’s working as expected. I am probably going to update this to support more groups and then create a virtual button that represents a scene of “All Dark” Which can then be called to turn off all of the lights.

definition(
    name: "Scene Control",
    namespace: "AAptastic",
    author: "Anthony Pastor",
    description: "This app sets a lighting scene for 1 OR 2 groups of your HUE light switches to specific colors / levels, and/or turn 1 or 2 sets of switches on / off.",
    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")

preferences {
	section("Select Switch to monitor"){
		input "theSwitch", "capability.switch"
	}
	section("Which HUE light(s) to put into Group 1:") {
		input "group1", "capability.colorControl", multiple: true, required: false
	}
	section("Group 1's detailed light settings") {
        input ("group1Lvl", "integer",title: "Level", required: false, multiple: false)  
        input ("group1Color", "enum", multiple: false, title: "Color (Default is Soft White)", required: false, metadata:[values:["Soft White","White","Daylight","Warm White","Red","Green","Blue","Yellow","Orange","Purple","Pink"]])
    }
	section("Which HUE light(s) to put into Group 2:") {
		input "group2", "capability.colorControl", multiple: true, required: false
	}
	section("Group 2's detailed light settings") {
        input ("group2Lvl", "integer",title: "Level", required: false, multiple: false)  
        input ("group2Color", "enum", multiple: false, title: "Color (Default is Soft White)", required: false, metadata:[values:["Soft White","White","Daylight","Warm White","Red","Green","Blue","Yellow","Orange","Purple","Pink"]])

    }
    section("Turn ON these lights..."){
		input "switch1", "capability.switch", multiple: true, required: false
	}
    section("Turn OFF these lights..."){
		input "switch2", "capability.switch", multiple: true, required: false
	}
  	//section("And then off when there's been no movement for..."){
	//	input "delayMinutes", "number", title: "Minutes?"
    //    input "motion", "capability.motionSensor", title: "on this sensor...", required: false, multiple: true
	//}
}

def installed()
{

    initialize()
}

def updated() {

	unsubscribe()
    initialize()

}


def appTouch(evt) {
	log.info evt.value
    if (group1){
     	def myLightLevel = state.lightLevel1 as Integer	
	    def myhueColor = state.hueColor1 as Integer
    	def mySat = state.saturation1 as Integer
		def newValue = [hue: myhueColor, saturation: mySat, level: myLightLevel]

   		group1.each {    
		    it.setColor(newValue)       
		}
	}

    if (group2){
     	def myLightLevel = state.lightLevel2 as Integer	
	    def myhueColor = state.hueColor2 as Integer
    	def mySat = state.saturation2 as Integer
		def newValue = [hue: myhueColor, saturation: mySat, level: myLightLevel]

        group2.each {    
		    it.setColor(newValue)       
		}

    }

	if(switch1){
    	switch1.on()
    }
    if(switch2){
    	switch2.off()
    }

}

def onHandler(evt) {
	log.info evt.value
    if (group1){
     	def myLightLevel = state.lightLevel1 as Integer	
	    def myhueColor = state.hueColor1 as Integer
    	def mySat = state.saturation1 as Integer
		def newValue = [hue: myhueColor, saturation: mySat, level: myLightLevel]

   		group1.each {    
		    it.setColor(newValue)       
		}

	}

    if (group2){
     	def myLightLevel = state.lightLevel2 as Integer	
	    def myhueColor = state.hueColor2 as Integer
    	def mySat = state.saturation2 as Integer
		def newValue = [hue: myhueColor, saturation: mySat, level: myLightLevel]

        group2.each {    
		    it.setColor(newValue)       
		}
    }

	if(switch1){
    	switch1.on()
    }
    if(switch2){
    	switch2.off()
    }

}


def initialize() {
	subscribe(app, appTouch)
	subscribe(theSwitch, "switch.On", onHandler)
    subscribe(motion, "motion.inactive", offHandler)

    colorSelection1()
    colorSelection2()

}

def colorSelection2() {
	state.lightLevel2 = group2Lvl
	state.hueColor2 = 23
	state.saturation2 = 56

	switch(group2Color) {
		case "White":
			state.hueColor2 = 52
			state.saturation2 = 19
			break;
		case "Daylight":
			state.hueColor2 = 53
			state.saturation2 = 91
			break;
		case "Soft White":
			state.hueColor2 = 23
			state.saturation2 = 56
			break;
		case "Warm White":
			state.hueColor2 = 20
			state.saturation2 = 80 //83
			break;
		case "Blue":
			state.hueColor2 = 70
			state.saturation2 = 100            
			break;
		case "Green":
			state.hueColor2 = 39
            state.saturation2 = 100
			break;
		case "Yellow":
			state.hueColor2 = 25
            state.saturation2 = 100
			break;
		case "Orange":
			state.hueColor2 = 10
            state.saturation2 = 100
			break;
		case "Purple":
			state.hueColor2 = 75
            state.saturation2 = 100
			break;
		case "Pink":
			state.hueColor2 = 83
            state.saturation2 = 100
			break;
		case "Red":
			state.hueColor2 = 100
            state.saturation2 = 100
			break;

	}
}

def colorSelection1() {
	state.lightLevel1 = group1Lvl 
	state.hueColor1 = 23
	state.saturation1 = 56

	switch(group1Color) {
		case "White":
			state.hueColor1 = 52
			state.saturation1 = 19
			break;
		case "Daylight":
			state.hueColor1 = 53
			state.saturation1 = 91
			break;
		case "Soft White":
			state.hueColor1 = 23
			state.saturation1 = 56
			break;
		case "Warm White":
			state.hueColor1 = 20
			state.saturation1 = 80 //83
			break;
		case "Blue":
			state.hueColor1 = 70
			state.saturation1 = 100
            break;
		case "Green":
			state.hueColor1 = 39
			state.saturation1 = 100
            break;
		case "Yellow":
			state.hueColor1 = 25
			state.saturation1 = 100			
            break;
		case "Orange":
			state.hueColor1 = 10
			state.saturation1 = 100
            break;
		case "Purple":
			state.hueColor1 = 75
			state.saturation1 = 100
            break;
		case "Pink":
			state.hueColor1 = 83
			state.saturation1 = 100
            break;
		case "Red":
			state.hueColor1 = 100
			state.saturation1 = 100                       
			break;
	}

			state.lastStatus = "on"

}

def offHandler(evt) {
		log.trace "offHandler($evt.name: $evt.value)"	

        state.motionStopTime = now()

		if(delayMinutes) {
			runIn(delayMinutes*60, turnOffMotionAfterDelay, [overwrite: false])
		} else {
			turnOffMotionAfterDelay()
		}
}

def turnOffMotionAfterDelay() {
	log.debug "In turnOffMotionAfterDelay"
	if (state.motionStopTime && state.lastStatus != "off") {
		def elapsed = now() - state.motionStopTime
		if (elapsed >= (delayMinutes ?: 0) * 60000L) {
			group1.setColor([level: 0])
			group2.setColor([level: 0])
			switch1.setColor([level: 0])

			theSwitch.off()


		}
	}
}

Lol. That’s my code.

2 Likes

Keo -

Did you also change the subscription in initialize() to call the offHandler() routine?

From this:

to something like this:

-Tony

I did initially, but it wouldn’t turn off the virtual switch. I’ll take a look at it this weekend and give it a second look. I’m not very good at coding in groovy, but I’ll get there with help and time.