Use variables in global variable names in strings


#1

1) Give a description of the problem

I tried asking this before, but got no takers. I’m trying it again. I’ve got four people with Life360 information stored. I’d like to call on the contents of global variables, but I want to place a variable in the global variable name so I can place it in a For-Next loop.

2) What is the expected behaviour?

The globals would be:

@LastAddressDerek
@LastAddressEric
@LastAddressTisha

I’d like to be able to use:

For Device = Derek, Eric, Tisha

Set variable LastAddress[inx] = @LastAddress{Device}

3) What is happening/not happening?

I cannot find the correct syntax for the variable statement.

**4) Post a Green Snapshot of the piston!

(Note: this is not the final piston. It’s one I created to try different ways to write the variable.


#2

Just thinking out loud here, in webCoRE we’re not able to set variable names programmatically afaik. It has to be explicitly defined beforehand wether it’s local or global but you could possibly set up a variable to be placeholder for the global names that you’re ‘assembling’.

I can see it in my head but I am not sure of the mechanics of it yet, or if it will even work :slight_smile: :gear::exploding_head:


#3

I was kinda thinking that as well, but then that would lead to something like @(variablename), which leads to the same problem. Even if I put them into an array to draw from, it would lead to the same problem. The globals and data already exist from another piston…I’m just trying to pull info from them in a particular way without listing every single one.

I’m pretty much given in to the realization that it’s probably not possible…it just would have helped cut down on some lines in my code. If you do come up with something, please let me know. :slight_smile:


#4

You can’t do what you are asking but perhaps you could define a variable using an expression:

string lastAddresses="{@LastAddressDerek},{@LastAddressEric},{@LastAddressTisha}"

As long as there are no commas in your addresses, then you can access each using:

for $index=0 to 2 step 1
   arrayItem($index, lastAddresses)
end for

Not sure what the contents of the address is if it is a name or an actual address.


#5

This was a project I came up with just for fun, and kept adding more and more stuff.

I have one Life360 piston that tells me when a family member has left a starred location (Home, Jack in the Box, Grandma’s House, etc.) It stores this info into globals (LastAddressEric, LastAddressEricSince, LastAddressEricDay). I have another piston that, when I ask Alexa where everybody is, she will pull the data for each person and, if they’re not home, will either tell me where they are (if it’s a starred location), or where they last were since what time. I was hoping to create a For-Next loop to pull all this information together, instead of line-by-line through a series of IFs. It works just fine as is, but just wanted to tighten up the code a little. It’s a bit of a monster.

Your idea sounds good. I’ve not done a lot of array coding like that, so I’ll have to try it out and see what I can do.

Piston One is for arrivals.

Piston Two keeps track of departures

Piston Three tells me the location of anyone not currently at home.


#6

now its possible to set variable with expression… in the set variable, select any variable (it would not change). Then in the value section, select expression and its something like: setVariable(var,value) so you could do setVariable("@lastadress"+device, derek) or adapt it to your need… I test it and works, its new…


#7

This sounds really useful.
I’ve just tried it, and it works. However, when you first type the expression, the evaluation console reports errors. If you then run the piston, & edit the setVariable time again, the evaluation console returns true


#8

Oh, never notice the console error… I ll check that but if it works without any issue other than the log its good I think! :slight_smile: I have been waiting for this new feature! :slight_smile:


#9

I really needed this feature quite a while ago, but I’ve forgotten what I was trying to do! Guess any message in the console doesn’t really make sense - its setting a variable. So when it says true, I think it’s just saying it will work. Not sure why it comes up with errors when you first create it though, it’s as if it thinks the variable you’re setting doesn’t exist.


#10

yes maybe! For my part I use it to update variable like my global variable from web browser with only one line of code… If I have multiple global to update, like I need one for updating the number of skip for my robot cleaner when I m away, another one for the skip value when I m home, another one to set if its school day (I check within gcal to see from there and the value automatically update from there… I also have other things… So for now I have if $args.skip is not null then @skip = $args.skip… and so on for each global to update! Now I m trying to have only one line like: if $arg.name is not null, set variable("@"$arg.name,$arg.value) so I just need to keep the format…

The only thing that I try to find is the way to send multiple name and loop through to set multiple in one url… like : $args.name = [val1,val2,val3] and $args.value=[val1,val2,val3] so I could iterate like for $index = 1 to 10 if $args.name[$index] is not null then setvar("@"+$args.name[$index],$args.value[$index]) but its not considered as array nor json…