Making a variable point to another variables' value?


#1

1) Give a description of the problem
I’m trying to create a For Each loop that’ll walk through each light, pull the wanted settings from an array of variables, and apply them. The issue I’m running into is that when I try to apply the settings for the variable to a placeholder variable, it saves the variables’ name instead of the value it holds. Because of the way it functions, it has to auto-generate the name of the variable pulled with each run-through, which is part of what I think the issue is.

Keep in mind this’s a small test script; I have more than three rooms, so I’d need to be able to expand whatever was used.

2) What is the expected behavior?
That the value of the generated variable pointed to is saved.

3) What is happening/not happening?
The name of the variable is saved instead.

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

(I know it’s supposed to be a Green Snapshot, but the naming scheme is important to the way the script functions and there’s nothing that should be kept private.)

5) Attach any logs (From ST IDE and by turning logging level to Full)
`

+1ms ╔Received event [Script Testing].button = pushed with a delay of 67ms
+120ms ║RunTime Analysis CS > 11ms > PS > 27ms > PE > 82ms > CE
+123ms ║Runtime (49793 bytes) successfully initialized in 27ms (v0.2.100.20171211) (122ms)
+124ms ║╔Execution stage started
+131ms ║║Comparison (enum) pushed gets (string) pushed = true (1ms)
+132ms ║║Cancelling condition #2’s schedules…
+133ms ║║Condition #2 evaluated true (5ms)
+133ms ║║Cancelling condition #1’s schedules…
+134ms ║║Condition group #1 evaluated true (state changed) (6ms)
+226ms ║║Cancelling statement #52’s schedules…
+238ms ║║Executed virtual command setVariable (1ms)
+242ms ║║Dining_Room_switch
+243ms ║║Executed virtual command log (1ms)
+247ms ║║Cancelling statement #52’s schedules…
+254ms ║║Executed virtual command setVariable (1ms)
+258ms ║║Kitchen_switch
+259ms ║║Executed virtual command log (1ms)
+263ms ║║Cancelling statement #52’s schedules…
+270ms ║║Executed virtual command setVariable (0ms)
+275ms ║║Living_Room_switch
+275ms ║║Executed virtual command log (1ms)
+278ms ║╚Execution stage complete. (154ms)
+361ms ╚Event processed successfully (356ms)

`


#2

Put curly brackets around your variable name to indicate you want the value, not the name. {variable}

What is your end goal with the device names? Might be able to help you come up with a better way to manage that.


#3

Same issue, when adding curly brackets.

+1ms ╔Received event [Script Testing].button = pushed with a delay of 81ms
+138ms ║RunTime Analysis CS > 14ms > PS > 38ms > PE > 86ms > CE
+141ms ║Runtime (49847 bytes) successfully initialized in 38ms (v0.2.100.20171211) (139ms)
+142ms ║╔Execution stage started
+149ms ║║Comparison (enum) pushed gets (string) pushed = true (1ms)
+150ms ║║Cancelling condition #2’s schedules…
+151ms ║║Condition #2 evaluated true (5ms)
+152ms ║║Cancelling condition #1’s schedules…
+153ms ║║Condition group #1 evaluated true (state changed) (6ms)
+362ms ║║Cancelling statement #52’s schedules…
+374ms ║║Executed virtual command setVariable (0ms)
+379ms ║║Dining_Room_switch
+379ms ║║Executed virtual command log (1ms)
+383ms ║║Cancelling statement #52’s schedules…
+390ms ║║Executed virtual command setVariable (1ms)
+394ms ║║Kitchen_switch
+395ms ║║Executed virtual command log (1ms)
+399ms ║║Cancelling statement #52’s schedules…
+406ms ║║Executed virtual command setVariable (1ms)
+410ms ║║Living_Room_switch
+410ms ║║Executed virtual command log (1ms)
+413ms ║╚Execution stage complete. (271ms)
+495ms ╚Event processed successfully (494ms)

I set line 42 to use Expression as the type of value, so it should already have one. In addition, I added an additional set.

The idea is that, for now, each device has set levels I want. So, for instance, I want the bathroom lights to turn on. I set the variable to control this({Bathroom_switch}), and when the small block section runs, a small placeholder variable({device_var_switch}) is loaded with the information from {Bathroom_switch} by concatenating the $device in the current iteration plus the function being controlled. Hence, line 42 is saying “Load {device_var_switch} with $device plus _switch, after replacing all spaces in $device with underscores”. My intent is to eventually replace this long list of variables with arrays where each device is a single variable named after the location and light mode({Living_Room_NightLight}, for instance) holding the entire setup for that room, but that won’t be until I can get at least this part to work.

The problem seems to be that when it does that, instead of the result of the concatenation being the variable which calls the result, it’s a string of the variable name, and I’m not sure how I can force it to either A) Store it as a variable name(Thereby making {device_var_switch} = {Bathroom_switch} = on) or B) Have it immediately turn into the value held in the variable and store that in the variable({device_var_switch} = on). Either one would work, frankly.

The reasoning behind this is that I currently have an extremely large script which controls the lighting modes for my house, and I’d like to par them down. I figured a more dynamic script like this might be the way to go.


#4

Well, I’ll just assume that’s a no-go for this, then.


#5

Is what you’re trying to do more complicated than this? Tried to use strings and integers - both worked fine:


#6

Yes. I’ve made another example, which shows just the concept I’m going for.

In this case, I have three static variables; Value1, Value2, and Value3. During each loop, I need to call a different one, so I put together an concatenation expression to pair together the string “Value” and the loop index to create the names of the variables. Thus, on loop 1 the variable Value1 will be generated and loaded into loopValue, and loopValue should have a value of “on”, since that is the value of Value1. Instead, it loads the literal string “Value1”.

11

125ms, 144ms, and 163ms shows the current value of loopValue, and in all three cases it’s literally just the name of the variable, not the contents. In creating this example, I also added curly brackets around the concat() function, and put it in as an expression, which should have implied curly brackets as well.


#7

Have you tried using a list variable? Your loop number would point to a location in the list and pull that value for you by using variable[0], variable[1]etc where the number between the brackets is represented by your loop variable.


#8

The big issue with that is that in the final script, I’m using room names and arguments. So I might have Kitchen_Nightlights. I’ll be using a list variable to hold the various values for the lights.

So I need to be concatenating room name + light mode, because I won’t be able to just use loop index in the final script.