Usage of getValue with name of variable is not intuitive

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
kintrupf
Posts: 257
Joined: 10 Sep 2013 08:59

Usage of getValue with name of variable is not intuitive

Post by kintrupf » 24 Jul 2014 11:52

The usage of getValue and removeVariable with the name of a variable in quotes is not intuitive at all.
I don't know how often I already fell into that particular pit... :cry:

Code: Select all

global_something = getValue(global_something, true);
This will always set global_something to true, because getValue expects the name of a variable and not the variable itself.
Even if global_something already exists it will most certainly not contain it's own name, so getValue will always return the given default value... ;)

This, however, will work, please note the single quotes around the first parameter:

Code: Select all

global_something = getValue('global_something', true);
If that behaviour cannot be changed for some strange reason getValue or removeVariable should at least throw an exception if the evaluation of the name parameter yields null.
Putting empty names into those functions can't make any sense at all, right?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Usage of getValue with name of variable is not intuitive

Post by Martin » 24 Jul 2014 18:39

The purpose of the functions is exactly to access the variables by name so that you can build dynamic variable names like getValue("var_{n}", true).
Are you using the function as shorthand to get a default value when the variable is null instead if using an if? I could add a function for that.
Checking for null makes sense, should be fixed in the next build.

User avatar
kintrupf
Posts: 257
Joined: 10 Sep 2013 08:59

Re: Usage of getValue with name of variable is not intuitive

Post by kintrupf » 24 Jul 2014 20:10

Martin wrote: Are you using the function as shorthand to get a default value when the variable is null instead if using an if? I could add a function for that.
That is exactly what I'm trying to do. If you could add such a function I would be very pleased :P
Thank you!

Impermenance
Posts: 2
Joined: 09 Jul 2015 15:56

Re: Usage of getValue with name of variable is not intuitive

Post by Impermenance » 09 Jul 2015 16:07

I agree, easy one to get stuck on. (Also am a professional programmer for a living). I too spent many hours stuck on X=GetValue(X, 1) when it should have been X=GetValue("X",1).
Agree this is non intuitive, and ideally at least a warning should be raised in the editor. (would have saved a few hours of my life :)

I note this post is a few years old. What is the current status?

Can you recommend the best way for a simple toggle? For example, I am wishing to create a simple voice activated toggle for my Philips Hue lights. Can you please reccomend the best way to maintain state for a toggle function?

I am currently attempting to use just this:
Global_LightsState=GetValue("Global_LightsState", true)
Global_LightsState= NOT Global_LightsState

Does this code seem correct to you? What is your reccommended Best Technique to have a simple Flow that effectively toggles/alternates a setting each time it is run?


(FYI the reason for this is faked Intelligence for the voice activation. I.e instead of having two voice activated flows "Lights On" and "Lights Off", I prefer to to be thoughtful and state dependent.

i.e if I say "mumble mumble lights blah mumble", and the lights are already On, clearly that means I want them off! and vice versa. i.e I will never say "Lights On" if the lights are already on.
Also, this has the benefit of effectively improving the voice recognition positive hit rate, as there is less to process (correctly).

Appreciate your advice on the best way to maintain a state/toggle.

EDIT: Ah, solved my problem: adding the quotes, and realising Java is case sensitive: changed Global_ to global_. Given that your product (which is excellent by the way! just switched from Tasker) will be used my many non or beginner programmers, and there will never be a realistic need for "Global_", how about another compiler warning message? :) Also, is this your recommended way to maintain state?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Usage of getValue with name of variable is not intuitive

Post by Martin » 10 Jul 2015 10:50

Hi,

function getValue was specifically added to get the value of a variable by name/string. Accessing a regular defined variable should be done by just using the variable.
I would expect that the getValue-function is often used by passing a variable that contains the name of the variable so it would show a warning quite often which I don't like.

I often use a snippet like this to ensure that a variable is defined in a flow:

Code: Select all

if (global_LightsState == null) {
  global_LightsState = true;
}
You could add this to a separate script-action with a name like 'Script: initialize defaults when necessary':

Obviously I've missed to add a function like getDefaultWhenNull(var, defulatValue);
I add it to the todo-list since such a function makes sense.

I would also toggle a variable like you did:
global_LightsState = !global_LightsState; //or global_LightsState = NOT global_LightsState

global variables are the best way to maintain state since global variables are also saved and reloaded when the device is restarted.
I'll try to add a warning for cases where a variables or function calls are written using the wrong case.

Regards,
Martin

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Usage of getValue with name of variable is not intuitive

Post by Martin » 10 Jul 2015 11:59

Silly me, the function to get a default value when a variable is undefined respectively contains null already exists and is called convertNull:
global_LightsState=convertNull(global_LightsState, true);

I'll move the getValue/setValue functions to the end of the list since the functions are probably not used very frequently.

Martin

natong
Posts: 80
Joined: 29 Aug 2015 05:24

Re: Usage of getValue with name of variable is not intuitive

Post by natong » 04 Sep 2015 09:13

I mistook for a long time to debug script.

Please add example to help

Example:

removeVariable("global_abc"); #string
getValue("var_{n}", true);

phil
Posts: 6
Joined: 22 Sep 2013 13:58

Re: Usage of getValue with name of variable is not intuitive

Post by phil » 24 Sep 2015 22:08

Hi !

A little suggestion... instead of a convertNull function, you could support the Elvis operator ?: (see https://en.wikipedia.org/wiki/Elvis_operator) ?

Phil

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Usage of getValue with name of variable is not intuitive

Post by Martin » 27 Sep 2015 17:55

Hi,

Thanks for the suggestion, I add it to the list of features under consideration.

Regards,
Martin

Post Reply