Hi all! I’m new to Sharptools and am looking for some pointers.
I would like to create a role that rotates the color/hue of an LED light through 4 different hues that represent Red Blue Green and Orange. There is no way that I can tell to define a variable that is an array of integers via the “Manage Variables” feature. So in my rule I set a variable like this:
What is the type of the $colorArray variable? I suspect it was created as a Text variable, so the expression is ultimately getting implicitly serialized to a string.
You can also explicitly serialize and deserialize data to a string. If the variable type is Text, then values will implicitly be serialized to a string, but it will stay a string after being written to a Text variable. If you want to parse it back into array/object format for use within an expression, you must do that explicitly within the expression.
Under the General String Manipulation section of the General Functions help article, you’ll find the functions stringify() and parseJson() which you can use to explicitly serialize and deserialize object data to/from a string.
So you could do something like the following if you want to explicitly serialize the data (though the implicit serialization may be fine for your needs):
Or if you know the values you want in the array, just write it directly into the string as it sounds like the deserializing the data back to an array is the main part you need (eg. Set Variable $colorArray to static value [71,33,11,0])
Then you could deserialize the string into an array and grab the item you want out of it:
This is using a trick that creates a hyper-local variable called hues within the expression evaluation just to make it easier to reference things. Since your $colorArray will already have the value set, you should be able to Tap to Evaluate the expression and see the result to make sure your syntax is right.
Make sure your $colorHue variable is a Numeric type if you want to use it in commands that expect a numeric argument.
If you wanted a random value from the array rather than to sequentially iterate through the colors, you could use the pickRandom([item1, item2, ... itemN]) function also documented in the help article linked above:
Thanks for the update. In that case, the ‘Tap to Evaluate’ is capable of evaluating the expression and returns the raw result… but since the expression ultimately tries to save an Array to a Number, the implicit conversion fails and it would get saved to the variable as null/NaN (‘Not a Number’) when the rule actually runs.
If you don’t have any need for the $colorHueXXX variables individually, either deleting and recreating the $colorHueArray as a string and setting the value manually, or even just statically entering the array into the expression for setting the $colorHueValue could work for you (or the random() approach if you want some randomness in the order items are picked).
Set $colorHueValue to expression
[71,33,11,0][ $cyclingCounter % 4 ]
I suppose that’s the neat thing with expressions - they provide lots of flexibility in how you implement things depending on your needs!
Ok thank you. That comes out to a rate of every 3 seconds. I’ll set my rule back to include a 10 second delay and we’ll see what happens. If it fails again with that error condition I’ll contact support and hopefully they’ll be able to see what’s going on.
While we’re talking about arrays, if a user is trying to filter an existing array for values that don’t match a given value, should they write the expression in this form: filter ([1,2,3], x<>3)
or this form: filter ([1,2,3], not(x=3))
My end goal is to use filter() in this way as a makeshift remove(). May need get even more creative in the absence of min, max, first, last, push, pop, etc. functions.
Either approach would work, but the correct syntax would be:
filter ([1,2,3], x != 3)
filter([1,2,3], not(x == 3))
In the second case, the single = is an assignment operator rather than an equality comparison == and in the first case, the ‘not equals’ comparison operator would be !=
Edit: I would note that min(), max() and similar math functions exist. You can get the first and last indexes from an array based on the index position. I’ll double check on the math functions as they might be missing from the docs.