Need help with my first lighting app mod


#1

I’ll start out with this, I’m not a good programmer, by any means, I will probably never fully get this down.

I’ve been using the Built-in Smart Lighting app like crazy to set colors and scenes for my Lightify bulbs, which are now all over the house. 2 things I don’t like about the app:

  1. It’s built for Hue, which means the colors are off with Lightify. Blue is purple, green is turquoise, purple is pink, etc.
  2. It only lets you set dim levels in 10% increments. I prefer 5% or less, especially for movie nights.

I wanted to simply modify the source code, which would be EASY, but I found out that the source code is a guarded secret. Plan B, I found this sample app, which is a simplified version:
http://docs.smartthings.com/en/latest/smartapp-developers-guide/parent-child-smartapps.html#example

Bingo, a good app for me to start with. I needed to make 4 modifications, though:

  1. Change the 10-20-30…% increment enum to a simple integer value, so I could input any value I want.
  2. Add some more color choices, like Teal, Sky Blue, Indigo, etc.
  3. I don’t want to activate at a certain time, but rather when the mode changes. (this also means I deleted the line for “Off time”)
  4. I will still need to go in and tweak the color values for Lightify.

For now, the program compiles, but I get an unknown error in the app.

definition(
    name: "Simple Automation",
    namespace: "mynamespace/automations",
    author: "Your Name",
    description: "A simple app to control basic lighting automations. This is a child app.",
    category: "My Apps",

    // the parent option allows you to specify the parent app in the form <namespace>/<app name>
    parent: "mynamespace/parent:Simple Lighting",
    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 {
    page name: "mainPage", title: "Automate Lights & Switches", install: false, uninstall: true, nextPage: "namePage"
    page name: "namePage", title: "Automate Lights & Switches", install: true, uninstall: true
}

def installed() {
    log.debug "Installed with settings: ${settings}"
    subscribe(location, "mode", modeChangeHandler)
    initialize()
}

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

def initialize() {
    // if the user did not override the label, set the label to the default
    if (!overrideLabel) {
        app.updateLabel(defaultLabel())
    }
}

def allModes = location.modes // ex: [Home, Away, Night]
	log.debug "all modes for this location: $allModes"


// main page to select lights, the action, and trigger mode
def mainPage() {
    dynamicPage(name: "mainPage") {
        section {
            lightInputs()
            actionInputs()
        }
        modeInputs()
    }
}

// page for allowing the user to give the automation a custom name
def namePage() {
    if (!overrideLabel) {
        // if the user selects to not change the label, give a default label
        def l = defaultLabel()
        log.debug "will set default label of $l"
        app.updateLabel(l)
    }
    dynamicPage(name: "namePage") {
        if (overrideLabel) {
            section("Automation name") {
                label title: "Enter custom name", defaultValue: app.label, required: false
            }
        } else {
            section("Automation name") {
                paragraph app.label
            }
        }
        section {
            input "overrideLabel", "bool", title: "Edit automation name", defaultValue: "false", required: "false", submitOnChange: true
        }
    }
}

// inputs to select the lights
def lightInputs() {
    input "lights", "capability.switch", title: "Which lights do you want to control?", multiple: true, submitOnChange: true
}

// inputs to control what to do with the lights (turn on,, turn off, turn on and set color, turn on and set level)
def actionInputs() {
    if (lights) {
        input "action", "enum", title: "What do you want to do?", options: actionOptions(), required: true, submitOnChange: true
        
        if (action == "level" || action == "color") {
            input "level", "number", title: "Dimmer Level", defaultValue: "50"
        }
        
        if (action == "color") {
            input "color", "enum", title: "Color", required: true, multiple:false, options: [
                ["Soft White":"Soft White"], ["Cool White":"Cool White"], ["Daylight":"Daylight"], ["Warm White":"Warm White"],
                "Red","Green","Blue","Aqua","Sky","Indigo","Lime","Mint","Yellow","Orange","Purple","Pink"]
        }
        
    }
}

// utility method to get a map of available actions for the selected switches
def actionMap() {
    def map = [on: "Turn On", off: "Turn Off"]
    if (lights.find{it.hasCommand('setLevel')} != null) {
        map.level = "Turn On & Set Level"
    }
    if (lights.find{it.hasCommand('setColor')} != null) {
        map.color = "Turn On & Set Color/Level"
    }
    map
}

// utility method to collect the action map entries into maps for the input
def actionOptions() {
    actionMap().collect{[(it.key): it.value]}
}

// inputs for selecting modes
def modeInputs() {
    if (settings.action) {
        section {
            input "modes", "mode", title: "select a mode(s)", multiple: true, required: true
        }
    }
}

// a method that will set the default label of the automation.
// It uses the lights selected and action to create the automation label
def defaultLabel() {
    def lightsLabel = settings.lights.size() == 1 ? lights[0].displayName : lights[0].displayName + ", etc..."

    if (action == "color") {
        "Turn on and set color of $lightsLabel"
    } else if (action == "level") {
        "Turn on and set level of $lightsLabel"
    } else {
        "Turn $action $lightsLabel"
    }
}

// the handler method that turns the lights on/off and sets level and color if specified
def actionHandler() {
    // perform the selected action
    switch(action) {
        case "level":
            lights.each {
                // check to ensure the switch does have the setLevel command
                if (it.hasCommand('setLevel')) {
                    it.setLevel(level as Integer)
                }
                it.on()
            }
            break
        case "on":
            lights.on()
            break
        case "color":
            setColor()
            break
        case "off":
            lights.off()
            break
        }
}

// set the color and level as specified, if the user selected to set color.
def setColor() {

    def hueColor = 0
    def saturation = 100

    switch(color) {
        case "Cool 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
            break;
        case "Red":
            hueColor = 100
            break;
        case "Green":
            hueColor = 39
            break;
        case "Blue":
            hueColor = 70
            break;
        case "Aqua":
            hueColor = 39
            break;
        case "Sky":
            hueColor = 39
            break;
        case "Indigo":
            hueColor = 39
            break;
        case "Lime":
            hueColor = 39
            break;
        case "Mint":
            hueColor = 39
            break;         
        case "Yellow":
            hueColor = 25
            break;
        case "Orange":
            hueColor = 10
            break;
        case "Purple":
            hueColor = 75
            break;
        case "Pink":
            hueColor = 83
            break;
    }

    def value = [switch: "on", hue: hueColor, saturation: saturation, level: level as Integer ?: 100]
    log.debug "color = $value"

    lights.each {
        if (it.hasCommand('setColor')) {
            it.setColor(value)
        } else if (it.hasCommand('setLevel')) {
            it.setLevel(level as Integer ?: 100)
        } else {
            it.on()
        }
    }
}

(Robin) #2

Or you could just use webCoRE lol :joy:


(Robin) #3

Set to 1% increments

Colours can be chosen via presets or RGB