MySwitch.currentSwitch == "on" not responding / weird behavior

smartapp

(ELllv) #1

Hi everybody,

I’m stuck trying to get an app to read the on/off status of several virtual switches (using juano2310 virtual switch code) and if I can have the handler to log.debug on or off as events, I can’t get it to react to different switches.

In other words here are the switches defined in settings and their respective variables to read their current status :

def CurrS1 = switches1.currentSwitch
def CurrS2 = switches2.currentSwitch
def CurrS3 = switches3.currentSwitch
def CurrS4 = switches4.currentSwitch
def CurrS5 = switches5.currentSwitch

Now if I log.debug them it displays the correct status.

But if I add a condition such as :

if (CurrS1 == “on”)
or
if(CurrS1 == evt.value)

although evt value logs perfectly the condition never works.

I also tried using this variable instead :

def switches1ID = switches1.find{it.id == evt.deviceId}

but no joy.

And if I type the documentation’s suggested syntax :

if (“off” != switches1.currentSwitch) it will simply always return true, whatever the actual on/off status of the switch.

So, now, I’m lost… any idea what I’m doing wrong?

Here is my entire app code :

/**
*  Routines and Switches
*
*  Copyright 2014 ELFEGE
*
*  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.
*
*/
definition(
    name: "Routines and Switches",
    namespace: "elfege",
    author: "elfege",
    description: "Runs routines based on switches status",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
)

preferences {
    page(name: "settings", title: "Select your preferences", install: true, uninstall: true) 
}

def settings() {

    dynamicPage(name: "settings", title: "Select your settings", install: true, uninstall: true) {

        section([mobileOnly:true]) {
            label title: "Assign a name", required: false
        }
        section {
            input "switches1", "capability.switch", title: "When this switch is on", required: false, multiple: false               
        }
        section("run this routine")
        // get the available Routines
        def Routine = location.helloHome?.getPhrases()*.label
        if(Routine) {
            // sort them alphabetically
            Routine.sort()
            section("Pick a routine") {
                log.trace Routine
                // use the actions as the options for an enum input
                input "RunRoutine1", "enum", title: "Select a routine to execute", options: Routine, required: true 
            }
        }
        section {
            input "switches2", "capability.switch", title: "When this switch is on", required: false, multiple: false            
        }
        section("run this routine")
        if(Routine) {
            Routine.sort()
            section("run this routine ") {
                log.trace Routine
                input "RunRoutine2", "enum", title: "Select a routine to execute", options: Routine, required: false 
            }
        }
        section {
            input "switches3", "capability.switch", title: "When this switch is on", required: false, multiple: false                 
        }
        section("run this routine")
        if(Routine) {
            Routine.sort()
            section("run this routine ") {
                log.trace Routine
                input "RunRoutine3", "enum", title: "Select a routine to execute", options: Routine, required: false 
            }
        }
        section {
            input "switches4", "capability.switch", title: "When this switch is on", required: false, multiple: false                
        }
        section("run this routine")
        if(Routine) {
            Routine.sort()
            section("run this routine ") {
                log.trace Routine
                input "RunRoutine4", "enum", title: "Select a routine to execute", options: Routine, required: false 
            }
        }
        section {
            input "switches5", "capability.switch", title: "When this switch is on", required: false, multiple: false                 
        }
        section("run this routine")
        if(Routine) {
            Routine.sort()
            section("run this routine ") {
                log.trace Routine
                input "RunRoutine5", "enum", title: "Select a routine to execute", options: Routine, required: false 
            }
        }     

        section("_____________________") {
            mode(name: "modeMultiple", 
                 title: "under which modes do you want this app to run exclusively?",
                 required: false,
                 image: "http://elfege.com/penrose.jpg")
        }
    }
}

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

    initialize()
}

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

    unsubscribe()
    initialize()

}

def initialize() {

    subscribe(switches1, "switch.on", switchHandler)
    subscribe(switches2, "switch.on", switchHandler)
    subscribe(switches3, "switch.on", switchHandler)
    subscribe(switches4, "switch.on", switchHandler)
    subscribe(switches5, "switch.on", switchHandler)

    subscribe(switches5, "routineExecuted", routineChangedHandler)

}

