Set a variable based on results of string manipulation


#1

1) Give a description of the problem
I am attempting to reduce some code in a piston for tracking when someone is awake or asleep. I have a button named after each person, and a boolean global variable which keeps track of if they are awake or not, also based on their name. Using an if statement, I can check if a button is pressed/held, and if the corresponding global variable is correctly set. The down side is I end up with 1 condition per person.

Seeing as how the name of the button that was pressed would tell me which global variable to check, I wanted to compress this to pragmatically determine the correct global variable to use based on the name of the device that was activated. I can get to the point where I write the NAME of the global variable out in the info log by parsing the device string and building the known global variable name. I even saved it in a local variable. However I can’t figure out a way to the SET a variable, if the variable that will be manipulated could change depending on the input.

2) What is the expected behavior?
When the button is “pushed”, and the global varriable which corrosponds to it is false, set the global variable to true. (in my example, this would be waking up)

3) What is happening/not happening?
It is correctly saving a local variable which has the name of the global variable tied to the button that was pressed, and using “bool” function I can check if it is false. Right now I can’t figure out what to put in the then statement to now set it to true. All “set variable” functions I see make me choose one global varaible. I want to set the global variable who’s name is now in the local variable “GlobalVariable” to True. But since the determination of which global variable is being altered is dependent on the device, I need set function that I can pass a string to rahter than picking one hard coded.

**4) Post a Green Snapshot of the piston![image|45x37]

**5) Attach any logs
No logs, since I am stuck as to how to do the setting…


#2

So, I couldn’t find a way to take a constructed string and then correctly reference a variable. I was able to do something that isn’t quite as clean as I would like though. If I crate virtual switches, and just use the On/Off State to represent awake/asleep… then I can track state across runs and use a device list and a for loop to solve my problem.

The only thing I don’t like about this, is that I have all these “virtual” devices in my smartthings. If I could do this with global variables, it would be much cleaner. At least with this, I can add a new button to the local variable, and a new virtual-switch to the local variable, and it all buttons will do the same thing with out repeating code. I think this would be possible if there was a for loop that wasn’t just devices. I just can’t seem to loop over a list of global variables and operate on each one (comparing and saving) with a $variable according to the loop


#3

This little trick helps me to stay organized…

When I create any type of Simulated device, I always add a prefix of “V” when assigning a unique “Device Network Id”. This lets me later sort by that column, and all my “virtual” devices will be all grouped together.

temp

I will still use a “friendly” name for my “Display Name”, so I can easily trigger that device by voice with Alexa.


#4

Thanks, I do similar with the names being “Virtual-” so I can tell it isn’t actually a device. I just was hoping I could program those away and not have them clogging up my list of devices.

To solve the friendly name for voice commands, I make aliases in my google home so that when I say a friendly name (or a few similar ones) it will translate that to the virtual switch of my choice. I just hate opening smartthings and seeing a bunch of non-existent things.


#5

Edits to a global variable are not written until the piston has completed all the commands. This usually means a loop writing to a global will not see the first change until after the loop has completed. I typically avoid looping with globals for this reason.

On the other hand, “Simulated Switches” are changed nearly instantly upon request.


#6

Personally, I would think that using local variables for 95% of this piston would be the path I would take. And only pushing one global, one time per piston run.

But that’s just me. There are probably lots of ways to accomplish this.

I am out of freetime at the moment though. Just wanted to give you some ideas


#7

In my one case, it wouldn’t matter if it was delayed until the piston exits. Only a single button is processed at a time. But I can’t seem to find a valid list for holding global variables, or even a for loop that doesn’t assume the things in the loop are devices. So I never got to the point of finding out if a delayed write would be an issue or not!

However, if this DOES make for a faster response times in follow up pistons that might act on this change… maybe this is the better option all together.