Referencing a Global variable within Local Variable using arrayItem and indexOf


#1

I’m currently refining a Sonos piston and am seeking expertise on a specific reference challenge.

Summary: The challenge lies in referencing variables within a piston, particularly when using global variables inside a local piston, leading to unexpected behavior in a formula. The issue appears to be that the formula isn’t utilizing the intended value but instead is including all the global variables in the calculation as one string.

Use Case: Every workday, a scene kicks off, managing various tasks, including adjusting the Sonos volume in the Basement (my wife’s office) and the Kitchen. When the workday ends, another scene takes over to restore the Sonos settings.

Setup: Sonos volume control is organized into sub-routines (sub-pistons) called by a main piston, ensuring easy reusability.

Challenge: In the current iteration, manually populating the local variable “DefaultVolumeList” has led me to contemplate referencing a variable within another variable, all while maintaining the efficacy of arrayItem and indexOf.

Current Piston:

Log Results: Fortunately, the log shows that the calculations are on point!

1/24/2024, 5:29:41 PM +223ms
+4ms	╔Received event [Hubitat Hub].execute = [86.48.14.102, 130.176.130.148] with a delay of 41ms, canQueue: true, calledMyself: false
+36ms	║RunTime initialize > 35 LockT > 1ms > r9T > 20ms > pistonT > 19ms (first state access 10 m:14 5 30)
+40ms	║Runtime (9768 bytes) initialized in 20ms (v0.3.114.20231008_HE)
+42ms	║╔Execution stage started
+54ms	║║Executed virtual command [Sonos Advanced - Kitchen Player].setVariable (2ms)
+59ms	║║Executed virtual command [Sonos Advanced - Kitchen Player].wait [55] (0ms)
+60ms	║║executeTask: Waiting for 55ms; lastPause: null
+108ms	║║Calculating (decimal)29.0 * (decimal)0.85 >> (decimal)24.65
+110ms	║║Calculating (decimal)24.65 , (decimal)0.0 >> (decimal)24.65
+151ms	║║Executed device command [Sonos Advanced - Kitchen Player].setVolume(24) (36ms)
+161ms	║║Executed virtual command [Sonos Advanced - Laundry Player].setVariable (2ms)
+165ms	║║Executed virtual command [Sonos Advanced - Laundry Player].wait [55] (1ms)
+166ms	║║executeTask: Waiting for 55ms; lastPause: 1706135381313
+216ms	║║Calculating (decimal)30.0 * (decimal)0.8 >> (decimal)24.0
+218ms	║║Calculating (decimal)24.0 , (decimal)0.0 >> (decimal)24.0
+248ms	║║Executed device command [Sonos Advanced - Laundry Player].setVolume(24) (25ms)
+257ms	║║Executed virtual command [Sonos Advanced - TV Room Player].setVariable (2ms)
+261ms	║║Executed virtual command [Sonos Advanced - TV Room Player].wait [55] (1ms)
+262ms	║║executeTask: Waiting for 55ms; lastPause: 1706135381420
+315ms	║║Calculating (decimal)21.0 * (decimal)0.75 >> (decimal)15.75
+318ms	║║Calculating (decimal)15.75 , (decimal)0.0 >> (decimal)15.75
+353ms	║║Executed device command [Sonos Advanced - TV Room Player].setVolume(15) (30ms)
+358ms	║╚Execution stage complete. (317ms)
+366ms	╚Event processed successfully (362ms)

Revised Piston: In the proposed revision is the introduction of sections responsible for writing to specific global variables originating from the Workday Morning Sonos Volume Adjuster (Sub). These globals subsequently play a pivotal role in the Workday Evening Sonos Volume Adjuster (Sub).

Issue: The challenge I’m facing is that when using global variables in the DefaultVolumeList local variable, the formula in NewVolumeToSet exhibits unexpected behavior. It isn’t using the value to execute the formula; instead, it is cramming all the global variables in.

By the way, I have separate lines for writing to the global variables only for testing today. I’ll use automation to load this when I can get this concept to work first and have time to figure out the best way to do that.

