Help with custom smartapp?


(Stickstickly) #1

I was wondering if there are any nice programmer types that would be willing to help me modify a smartapp to fit my needs. I found one that almost does what i’m looking for with one exception. I found the “Switch Controls Outlet” smartapp when browsing the custom smartapps on the IDE. Currently, the code ties an outlet to a switch and operates both what your switch controls and what is on your outlet when the switch is turned on and off, and only the outlet if the switch is already off and you hit off again, or if the switch is already on and you hit on again.

What I am looking for is if the switch is off and you hit on, or if the switch is on and you hit off, then it only controls the light tied to that switch like normal, rather than it operating both the switch and the outlet under normal use. I would like to keep the functionality where if you hit off when the switch is off, or hit on when the switch is on, it will only toggle the outlet and not the switch.

The theory being i can use 1 switch to control both the light that is connected to it, as well as light connected to my outlet, and do this independently without operating both at the same time. I am not a programmer, so any help with modifying this app to suit my needs would be appreciated. Here is the code that brian was nice enough to share:

/**
 *  Switch Controls Outlet
 *
 *  Author: brian@bevey.org
 *  Date: 2013-11-16
 *
 *  A Z-Wave switch controls any given outlets.  Allows independent control of
 *  outlets by turning the switch off when already in an off state - or turning
 *  the switch on when already in an on state.
 *
 *  If switch is off: Turn switch on and it will turn on switch and any
 *  outlets.
 *  If switch is on: Turn switch off and it will turn off switch and any
 *  outlets.
 *  If switch is off: Turn switch off and it will turn on any outlets and
 *  switch will remain off.
 *  If switch is on: Turn switch on and it will turn off the switch and any
 *  outlets will remain on.
 */
preferences {
  section("Turn on with which switch?") {
    input "wallSwitch", "capability.switch"
  }

  section("Turns on which outlets?") {
    input "outlets", "capability.switch", multiple: true
  }

  section("Use switch for additional toggling (on if already on, off if already off)?") {
    input "toggle", "enum", metadata: [values: ["Yes", "No"]], required: false
  }
}

def installed() {
  init()
}

def updated() {
  unsubscribe()
  init()
}

def init() {
  state.wallSwitch = wallSwitch.latestValue("switch")
  subscribe(wallSwitch, "switch", changeLights, [filterEvents: false])
}

def changeLights(evt) {
  if(evt.isPhysical()) {
    if((wallSwitch.latestValue("switch") == "on") &&
       (evt.value == "on") &&
       (toggle != "No") &&
       (state.wallSwitch == "on")) {
      log.info("Switch is on, but we want to toggle outlets")

      toggleOutlets()
    }

    else if((wallSwitch.latestValue("switch") == "off") &&
            (evt.value == "off") &&
            (toggle != "No") &&
            (state.wallSwitch == "off")) {
      log.info("Switch is off, but we want to toggle outlets")

      toggleOutlets()
    }
      
    else if(evt.value == "on") {
      log.info("Turning on lights")
      outlets?.on()
    }

    else if(evt.value == "off") {
      log.info("Turning off lights")
      outlets?.off()
    }
  }

  state.wallSwitch = wallSwitch.latestValue("switch")
}

def toggleOutlets() {
  if(outlets.findAll { it?.latestValue("switch") == "on" }) {
    log.info("Toggle lights off")

    outlets?.off()
  }

  else {
    log.info("Toggle lights on")

    outlets?.on()
  }
}

(Stickstickly) #2

Nevermind, figured it out, just deleted the last two else if statements and it started working like i wanted.

*  A Z-Wave switch controls any given outlets.  Allows independent control of
 *  outlets by turning the switch off when already in an off state - or turning
 *  the switch on when already in an on state.
 *
 *  If switch is off: Turn switch on and it will turn on switch only.
 *  If switch is on: Turn switch off and it will turn off switch only.
 *  If switch is off: Turn switch off and it will toggle any outlets and
 *  switch will remain off.
 *  If switch is on: Turn switch on and it will toggle any outlets and the 
 *  switch will remain on.
 */
preferences {
  section("Turn on with which switch?") {
    input "wallSwitch", "capability.switch"
  }

  section("Turns on which outlets?") {
    input "outlets", "capability.switch", multiple: true
  }

  section("Use switch for additional toggling (on if already on, off if already off)?") {
    input "toggle", "enum", metadata: [values: ["Yes", "No"]], required: false
  }
}

def installed() {
  init()
}

def updated() {
  unsubscribe()
  init()
}

def init() {
  state.wallSwitch = wallSwitch.latestValue("switch")
  subscribe(wallSwitch, "switch", changeLights, [filterEvents: false])
}

def changeLights(evt) {
  if(evt.isPhysical()) {
    if((wallSwitch.latestValue("switch") == "on") &&
       (evt.value == "on") &&
       (toggle != "No") &&
       (state.wallSwitch == "on")) {
      log.info("Switch is on, but we want to toggle outlets")

      toggleOutlets()
    }

    else if((wallSwitch.latestValue("switch") == "off") &&
            (evt.value == "off") &&
            (toggle != "No") &&
            (state.wallSwitch == "off")) {
      log.info("Switch is off, but we want to toggle outlets")

      toggleOutlets()
    }
      
   
  }

  state.wallSwitch = wallSwitch.latestValue("switch")
}

