Charmed Quark Systems, Ltd. - Support Forums and Community
Passing device name as variable - Printable Version

+- Charmed Quark Systems, Ltd. - Support Forums and Community (http://www.charmedquark.com/vb_forum)
+-- Forum: General Discussion (http://www.charmedquark.com/vb_forum/forumdisplay.php?fid=3)
+--- Forum: CQC Support (http://www.charmedquark.com/vb_forum/forumdisplay.php?fid=9)
+--- Thread: Passing device name as variable (/showthread.php?tid=10426)

Pages: 1 2


Passing device name as variable - jokermac - 11-13-2017

Think I have asked this before, but now can't remember how to accomplish this.  

I want to write a single global action that covers setting setting any single light requests using Alexa.  Rather than using duplicate code for each light, I can just setup the echo global action to then pass the device name (for example: ELK-M1.LGHT#Dim_Main_Kitchen_Eat-in), text for echo response, and the value to the single global lighting action that will then use the passed parameters to set the light passed as parm_1.

The problem is when I try to use parm_1 to load the value into a variable.

What I am trying to do:

LocalVars::SetVariable
    P1=LVar:Light_Level
    P2=$(%(LVar:CQCActParm_1))   <-----This fails - What is the proper syntax?  



If entered directly:

LocalVars::SetVariable
    P1=LVar:Light_Level
    P2=$(ELK-M1.LGHT#Dim_Main_Kitchen_Eat-in)


RE: Passing device name as variable - wuench - 11-13-2017

LocalVars::SetVariable
P1=LVar:Light_Level
P2=%(LVar:CQCActParm_1)

$ is used to dereference a field name
% is used to dereference a variable name


RE: Passing device name as variable - jokermac - 11-13-2017

Understood, but in the this case LVar:CQCActParm_1 contains the field name that needs to be dereferenced to set the value of L_Var:Light_Level.  The text you provide will only set LVar:Light_Level to "ELK-M1.LGHT#Dim_Main_Kitchen_Eat-in".  I want LVar:Light_Level set to the current value of that field.


RE: Passing device name as variable - Dean Roddey - 11-13-2017

Use ReadField or ReadField2 to read the value. In that case, you can pass in the name of the field as a parameter, and get the value back out in variable.


RE: Passing device name as variable - jokermac - 11-13-2017

Now I remember.  Thanks.

Another quick question.  When using Alexa to set something to a specific number, is there an easy way to convert the text to a numerical value?  For instance when you say "fifty", it returns fifty, not 50.  Would like to do things like set thermostat to 70, or set lighting to 40%.  Do I have to create a function to do the conversion, or is something already built-in?


RE: Passing device name as variable - Dean Roddey - 11-13-2017

I wrote a helper macro to do that a while back, and posted it here on the forum. But I'm blanking on what it was called. Maybe someone else remembers that. I do need to make it a built in command.

Oh, nevermind, it maybe shipped. Look for:

/User/CQC/Num2Num

You pass parameters like this:

"one hundred fifty two", "GVar:Result", L"GVar:ErrMsg"

So the numeric value to convert, a variable name to put the resulting number in, and an error msg variable if it fails. It returns true if it works and false if it fails so you can check the result.

If it's not shipped I can create you a package. I'll push it up on my list of smaller things I can take on for the next drop to get that done as a built in.


RE: Passing device name as variable - jokermac - 11-18-2017

Okay, so I found the Num2Num macro and installed the macro pack.  Now having some issues implementing it.  Below is a example of what I'm trying to do.  The first if is to determine if Parm_3 is a number value...  If so, then write the level to the device field.  If it is not then test for specific key words to perform set actions.  

This format does not error on save, but clearly is not working correctly.  It executes, but does not return the error message and always falls to execute the device write (falls out the else).  What am I doing wrong?



If 
MacroEng::RunMacro
    P1=MEng.User.Num2Num
    P2=%(LVar:CQCActParm_3), "GVar:Light_Level_Num", "LVar:ErrMsg" 

Devices::FieldWrite
    P1=%(LVar:CQCActParm_1)
    P2=%(GVar:Light_Level_Num)
    P3=True

-GlobalVars::SetVariable
    P1=GVar:EchoReply
    P2=%(LVar:ErrMsg)

-GlobalVars::SetVariable
    P1=GVar:EchoReply
    P2=%(LVar:Light_Level)

Else

'Do other stuff


RE: Passing device name as variable - jokermac - 11-18-2017

Okay, so I am using the below syntax and it runs and will return (say "Set Value"), but if I try to do anything with GVar:Light_Level_Num, it will fail.  The error that shows in log is "Token expansion failed during command processing"  Also, if I use anything longer than 1 word, it fails to execute the macro claiming I am passing more than 3 parameters.

If 
MacroEng::RunMacro
    P1=MEng.User.Num2Num
    P2=%(LVar:CQCActParm_3), GVar:Light_Level_Num, LVar:ErrMsg

GlobalVars::SetVariable
    P1=GVar:EchoReply
    P2=Set Value

Devices::FieldWrite
    P1=%(LVar:CQCActParm_1)
    P2=%(GVar:Light_Level_Num)
    P3=True

Else

GlobalVars::SetVariable
    P1=GVar:EchoReply
    P2=Fell out Else

End


RE: Passing device name as variable - Dean Roddey - 11-18-2017

In this part, try this:

Code:
MacroEng::RunMacro
    P1=MEng.User.Num2Num
    P2="%(LVar:CQCActParm_3)" GVar:Light_Level_Num LVar:ErrMsg

I.e. it's not a comma separated list of values, it's just space separated, and quote the first one since it will likely have spaces in it, and you need it to be treated like a single value.

If the macro fails, it will won't set the variable value, so if it doesn't already exist it won't get created.


RE: Passing device name as variable - jokermac - 11-18-2017

Okay, so I think that fixed the error, but still not returning any values.  Perhaps this is what your last line is referring to.  

The odd thing is that the behavior is opposite to expected.  If I send it a real value, "ten" for instance, it falls through to the else statement.  If I send it a non-numerical text, like "up" for instance, it executes returning 0 or making no change.

GlobalVars::CreateVariable
    P1=GVar:Light_Level_Num
    P2=Int
    P3=
    P4=0

LocalVars::CreateVariable
    P1=LVar:ErrMsg
    P2=String
    P3=
    P4=Null

If 
MacroEng::RunMacro
    P1=MEng.User.Num2Num
    P2="%(LVar:CQCActParm_3)" GVar:Light_Level_Num LVar:ErrMsg

GlobalVars::SetVariable
    P1=GVar:EchoReply
    P2=Set Value to %(GVar:Light_Level_Num)

Else

GlobalVars::SetVariable
    P1=GVar:EchoReply
    P2=Error Message %(LVar:ErrMsg)

End