Pulling data from a string variable


#1

1) Give a description of the problem
So what I’m trying to do is when one of my Philip’s Hue Light Bulb or bulbs turn on they pull specific data out of a global variable that stores all of the attributes for specific hue scenes. The global variable is set up in the form of device, color temperature, hue, saturation, and level. So far i have been successful with setting the lights attributes when all of the lights turn on. But when I try to pull the data to set one bulb the index of the string is off and i cannot for the life of me figure out how to make this work. If someone could please hello me figure this out I’d really appreciate it, I’m completely stumped and have spent weeks trying to figure it out.

Here is a example global variable that I’m using

Bed Lightstrip,6500,248,100,76,Ceiling,6500,216,98,76,Center,6500,158,100,75,Chandelier 1,2571,61,100,76,Chandelier 2,6500,273,87,76,Chandelier 3,6500,356,100,76,Chandelier 4,6500,133,100,76,Chandelier 5,6500,252,100,76,Desk Go,6500,7,100,70,Desk Lightstrip 1,6500,266,85,75,Desk Lightstrip 2,6500,165,69,70,Dresser Go,6500,360,96,76,Left,6500,237,100,75,Left Nightstand,6500,140,85,76,Right,6500,277,100,75,Right Nightstand,6500,262,85,76,TV Lightstrip,6500,345,100,75,

2) What is the expected behaviour?
What I’m trying to accomplish is depending on the time of day and the current location mode have preset light scenes that are saved as global variables. When turning all of the lights on there is no issue setting the bulbs to the correct color temperature, hue, saturation, and level. But when trying to set one bulb to the correct attributes I’m having a ton of trouble pulling out the desired data string.

3) What is happening/not happening?
What’s happening is that the index of the values isn’t consistent. For example when i make an equation that correctly finds the index of the TV Lightstrip and its attributes, then ill use the same equation to find the index of the Dresser Go it gives me the wrong values.

**4) Post a Green Snapshot of the
I don’t have a code that works for this but ill post my code that works when I’m turning all of the lights on. And here’s the code 0f4e and i apologize i wrote this on my phone.

5) Attach logs after turning logging level to Full
This code currently works.

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

My initial reaction, without looking at the code, is that I would use arrayItem instead of indexOf to accomplish this task.

You have five items for each device (i.e., Ceiling,6500,216,98,76) and 16 or 17 devices if I counted correctly. If you assign each device a number starting with zero (i.e., Bed Lightstrip=0,Ceiling=1,Center=2) then you can get the attributes one at a time using a formula like this

arrayItem(5*deviceNumber+attributeNumber, yourGlobalVariable)

So if you’re looking for the attributes for Center, which is Device Number 2
arrayItem (5 * 2 + 0,yourGlobal Variable) = Center
arrayItem (5 * 2 + 1,yourGlobal Variable) = 6500
arrayItem (5 * 2 + 2,yourGlobal Variable) = 158
arrayItem (5 * 2 + 3,yourGlobal Variable) = 100
arrayItem (5 * 2 + 4,yourGlobal Variable) = 75

Edit: Fixed Formula & Editor Formatting Issue


#3

So this is a Piston I’ve been using just to figure out how to program it

dxk2

Im just confused on why it doesn’t work, I think the logic is the it just doesn’t find the right index and I’ve got no idea why but I’ll definitely try what you posted above


#4

Thanks for the help, I don’t know why I was over complicating the hell out of it, but heres my somewhat final result.


#5

Looks good!

“Somewhat final” is a good description for every piston I’ve ever written. I’m forever tinkering with mine, sometimes to modify functionality, other times just to streamline the code.


#6

Or in my case, to break it! LOL


#7

I’ve done that too. :upside_down_face:


#8

Yeah I tend to do that, or just completely rewrite the code to either streamline it or use completely different functions/expressions to write the same code.

I’m having some issues though with the last part, when the lights turn off during night. What im trying to do is preset the lights during the night mode so that the lights aren’t coming on at a high level but everytime the lights turn off it switches the scene to relax and some of the bulbs go on and off a couple of times. Do you have any idea what might be going on or why its now doing that?


#9

If you look at the left-hand side of the image you posted, you’ll see the little lightning bolts indicating that a trigger is present in that line. You’ve got several. Each time one of those elements changes, the entire piston is going to re-evaluate. That can cause orphan lines like #33 to execute when it’s not intended, or cause parts of the piston (i.e., WAITs) that are already executing to be abandoned.

The biggest issue I see as I look at it again, is the section of code beginning with line 65. You’ve buried an IF statement with multiple triggers (lines 66 and 67) inside another IF statement. While I’m not as strict as some here when it comes to having triggers in multiple places in a single piston, nesting them is going to cause trouble 99.99% of the time.

The first thing I’d do is move that block under the ELSE (starting at line 65) to its own block. Then make absolutely sure that none of the actions under any of the SWITCH or IF blocks is causing one of the other blocks to execute.


#10

So I have been trying to deal with that, I have it nested because I was trying to figure out how to make everything come together but its not working. I’m still playing around with it trying to figure out a good way to make it work. I’ll just run down what I’m trying to do and will probably re-post where I’m at to see what I could do to improve it.

Ideally, my end goal is to have a piston or pistons that does this.

  1. If lights switch is turned on,
    then set bulb(s) to preset attributes.
  2. If the dimmer switch is pressed - associated bulb(s) are off,
    then set bulb(s) to preset attributes.
  3. If motion sensor becomes active - illuminance is less than x - associated bulb(s) are off,
    then set bulb(s) to preset attributes for y minutes.
  4. If Time-of-Day changes - a) bulb(s) that are currently on - or - b) illuminance of motion sensor is less than x - associated bulb(s) are off - room is occupied,
    then set bulb(s) to preset attributes.
  5. If location mode changes - illuminance of motion sensor is less than x - room is occupied,
    then set bulb(s) to preset attributes.

I know that it is a lot but I’m learning/working on how to do it, I wish I had a better understanding on the syntax. Because I understand whats going on and how to write code its just the syntax that’s really screwing me up when trying to write formulas.


#11

Yeah I’m still stumped, so far the part of the piston that sets the lights is good, the scenes are good, but whats throwing everything off and I don’t know the syntax of the time functions that well. What I’m trying to accomplish is that when location mode turns to night I want the lights to be set to nightlight and after they are all set to then turn off the lights. I think I know that I need to use either a wait, previous age, or a lockout function? And for when the the location mode is already night or away the lights should be off, and when changing to morning or arriving I want the lights to turn on. Here is my piston so far.


#12

And I guess one last thing for now… hahaha is that I think the switch that is setting the scenes isn’t reading correctly. For instance I just ran the piston while set to home and it set to light mode to read but my calling variable is set to relax. I don’t know why its starting to do that now.


#13

Usually the best place to start is with separate pistons as it reduces the complexity of the code and makes it much easier to debug.

I’m going to be slammed the next 2-3 days so I might not get back to this right away. If you’re in a hurry, maybe one of the other regulars like @WCmore can step in and take a closer look at your piston.


#14

I agree with @bthrock… I would likely split these into 5 pistons centered around their respective triggers.


Note: You may already have basic pistons created for 1, 3 & 5.
If so, you can likely just add a new block to your existing pistons for those triggers.


#15

Yeah I came to that conclusion last night when i tried to go to bed and the light show started… haha i found out why the scene kept on going to relax, it was a different Piston doing that. And thanks again for the help @bthrock for helping and @WCmore for looking at it to. I’m going to make the 5 pistons and see where out goes!