Any fast way to populate an array?


#7

Wellllllll… That’s not entirely true. It can be done in a single line, as seen below.
(ignore the highlighted region)


Unfortunately, I think it takes more work to code it this way…
(and it’s easier to mistype the syntax)


#8

Agreed. (Though the pedantic part of me says that even webCoRE says that’s four lines :slight_smile: ) If I had to do it, that’d be the way I’d do it, but since my primary use for wanting to pre-load a list is a list of strings, the comma delimited single string works well and can be done as a constant in the define.

“Unfortunately, I think it takes more work to code it this way…”

Yes, and that goes against my “enlightened laziness” nature.


#9

I agree. The main reason I even mentioned it is for others finding this thread later…

I guess, the philosophy I usually adhere to when coding is, I would rather more text/lines, IF it makes the piston easier to read and/or edit… or perhaps more importantly, if it executes smoother.

I rarely condense multiple lines into one just for the sake of less “bytes”.
(we live in a terabyte world, and each piston is less than 0.1 megabytes)


#10

Just for the record: I created the “epmk” piston above just to see if it could be done in one line, but I normally would populate the array like this

temp

I use the drag & drop method to copy the “Alex” line 3 times. This makes editing quick & easy.

Once I have pressed TEST, the array gets populated. At that point, I will often Disable that one block, since those types of variables only change a couple of times a year. (that block doesn’t need to run at each trigger) This also makes future edits a breeze. If I want to add or change any of the current variables, I just Enable that block again.


#11

Curiosity got the best of me… so I had to run a few tests.

It seems that when comparing “looping on one line
loopY

versus “four individual commands”
loopN

the loop method takes up more than double of the allowed space.


Real life testing using the two snippets above:

Loop    X 10 = 13 'chunks' of data
No Loop X 10 =  6 'chunks' of data

I think I will continue using the four individual lines to conserve space.
(in this case, the “pretty” code also takes up the least amount of space)


#12

" I would rather more text/lines, IF it makes the piston easier to read and/or edit… or perhaps more importantly, if it executes smoother."

Exactly. “Cute code” can be fun to write, but it’s not at all fun to try to read when you haven’t looked at it for a few months.

Once I have pressed TEST, the array gets populated

I had thought a while back that having a non-triggered “initialize variables” piston could come in handy for situations like that, but that would require using globals. Disabling a block in the code means you can keep things local. I like that.


#13

Hi all, bumping this topic from last year as I’m in a similar situation, however I am trying to populate an array of times instead. According to what I’ve read and experienced, I have to do a series of manual
Set Variable myArray[0] = value1
Set Variable myArray[1] = value2
Set Variable myArray[2] = valueN

Is there really to this date no way or workaround for setting an Array item with a variable index, i.e. Set Variable myArray[myIndex] = something?

Thanks,
/Max


#14

Arrays, or Lists, in WebCoRE are interesting objects. No, there is no simple way to populate an array. I think this is because the indexes are not even actually numbers. For example, you might want

   myArray[0]=value1
   myArray[1]=value2

and that works just fine. You can then reference myArray[0] or myArray[1] to get back the values you stored. You can also, however do this:

   myArray["green"]="Grass"
   myArray["blue"]="Sky"

In this case if you try to reference myArray[0] WC will have no idea what you are talking about. myArray[“green”] however will return “Grass”. So it is not a normal array and, therefore, cannot be populated in simple ways.


Set variable value for Sonos preset from smartthings
#15

Eloquently described, @guxdude:+1:

For what it’s worth, I am a fan of creating just a single line, IE:
Set Variable myArray[0] = value1
… and then Drag / Copy / Edit that line a few times.

It takes me about ten seconds per line, and then I disable the block afterwards, in case I ever need to initialize them again


#16

Huh, how about that… Thans for claryfying. So the index is actually more a mnemonic reference. Interesting. @guxdude It should be said I came across this posting: Correct Way to Do Array?
It would appear what’s being done in @michicago 's loop example is exactly what I need, however it does not work for a list of timestamps. Even if it supposedly works for a string array, it does however seem a bit over the top to convert back and forth between string and datetime formats just to process in an array. I guess I’ll have to bite the bullet and populate the array manually, bleh.


#17

Note that the loop example there is assigning data from one list to another list but the source list still needed to be populated manually at the beginning of the piston. Copying data can be simplified but you have to start somewhere. So, yes, bleh. :persevere:


#18

So, wait a sec - are you saying that if the array is manually pre-populated/initialized at piston start one can later set other values using a for-loop’s $index then? Copying data from one list to another is actually what I’m trying to do.

Thanks
/Max


#19

Sure. Once the one list is populated, you can copy it to another in a for loop. You can even do manipulations using formulas for the indexes (like reverse the order) as long as all the indexes are valid.


#20

Okay great. I think I can work with that. Thanks a bunch for the help dude. Strangest array objects I’ve ever dealt with :slight_smile: Btw, this is the thing I’m tinkering with. I’ve got 4 irrigation circuits which I (wifey) needs (wants) rather granular control over. I’m using 3 virtual dimmer devices for each circuit. The level slider values are for configuring duration for manual and automatic as well as start time in the AM. One of the switches (twice) allows one to add another water cycle at opposite hours:


#21

Very nice. I’m planning to build a z-wave sprinkler controller soon (always next week haha). Seems like the ones on the market are very expensive. What are you using?


#22

I’ve got four circuits. The two first are the built in sprinkler systems for the property. These have 24V AC solenoids (which I believe are standard for permanent irrigation systems) Once they were connected to one of those green Orbit timer boxes sitting in the garage. Instead I disconnected the timer box and hooked the solenoids up to a couple of powersupplies I had sitting around. These in turn are just plugged in to a couple of z-wave switches. Easy-Peasy-LemonSqueasy.

Note: IF you have 3 wires coming out of the wall, it just means the the two solenoids share a wire for one side of the coil. All you have to do is connect the common wire to ONE of the terminals on BOTH power supplies

The two other are auxiliary circuits for wifey’s potted plants and veggie garden, where I had to run my own tubing. The hardware consists of a cheap 110V solenoid valve, a 110V zigbee switch and everything mounted in a nice box on the wall. The box below has 4 switches inside (two others control pool shower and mister cooling systems (we’re in the Nevada desert, it gets kinda toasty out here :slight_smile: Each circuit has a manual override switch just in case of… well Murphy and Mesh failure


#23

is there a different way to index a string array then? it seems to work with a device array, but as you said does not work with the string array (like below, which does not work)

define
string actionList = held,pushed
device typeList = Plug1 and Light1
device activeDevice
end define
....
set variable activeDevice = {arrayItem(indexOf(actionList,$currentEventValue),typeList)

#24

I haven’t discovered a way to index a string but I have a workaround as long as the list is short (the other option is a loop). Essentially define a new integer variable based on where the event falls in the string list and then use that integer the pull the device.

set variable x = {$currentEventValue == arrayItem(0,actionList) ? 0 : ($currentEventValue == arrayItem(1,actionList) ? 1 : '')}
set variable activeDevice = {arrayItem(x,typeList)

#25

The difference with string arrays is each character is an ‘item’ rather than being able to separate by comma. You would have to search for the commas and create your own index.


#26

that seems like it’s exactly what I need. Thank you, because I would not have known how to write that expression.