Piston with Changes:

Log Results (Concerning Observations): As you can see from the log, it appears to be trying to use all the Global Variables DefaultVolumeList instead of the specific required one.

1/24/2024, 5:43:20 PM +555ms
+5ms	╔Received event [Hubitat Hub].execute = [86.48.14.102, 130.176.130.148] with a delay of 40ms, canQueue: true, calledMyself: false
+39ms	║RunTime initialize > 38 LockT > 1ms > r9T > 30ms > pistonT > 28ms (first state access 2 m:7 6 32)
+43ms	║Runtime (10634 bytes) initialized in 30ms (v0.3.114.20231008_HE)
+45ms	║╔Execution stage started
+59ms	║║Executed virtual command [Sonos Advanced - Kitchen Player].setVariable (2ms)
+68ms	║║Executed virtual command [Sonos Advanced - Laundry Player].setVariable (3ms)
+77ms	║║Executed virtual command [Sonos Advanced - TV Room Player].setVariable (2ms)
+86ms	║║Executed virtual command [Sonos Advanced - Kitchen Player].setVariable (2ms)
+90ms	║║Executed virtual command [Sonos Advanced - Kitchen Player].wait [55] (1ms)
+91ms	║║executeTask: Waiting for 55ms; lastPause: null
+130ms	║║Calculating (string)20 + (string), >> (string)20,
+131ms	║║Calculating (string)20, + (string)20 >> (string)20,20
+132ms	║║Calculating (string)20,20 + (string), >> (string)20,20,
+133ms	║║Calculating (string)20,20, + (string)15 >> (string)20,20,15
+149ms	║║Calculating (decimal)20.0 * (decimal)0.85 >> (decimal)17.0
+151ms	║║Calculating (decimal)17.0 , (decimal)0.0 >> (decimal)17.0
+182ms	║║Executed device command [Sonos Advanced - Kitchen Player].setVolume(17) (26ms)
+192ms	║║Executed virtual command [Sonos Advanced - Laundry Player].setVariable (2ms)
+196ms	║║Executed virtual command [Sonos Advanced - Laundry Player].wait [55] (0ms)
+197ms	║║executeTask: Waiting for 55ms; lastPause: 1706136200677
+235ms	║║Calculating (string)20 + (string), >> (string)20,
+236ms	║║Calculating (string)20, + (string)20 >> (string)20,20
+237ms	║║Calculating (string)20,20 + (string), >> (string)20,20,
+239ms	║║Calculating (string)20,20, + (string)15 >> (string)20,20,15
+255ms	║║Calculating (decimal)20.0 * (decimal)0.8 >> (decimal)16.0
+258ms	║║Calculating (decimal)16.0 , (decimal)0.0 >> (decimal)16.0
+287ms	║║Executed device command [Sonos Advanced - Laundry Player].setVolume(16) (24ms)
+297ms	║║Executed virtual command [Sonos Advanced - TV Room Player].setVariable (2ms)
+301ms	║║Executed virtual command [Sonos Advanced - TV Room Player].wait [55] (0ms)
+302ms	║║executeTask: Waiting for 55ms; lastPause: 1706136200782
+343ms	║║Calculating (string)20 + (string), >> (string)20,
+345ms	║║Calculating (string)20, + (string)20 >> (string)20,20
+346ms	║║Calculating (string)20,20 + (string), >> (string)20,20,
+348ms	║║Calculating (string)20,20, + (string)15 >> (string)20,20,15
+368ms	║║Calculating (decimal)15.0 * (decimal)0.75 >> (decimal)11.25
+370ms	║║Calculating (decimal)11.25 , (decimal)0.0 >> (decimal)11.25
+399ms	║║Executed device command [Sonos Advanced - TV Room Player].setVolume(11) (24ms)
+404ms	║╚Execution stage complete. (358ms)
+410ms	╚Event processed successfully (405ms)

Question: How can I get around this?

Thank you for your time and expertise.


#2

It appears I was focusing on the wrong area while verifying the accuracy of the results. In reality, this piston does function correctly