Variable contents and their values (Variable Variables)


#1

in a WeBCORE piston, I’ve ‘concatenated’ a variable named

VarTemp

that consists of

{$locationMode} {SomeUserVariable}

so… VarTemp=‘DayCool’

In Webcore, how do I retrieve the value of the variable named ‘DayCool’ - In another language, I might use &VarTemp

So simple, I know, but I cannot find answer in the Wiki. tHank you.


#2

If you concat the values separated with a comma, Day,Cool, you can extract the values using arrayItem().

(expression) arrayItem(0,VarTemp) »»» (string) Day
(expression) arrayItem(1,VarTemp) »»» (string) Cool


#3

Sorry - Bad question - I would like to return the content of variable DayCool which is concatenated from {locationMode} and {thermostatMode}.
I define DayCool early on. Then I put together the variable name (DayCool) and want to retrieve the defined value. Thanks

But I had no idea you could use arrays in webcore. I’m in heaven! I just Wiki’d Webcore and array and didn’t get any hits. Can you share a link to array related WebCore info? Thx


#4

I think this might be known as a variable variable…

You know PHP?

<?php

$question=’$favoriteSport’;

$favoriteSport=‘Tennis’ ;

echo $question ;
//returns $favoriteSport

echo $$question;
//returns Tennis

?>

Does this help?
Thanks again

gg


#5

Here is a short sample piston that sets {varTemp} and then reports to the log on the next line.

temp


I use “Expressions” mostly, so the variable name needs to be outside of quotes like this:

temp


Of course, this same logic can be applied to using variables in SMS, Notifications, webcalls etc…


#6

I’m looking to programmatically concatenate a variable name, then retrieve the value of the concatenated variable. Known as a ‘variable variable’ which webcore doesn’t support, though some have suggested lists as a work around. Thank you


#7

If you want to “retrieve the value … DayCool”, then my last post does the trick.
(dump Day and Cool into a new variable, and call that variable)

If you want to convert “DayCool” into “Day” and “Cool” there are other methods you can use.


Upon re-reading your posts above, it sounds like you may be trying to dynamically create a variable whose name changes. I believe the variable's name is hard coded, and only the values change.

Set variable {question1} = "Favorite Sport"
Set variable {answer1} = "Tennis"
Log to console = "My "question1" is "answer1

This returns:
My Favorite Sport is Tennis


#8

Copying in from your Reddit thread, for the benefit of anyone who finds this later:

If you’re using global variables this may not be possible but if all within the same piston you may be able to use lists. For example if you make a list variable called values then you can use Set Variable to map strings to the actual values. You can set a key ThermostatAdminDayOffsetCool of values with the cooling setpoint temperature. Then when your piston logic sets VarTemp = 'ThermostatAdminDayOffsetCool' now you can access that value in the list with values[VarTemp] which should give you the value of values.ThermostatAdminDayOffsetCool .

It might be a lot of reworking but should do what you need; lists are the closest you’re going to get with webCoRE. It’s not as elegant but accomplishes the same thing as the PHP example. Pseudocode:

values.favoriteSport = favoriteSport; // set the value of favoriteSport to an array key named favoriteSport
question = 'favoriteSport'; // the name (or build this string by concatenating parts in a loop, no difference)
log question; // prints the text "favoriteSport"
log values[question]; // prints the value of favoriteSport

#9

This solution worked for me. In case anyone needs the Webcore implementation of this solution, here is an example:


#10

Where is the Values index “favoriteSport” defined?