def toggleOutlets() {
  if(outlets.findAll { it?.latestValue("switch") == "on" }) {
    log.info("Toggle lights off")

    outlets?.off()
  }

  else {
    log.info("Toggle lights on")

    outlets?.on()
  }
}

(Brandon De Young) #3

Hi, I have a custom SmartApp that I need some help with, too. Nelemansc has been helping me with it, but we’re sort of hitting a road block and I hate to keep bugging him about it. Can anyone else help us figure out why this is not working right? The app is supposed to execute when the specified virtual switch is turned on, OR if the SmartApp play button is pressed. Neither of these work for me when I try them. Any help would be greatly appreciated!

definition(
    name: "Virtual Switch Control Lights",
    namespace: "Virtual Switch Control Lights",
    author: "cbd@deyoungproperties.com",
    description: "Set a group of your light switches to specific levels, or turn them off, with the press of a single 'virtual switch'.",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
)

preferences {
	section("Select Switch to monitor"){
		input "theSwitch", "capability.switch"
	}
	section("Which light(s) to dim to Level 1:") {
		input "switches1", "capability.switchLevel", multiple: true, required: false
	}
    section("Level 1"){
    	input "lvl1", "number", required: false
    }
	section("Which light(s) to dim to Level 2:") {
		input "switches2", "capability.switchLevel", multiple: true, required: false
	}
    section("Level 2"){
    	input "lvl2", "number", required: false
    }
    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
	}
}

def installed()
{
	subscribe(app)
        initialize()
}

def updated()
{
	unsubscribe()
	subscribe(app)
        initialize()
}


def appTouch(evt) {
	log.info evt.value
    log.info "lvl1: $lvl1.value"
    log.info "lvl2: $lvl2.value"
    switches1.setLevel(lvl1.value)
	switches2.setLevel(lvl2.value)
    switch1.on()
    switch2.off()

}

def onHandler(evt) {

//
	log.info evt.value
    log.info "lvl1: $lvl1.value"
    log.info "lvl2: $lvl2.value"
    switches1.setLevel(lvl1.value)
    switches2.setLevel(lvl2.value)
    switch1.on()
    switch2.off()
    theSwitch.off()
}

def offHandler(evt) {
	log.debug "Received off from ${theSwitch}"
}

def initialize() {
	subscribe(theSwitch, "switch.On", onHandler)
	subscribe(theSwitch, "switch.Off", onHandler)
}

(Brandon De Young) #4

Update: here’s the same code with a small update. Looks like it works now, but ONLY if lights/values for level 1 and level 2 are selected. In other words, it does NOT work if you left levels 1 and 2 alone and only wanted some lights to be turned off. Any ideas of how to make it so you don’t need to input lights/values into levels 1 and 2 for this to work?

definition(
    name: "Virtual Switch Control Lights",
    namespace: "Virtual Switch Control Lights",
    author: "cbd@deyoungproperties.com",
    description: "Set a group of your light switches to specific levels, or turn them off, with the press of a single 'virtual switch'.",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
)

preferences {
	section("Select Switch to monitor"){
		input "theSwitch", "capability.switch"
	}
	section("Which light(s) to dim to Level 1:") {
		input "switches1", "capability.switchLevel", multiple: true, required: false
	}
    section("Level 1"){
    	input "lvl1", "number", required: false
    }
	section("Which light(s) to dim to Level 2:") {
		input "switches2", "capability.switchLevel", multiple: true, required: false
	}
    section("Level 2"){
    	input "lvl2", "number", required: false
    }
    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
	}
}

def installed()
{
	subscribe(app, appTouch)
        initialize()
}

def updated()
{
	unsubscribe()
	subscribe(app, appTouch)
        initialize()
}


def appTouch(evt) {
	log.info evt.value
    log.info "lvl1: $lvl1.value"
    log.info "lvl2: $lvl2.value"
    switches1.setLevel(lvl1.value)
	switches2.setLevel(lvl2.value)
    switch1.on()
    switch2.off()

}

def onHandler(evt) {

	log.info evt.value
    log.info "lvl1: $lvl1.value"
    log.info "lvl2: $lvl2.value"
    switches1.setLevel(lvl1.value)
    switches2.setLevel(lvl2.value)
    switch1.on()
    switch2.off()
    theSwitch.off()
}

def offHandler(evt) {
	log.debug "Received off from ${theSwitch}"
}

def initialize() {
	subscribe(theSwitch, "switch.On", onHandler)
	subscribe(theSwitch, "switch.Off", onHandler)
}

(Brandon De Young) #5

Anyone listening?? This would be a GREAT SmartApp for people to use with Tasker if the problem I explained in my previous post was fixed. Thanks in advance for any help you may be able to provide!