Controlling Bulb Color with Remote Buttons

smartapp_lighting
smartapp_remotes

(Jim W) #1

I was surprised at the lack of color support integrated into the ST base software/apps. I have multiple Aeon minimotes to control various functions of the house, but I couldn’t change bulb colors. After various searches I found two threads with people asking about this, but no solutions. So today I took the Hue mood app and tweaked it to be specific for button remotes. It’s working for any button, both pushed and held, plus it works for any color bulbs. I have it working with my LIFX bulbs!

Anyways, here is the code in case anyone else comes along searching for something like this. This is my first app (and first post), so please feel free to critique the code or provide suggestions, this won’t be my last app and I’m happy to learn from the experience of others!

/**
 *  Copyright 2015 SmartThings
 *
 *  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.
 *
 *  Change Bulb Color With Button Remote
 *
 *  Author: Jim W
 *  *
 *  Date: 2015-08-03
 */
 
import groovy.json.JsonSlurper
 
definition(
    name: "Change Bulb Color With Button Remote",
    namespace: "noname4444",
    author: "Jim Worley",
    description: "Choose a button and button action (press or hold) to set a number of lights to any color/brightness.",
    category: "My Apps",
    iconUrl: "http://www.iconsdb.com/icons/download/royal-blue/idea-32.png",
    iconX2Url: "http://www.iconsdb.com/icons/download/royal-blue/idea-64.png"
)

preferences {
	section("Select the button/remote...") {
		input "button1", "capability.button", title: "Button Press", required:false, multiple:false
	}
    section("Choose button and press type..."){
      input "buttonNumber","enum",title: "Button Number (defaults to 1)",required:false,
        options: ["1","2","3","4"]
      input "buttonAction","enum",title: "Button Action (defaults to 'Push')",required:false,
        options: [["pushed":"Push"],["held":"Hold"]]
    }
    section("Control these bulbs...") {
			input "hues", "capability.colorControl", title: "Which Color Bulbs?", required:true, multiple:true
		}
    section("Choose light effects...")
    {
        input "color", "enum", title: "Hue Color?", required: false, multiple:false, options: [
            ["Soft White":"Soft White - Default"],
            ["White":"White - Concentrate"],
            ["Daylight":"Daylight - Energize"],
            ["Warm White":"Warm White - Relax"],
            "Red","Green","Blue","Yellow","Orange","Purple","Pink"]
        input "lightLevel", "enum", title: "Light Level?", required: false, options: [[10:"10%"],[20:"20%"],[30:"30%"],[40:"40%"],[50:"50%"],[60:"60%"],[70:"70%"],[80:"80%"],[90:"90%"],[100:"100%"]]
    }
    section("More options", hideable: true, hidden: false) {
        input "modes", "mode", title: "Only when mode is", multiple: true, required: false
    }


}

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

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

def subscribeToEvents() {
    subscribe(app, appTouchHandler)
	subscribe(button1, "button", eventHandler)
}

def eventHandler(evt) {
	log.trace "Executing Mood Lighting"
    def btnNum = buttonNumber as Integer ?: 1
    def btnAct = buttonAction ?: "pushed"
       
    def slurper = new JsonSlurper().parseText(evt.data)
    def inButtonNumber = slurper.buttonNumber
    def inValue = evt.value

    //log.debug "Install button: ${btnNum} and Installed action: ${btnAct}"
    log.debug "Found button: $inButtonNumber and found action: $inValue"
   
	if (modeOk && inButtonNumber == btnNum && inValue == btnAct) {
		log.trace "ModeOk"
		takeAction(evt)
	}
    else {
        log.debug "Not taking action because we are not in an approved mode or wrong button press"
    }
}

def appTouchHandler(evt) {
	takeAction(evt)
}

private takeAction(evt) {
	def hueColor = 0
	def saturation = 100

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

	state.previous = [:]

	hues.each {
		state.previous[it.id] = [
			"switch": it.currentValue("switch"),
			"level" : it.currentValue("level"),
			"hue": it.currentValue("hue"),
			"saturation": it.currentValue("saturation")
		]
	}

	log.debug "current values = $state.previous"

	def newValue = [hue: hueColor, saturation: saturation, level: lightLevel as Integer ?: 100]
	log.debug "new value = $newValue"

	hues*.setColor(newValue)
}

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

(Justin) #2

AWESOME JOB! (20 characters)


(Mark Whipp) #3

That is exactly what I was looking for! It works too, thanks @noname4444 for your work and sharing. This helps make the transition from scenes and Pico world to the Minimote workable.

Glad you made this your first post and look forward to your other SmartApps in the future. BTW- My first post too!


#4

Hi, I have been using this for months and loving it but yesterday, the color of the Phillips Hue lights are wrong. Instead of the warm white I was expecting, it turns with a greenish tint.

Live logging gave me this: Not taking action because we are not in an approved mode or wrong button press

Any idea?

Thank you.


#5

The color bug is tracked here: Phantom Color Ghosts - Hue Just Went Bat Crazy


#6

Thank you!