def routineChangedHandler(evt) {
    log.debug "routineChanged: $evt"
    // name will be "routineExecuted"
    log.debug "evt name: ${evt.name}"
    // value will be the ID of the SmartApp that created this event
    log.debug "evt value: ${evt.value}"
    // displayName will be the name of the routine
    // e.g., "I'm Back!" or "Goodbye!"
    log.debug "evt displayName: ${evt.displayName}"
    // descriptionText will be the name of the routine, followed by the action
    // e.g., "I'm Back! was executed" or "Goodbye! was executed"
    log.debug "evt descriptionText: ${evt.descriptionText}"
}


def switchHandler(evt) { 
    log.debug "evt.device = $evt.device"
    log.debug "evt.value = $evt.value"

    state.evtvalue = evt.value

    def CurrS1 = switches1.currentSwitch
    def CurrS2 = switches2.currentSwitch
    def CurrS3 = switches3.currentSwitch
    def CurrS4 = switches4.currentSwitch
    def CurrS5 = switches5.currentSwitch

    log.debug "switches1 is $CurrS1"
    log.debug "switches2 is $CurrS2"
    log.debug "switches3 is $CurrS3"
    log.debug "switches4 is $CurrS4"
    log.debug "switches5 is $CurrS5"

    log.debug "running Main Loop"
    log.debug "CurrS1 value is $CurrS1.value"

    def switches1ID = switches1.find{it.id == evt.deviceId}
    def switches2ID = switches2.find{it.id == evt.deviceId}
    def switches3ID = switches3.find{it.id == evt.deviceId}
    def switches4ID = switches4.find{it.id == evt.deviceId}
    def switches5ID = switches5.find{it.id == evt.deviceId}

    log.debug "$evt.displayName turned On!"
    log.debug "The device id for this event: ${evt.device}"
  

    if (CurrS1 == "on") {
        location.helloHome?.execute(settings.RunRoutine1)
        log.debug "switch1 is on"
        log.debug "Now running $RunRoutine1 routine"
    }
    else if (CurrS2 == "on")  {
        if (switches2) {
            location.helloHome?.execute(settings.RunRoutine2)
            log.debug "switch2 is on"
            log.debug "Now running $RunRoutine2 routine"
        }
    }
    else if (CurrS3 == "on") {
        if  (switches3) {
            location.helloHome?.execute(settings.RunRoutine3)
            log.debug "switch3 is on"
            log.debug "Now running $RunRoutine3 routine"
        }
    }
    else if (CurrS4 == "on") {
        if (switches4)  {
            location.helloHome?.execute(settings.RunRoutine4)
            log.debug "switch4 is on"
            log.debug "Now running $RunRoutine4 routine"
        }
    }
    else if (CurrS5 == "on") {
        if  (switches5) {
            location.helloHome?.execute(settings.RunRoutine4)
            log.debug "switch5 is on"
            log.debug "Now running $RunRoutine5 routine"
        }
    }
    else { 
        log.debug "no condition was met"
    }
}


private correctTime() {
    def t0 = now()
    def modeStartTime = new Date(state.modeStartTime)
    def startTime = timeTodayAfter(modeStartTime, timeOfDay, location.timeZone)
    def startTimeEarly = timeTodayAfter(modeStartTime, timeOfDayEarly, location.timeZone)
    if (t0 >= startTime.time || t0 <= startTimeEarly.time) {
        true
        log.debug "The current time of day (${new Date(t0)}), is in the correct time window ($startTime):  running routine "
    } else {
        log.debug "The current time of day (${new Date(t0)}), is not in the correct time window ($startTime):  doing nothing"
        false
    }
}

private send(msg) {
    if (location.contactBookEnabled) {
        sendNotificationToContacts(msg, recipients)
    }
    else {
        if (sendPushMessage != "No") {
            log.debug("sending push message")
            sendPush(msg)
        }

        if (phone) {
            log.debug("sending text message")
            sendSms(phone, msg)
        }
    }

    log.debug msg
}

(Kevin) #2

I recommend switching to the Simulated Switch device handler to see if that fixes the problem.


(ELllv) #3

I eventually found the cause of my issue. It was shamely due to the fact that I was interested only in “on” status so I had not subscribed to off, hence the system getting nuts. I was sure I had tested the possibility to subscribe only to “on” though, that’s why I didn’t try again before a long time of frustration passed! :smiley: