Return Parameter from a function to another function


(Yogesh Mhatre) #1

Hello,

I’m not much of a programmer, but can understand few things. I’m writing a SmartApp to talk with my Alarm system provided by TotalConnect from HoneyWell. Part of this app, requires to login to the service, get a token ID and then used that token ID to perform other calls like arm/disarm.

So far my logic is pretty straight forward i.e. Login with Creds, Get the Token, Send this token to Arm or Disarm function.


def login() {

def paramsLogin = [
	uri: "LOGINURL",
	body: [userName: settings.userName , password: settings.password, ApplicationID: settings.ApplicationID, ApplicationVersion: settings.ApplicationVersion]
	]
	httpPost(paramsLogin) { responseLogin ->
	def token = responseLogin.data.SessionID 
    def message = "This is during login call. SessionID: '${token}'" 
	log.debug message
    return token
   }

}

def armAway() {

    log.debug token
    
	if(token == 'null') {
    	log.debug "Need to login"
   		login()
    return
	}

	def paramsArm = [
		uri: "ARMURL",
		body: [SessionID: token, LocationID: location, DeviceID: deviceID, ArmType: 0, UserCode: '-1']
		]
	httpPost(paramsArm) { responseArm ->
    	log.debug "Successfully Armed"
    } 

}

I want to use token that is retrieved in login() in armAway(), but the value is always null. Can some one help me with this?

Many Thanks


(Yogesh Mhatre) #2

Nevermind. I figured there was something fundamentally wrong with my understanding.

I just had to call "session = login(token) " to get my token value. Can wait to complete the smart app.


(swanny) #3

The problem is the variable named token is defined in the login() function and only exists there. In programming terms, this is called the scope of the variable.

You want to do something like:

def credentials = login()

This will put the return value from login() into the new variable called credentials. You can then use credentials in the function where it is defined.

If you want to use the same value across multiple functions, you can instead make the token variable part of the global scope:

def armAway() {
    log.debug state.token
    
    if(state.token == null) {
    	log.debug "Need to login"
    	state.token = login()
            // return - don't return here, you want to continue below
    }
    // ... rest of your function always using state.token here instead of token
}

Does that help?


(swanny) #4

I see you figured it out. Getting used to the scope of variables will definitely make things easier in the future.


#5

@mhatrey have you made any progress with this app as of yet? I’m about to start working on the same thing and was wondering if you had working code that could save me from having to start from scratch.


(Michael Langkilde) #6

Is there an update on the app?


(Yogesh Mhatre) #7

Hey Michael,

Yes. I have been using this app for a good year now. My wife loves it. I haven’t shared it with the community as its not polished. But it always gets the job done.

I’d be happy to share the code with you and also elaborate on how it works. Will PM you with the code.

= Explaination on Triggers =
In General, Triggers are based on the in built modes (Away, I’m Back, Morning & Night). Of course you can configure them.

I have setup ST to put home in Morning state at 6:30 AM and in Night state at 10:45 AM automatically. Also Away & “I’m back” mode are triggered based on the presence sensor in my case, Mobile Phones of myself and wife. Even if you don’t use your phone as presence sensor, the modes can be triggered from the SmartThings widgets.

At 10:45 PM - Night mode is triggered by ST, which Triggers the ArmStay mode of TotalConnect
At 6:30 AM - Morning Mode is triggered by ST which Triggers the DisArm mode of TotalConnect
When phones are not at home - Away mode is triggered by ST, which Triggers the ArmAway mode of Total Connect. You always need your ST app on phone running in background for location services to work well Or you could use the widgets from the App to set the appropriate Away or ImBack modes.

For the App, I have created a separate profile at Total connect https://rs.alarmnet.com/totalconnect2#/ (recommended).

That all said, there are some quirks that I need to iron out before I could actually publish the app for the community. TotalConnect APIs do not make it easy for me to work on this, and also Im not a programmer by nature of my work. I will explain those quirks in the PM. Hopefully I will get some time to figure out those, some day.

== Some ScreenShots ==

How App looks, What input it accepts, how it logs and How it reflects on the Total Connect web portal. Hope this helps to you and other folks using ST and HoneyWell Total Connect


New App: Integration with HoneyWell TotalConnect Alarm & Monitoring System
(Yogesh Mhatre) #8

Im moving the app discussion into new topic - New App: Integration with HoneyWell TotalConnect Alarm & Monitoring System where I will provide regular updates on development of it