Function to go through an array of arrays?

Hi there!

This would be more like a js question, but I wouldn't mind doing this with lots of nodes either. I am trying to figure out how to cycle through an array of arrays on each bang.
There are 3 numbers that constitute the RGB values of a light bulb. And I have a list of RGB values that I would like to be sent whenever a new message is passed.

Pseudocode would be something like this:

  1. Create an array that contains the following numbers: (234, 182, 62), (239, 210, 118), (241, 224, 181).. There are 20 pairs in this list
  2. Whenever a new message is received, a random pair would be output.
  3. If possible actually output should not be random, but more like random, except that it keeps track of each number which has been generated so not to repeat it until all the pairs have been sent. As a reference, I am using the urn object in another programming language that I use called Max MSP. See here.

Any help is truly appreciated!!

  1. you create an array with following values (can be done in change node - using jsonata expression)
      { "rgb" : [234, 182, 62 ],
        "randomNumber" :  <randomly generated number>
     }
  1. you sort the array according "randomNumber" (can be done in change node - using jsonata expression)
  2. you store the array in context variable
  3. you store also indexInArray = 0 in context variable.
  4. whenever a new message is received, you retrieve the indexInArray and array from context variable and take the "rgb" for index = indexInArray and you increment indexInArray (and if this is >= count cells in array then you reset indexInArray to 0).

I think @yqreq wants an array of arrays, something like [[234, 182, 62],[239, 210, 118],[...]...]

Thank you, guys! I am a newbie in case it was not obvious. Indeed I am trying to access arrays of arrays. @janvda, your example is a bit too complex for my knowledge. I don't even know if it helps with my situation.

For now I am working on this. Of course it does not work. But I guess as it is, it is not that difficult for a more experienced user.

Any suggestion on how I can change the js code to have the 3 rgb values as output?

[{"id":"3ebcecc8.c11834","type":"random","z":"34e6c4e0.08031c","name":"","low":"1","high":"3","inte":"true","property":"payload","x":380,"y":540,"wires":[["c727d778.b08b18","65ffc897.731068"]]},{"id":"92a8b33f.2bd7f","type":"debug","z":"34e6c4e0.08031c","name":"result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":540,"wires":[]},{"id":"e9b4808b.575e3","type":"inject","z":"34e6c4e0.08031c","name":"bang","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":210,"y":540,"wires":[["3ebcecc8.c11834"]]},{"id":"c727d778.b08b18","type":"function","z":"34e6c4e0.08031c","name":"color","func":"\nvar index =  msg;\nvar color = [254, 0, 0]; \n\nif (index == 1){\n    color = [234, 182, 62];\n} else if (index == 2){\n    color = [239, 210, 118];\n} else {\n    color = [241, 224, 181];\n}\n \nmsg.color = color;\nreturn msg.color;\n\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":600,"y":540,"wires":[["92a8b33f.2bd7f"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"65ffc897.731068","type":"debug","z":"34e6c4e0.08031c","name":"random_number","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":600,"wires":[]}]

This is the function node code:


var index =  msg;
var color = [254, 0, 0]; 

if (index == 1){
    color = [234, 182, 62];
} else if (index == 2){
    color = [239, 210, 118];
} else {
    color = [241, 224, 181];
}
 
msg.color = color;
return msg.color;

It may look a bit more complicated but it is not actually.
Here is something for you to work with.

[{"id":"c6be2ff2.e4682","type":"function","z":"6da49dd5.862364","name":"","func":"//HELPER FUNCTIONS\n// to generate integer between 0 - 255\nfunction makeNumber(){\n    return Math.floor(Math.random()*256)\n}\n\n// to make an array (3 elements) of integers \nfunction makeTriple(){\n    let a = []\n    for(let i= 0;i<3;++i){\n        a.push(makeNumber())\n    }\n    return a\n}\n\n// to make an array (20 elements) of arrays of integers \nfunction makeSetOfTriples(){\n    let a = []\n    for(let i=0 ;i<20;++i){\n        a.push(makeTriple())\n    }\n    return a\n}\n//END OF HELPER FUNCTIONS\n\n\n// define variable that holds current set (array) (if doesn't exist, make an empty array)\nlet currentSet = context.get('current') || []\n\n//if there is no elements in currentSet, make whole new set\nif(currentSet.length == 0){\n    currentSet = makeSetOfTriples()\n}\n\n//make one random integer which is between 0 and count of currentSet elements\nlet random = Math.floor(Math.random() * currentSet.length)\n//get an array (3 elements) from currentSet which is at the index of just generated random\nlet result = currentSet[random]\n//remove that element from currentSet\ncurrentSet.splice(random,1)\n//store changed current to context.\ncontext.set('current',currentSet)\n//uncomment next line to print out currentSent if needed for anlayse \n//node.warn('currentSet : '+currentSet)\n//place result (array of 3 elements) to msg.payload\nmsg.payload = result\n// out goes the msg\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":350,"y":440,"wires":[["67fa5597.a1ca3c"]]},{"id":"c4c5d77d.1f6a38","type":"inject","z":"6da49dd5.862364","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":200,"y":440,"wires":[["c6be2ff2.e4682"]]},{"id":"67fa5597.a1ca3c","type":"debug","z":"6da49dd5.862364","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":440,"wires":[]}]
1 Like

Wow! This is genius! I definitely saved this and have a deeper look as there are many things to learn from.

The thing is I already have the list of rgb colors. And I would like to cycle through them. There are 20 pairs but for simplicity's sake 3 pairs are enough: [234, 182, 62], [239, 210, 118] and [241, 224, 181]

Do you think I can mingle with the flow you shared to get this list and cycle through it?
Thank you for the very very interesting code!

These are the colors:

I just realized you had double square parentheses there. This is how you can do it when you have an array of arrays?

Absolutely!
Let's just discuss what will be the difference.
I see you already did understand how array of arrays looks like.
arrayOfArrays = [[1,2,3],[4,5,6],[7,8,9]]
That way you should write down the base set. It should be a constant. It will never change.

Still you'll need a set which will change (elements are pulled out in every "bang" ). The current set.
And now the main difference - at the moment when there is no elements in current set, you'll need to make current set equal to base set. And that will do the trick for you :slight_smile:

I want to take it step by step and I am trying to figure out how to use arrays in array. I read here that you can call an element of an array inside an array by doing something like this.

rgbPairs [0][1] to access the 2nd element of the first array. But how do you access the whole array including the commas? Do I cycle through them and add somehow the commas?
rgbPairs [0][0, 1, 2] does not seem to work

// Create an array of arrays
rgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181]];

// Create a variable to hold the array being sent out
let result = rgbPairs[0][0, 1, 2]

//place result (array of 3 elements) to msg.payload
msg.payload = result
// out goes the msg
return msg

let result = rgbPairs[0][0, 1, 2] is wrong.

in this case the rgbPairs[0] is the first element in rgbPairs array -> [234, 182, 62]
if you'll need to reach into subarray elements you can reach them like that but one index at the time

rgbPairs[0][0] is first element of first subarray -> 234

But I can't see the reason (probably because of you haven't told) why you'll need to go into subarray elements one by one.

I don't want the elements one by one. I need the whole pair of 3, including the commas.

I am trying to replicate how an Ikea remote and an Ikea color light bulb communicate via the Ikea Tradfri Bridge

This is what I got so far. You can see in the code below. If I push the middle button I get to toggle on/off the light bulb. If I short press the upper button, I get to move the brightness up 15 points on each press and if I long press, I get to move the brightness up continuously until I stop pressing. The opposite for the lower button.

Now for the left and right button, I checked the Ikea Tradfri app while pushing the buttons and it seems there are 20 color presets that I randomly cycle through whenever I press the left or right button. Of course if I press left once and then right once, I should be going back to the same color. You can check the colors in the previous replies.

To simplify matters for now I would be very happy if I put the list and just cycle through it one by one, left and right depending on which button I press. Maybe like I did with the brightness, to cycle left if I press the left button, and right if I press the right button.

I am trying to create a function to fill an array (for output) with the array I selected from the main array (array of array). But I am not very good with functions.

[{"id":"5d7be55b.f6039c","type":"server-events","z":"7503cb14.1c6134","name":"","server":"3ed2660b.f6d8ba","event_type":"zha_event","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"waitForRunning":true,"x":310,"y":360,"wires":[["40f182d9.20278c"]]},{"id":"40f182d9.20278c","type":"switch","z":"7503cb14.1c6134","name":"Remote Commands","property":"payload.event.command","propertyType":"msg","rules":[{"t":"eq","v":"toggle","vt":"str"},{"t":"eq","v":"step_with_on_off","vt":"str"},{"t":"eq","v":"step","vt":"str"},{"t":"eq","v":"move_with_on_off","vt":"str"},{"t":"eq","v":"move","vt":"str"},{"t":"eq","v":"stop","vt":"str"},{"t":"eq","v":"press","vt":"str"},{"t":"eq","v":"hold","vt":"str"},{"t":"eq","v":"release","vt":"str"},{"t":"eq","v":"move_to_level_with_on_off","vt":"str"}],"checkall":"true","repair":false,"outputs":10,"x":320,"y":520,"wires":[["e12ab7d0.f3ebc8"],["b09c6110.5bd75"],["bfb6151c.7b4478"],["7a567951.447498","5676108d.f0dfd"],["bcf002be.218b3","5ef6a19f.a5bf9"],["3fbcd8d.43d8d28","ca7965f.8711b98"],["2b9a28b8.a5d628"],["636227e2.7d1ea8"],["3db29909.5835f6","8b943136.803fb"],[]],"info":"Normal presses\n\n1 Middle - toggle\n2 Up - step_with_on_off\n3 Down - step\n4 Left - press (args: 257, 13, 0)\nRight - press (args: 256, 13, 0)\n\n\nLong presses\n\n5, 6 Middle - toggle, then release then move_to_level_with_on_off, then press (arg 2, 0, 0)\n\n7 Up - move_with_on_off and then stop (when unpressing the button)\n\n8, 9 Down - move and then stop (when unpressing the button)\n\n10 Left - hold (args: 3329, 0) and then release (arg: 3742)\n\nRight - hold (args: 3328, 0) and then release (arg: 2696)\n\n\n\n\n1 toggle\n2 step_with_on_off\n3 step\n7 move_with_on_off\n8 move\n9 stop\n4 press\n10 hold\n5 release\n6 move_to_level_with_on_off\n\n"},{"id":"416c3892.19a9f8","type":"inject","z":"7503cb14.1c6134","name":"on","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":370,"y":660,"wires":[["8b53bc2a.fb39f"]]},{"id":"a736255f.2d2278","type":"inject","z":"7503cb14.1c6134","name":"off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":370,"y":700,"wires":[["2bf65be1.e30844"]]},{"id":"8b53bc2a.fb39f","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1260,"y":520,"wires":[["f144fb51.2307d8"]]},{"id":"2bf65be1.e30844","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn OFF Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_off","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1260,"y":580,"wires":[["f144fb51.2307d8"]]},{"id":"c5594516.62d738","type":"function","z":"7503cb14.1c6134","name":"Brightness up","func":"// add actual brightness value\nvar brightness =  msg.data.attributes.brightness;\n\nbrightness = brightness + 15;\n \n\nif(brightness >= 255){\n   brightness = 254;\n    }\n        \nif(brightness < 0){\n   brightness = 0;\n\t} \n    \nmsg.data.attributes.brightness = brightness\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":980,"y":640,"wires":[["c3ceaec4.05648"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"392c2534.62f7aa","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":640,"wires":[["c5594516.62d738"]]},{"id":"5f79d746.ba18c8","type":"function","z":"7503cb14.1c6134","name":"Brightness down","func":"// add actual brightness value\nvar brightness =  msg.data.attributes.brightness;\n\nbrightness = brightness - 15;\n \n\nif(brightness >= 255){\n   brightness = 254;\n    }\n        \nif(brightness < 1){\n   brightness = 1;\n\t} \n    \nmsg.data.attributes.brightness = brightness\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1000,"y":700,"wires":[["c3ceaec4.05648"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"fd53b136.0f8d4","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":700,"wires":[["5f79d746.ba18c8"]]},{"id":"c3ceaec4.05648","type":"api-call-service","z":"7503cb14.1c6134","name":"Light Brightness","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"{            \"brightness\": {{data.attributes.brightness}},           \"rgb_color\": {{data.attributes.rgb_color}}    }","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1260,"y":640,"wires":[["872dfdd2.ac1e8"]],"info":"{\"brightness\": {{data.attributes.brightness}}}"},{"id":"122f5e5a.528e42","type":"inject","z":"7503cb14.1c6134","name":"brightness up","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":350,"y":760,"wires":[["b09c6110.5bd75"]]},{"id":"7befef54.a0fe8","type":"inject","z":"7503cb14.1c6134","name":"brightness down","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":340,"y":800,"wires":[["bfb6151c.7b4478"]]},{"id":"e959ed01.4977f","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":780,"wires":[["b732e44b.522c08"]]},{"id":"b732e44b.522c08","type":"function","z":"7503cb14.1c6134","name":"Brightness up (continous)","func":"// add actual brightness value\nvar brightness =  msg.data.attributes.brightness;\n\nbrightness = brightness + 15;\n \n\nif(brightness >= 255){\n   brightness = 254;\n    }\n        \nif(brightness < 0){\n   brightness = 0;\n\t} \n    \nmsg.data.attributes.brightness = brightness\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1010,"y":780,"wires":[["c3ceaec4.05648","b9b9e9cf.ebe548"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"b9b9e9cf.ebe548","type":"delay","z":"7503cb14.1c6134","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1270,"y":780,"wires":[["7c61ace6.58ea64"]]},{"id":"7c61ace6.58ea64","type":"gate","z":"7503cb14.1c6134","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1450,"y":780,"wires":[["e959ed01.4977f"]]},{"id":"3fbcd8d.43d8d28","type":"change","z":"7503cb14.1c6134","name":"close","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":700,"wires":[["7c61ace6.58ea64"]]},{"id":"52cf825b.58ea6c","type":"inject","z":"7503cb14.1c6134","name":"open","props":[{"p":"payload","v":"open","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":1450,"y":700,"wires":[["7c61ace6.58ea64"]]},{"id":"80c6f7a4.645af8","type":"inject","z":"7503cb14.1c6134","name":"close","props":[{"p":"payload","v":"close","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":1450,"y":740,"wires":[["7c61ace6.58ea64"]]},{"id":"7a567951.447498","type":"change","z":"7503cb14.1c6134","name":"open","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":740,"wires":[["7c61ace6.58ea64"]]},{"id":"b0d76094.de4f","type":"inject","z":"7503cb14.1c6134","name":"brightness up (cont.)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":330,"y":900,"wires":[["7a567951.447498","5676108d.f0dfd"]]},{"id":"dee30c28.e5fdf","type":"inject","z":"7503cb14.1c6134","name":"brightness stop","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":340,"y":860,"wires":[["3fbcd8d.43d8d28","ca7965f.8711b98"]]},{"id":"872dfdd2.ac1e8","type":"debug","z":"7503cb14.1c6134","name":"light brightness","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1460,"y":640,"wires":[]},{"id":"f144fb51.2307d8","type":"debug","z":"7503cb14.1c6134","name":"light on/off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1450,"y":520,"wires":[]},{"id":"6cc68035.f2b4f","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":920,"wires":[["641611f3.c4328"]]},{"id":"641611f3.c4328","type":"function","z":"7503cb14.1c6134","name":"Brightness down (continous)","func":"// add actual brightness value\nvar brightness =  msg.data.attributes.brightness;\n\nbrightness = brightness - 15;\n \n\nif(brightness >= 255){\n   brightness = 254;\n    }\n        \nif(brightness < 0){\n   brightness = 0;\n\t} \n    \nmsg.data.attributes.brightness = brightness\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1000,"y":920,"wires":[["d28c63b4.e75fa","c3ceaec4.05648"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"d28c63b4.e75fa","type":"delay","z":"7503cb14.1c6134","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1270,"y":920,"wires":[["f162388f.fec128"]]},{"id":"f162388f.fec128","type":"gate","z":"7503cb14.1c6134","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1450,"y":920,"wires":[["6cc68035.f2b4f"]]},{"id":"ca7965f.8711b98","type":"change","z":"7503cb14.1c6134","name":"close","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":840,"wires":[["f162388f.fec128"]]},{"id":"1b89e3cd.7cde0c","type":"inject","z":"7503cb14.1c6134","name":"open","props":[{"p":"payload","v":"open","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":1450,"y":840,"wires":[["f162388f.fec128"]]},{"id":"f996addd.7811","type":"inject","z":"7503cb14.1c6134","name":"close","props":[{"p":"payload","v":"close","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":1450,"y":880,"wires":[["f162388f.fec128"]]},{"id":"bcf002be.218b3","type":"change","z":"7503cb14.1c6134","name":"open","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":880,"wires":[["f162388f.fec128"]]},{"id":"2bc0dcde.20e164","type":"inject","z":"7503cb14.1c6134","name":"brightness down (cont.)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":320,"y":940,"wires":[["bcf002be.218b3","5ef6a19f.a5bf9"]]},{"id":"b5620a4d.3ea0c8","type":"comment","z":"7503cb14.1c6134","name":"Brightness","info":"","x":580,"y":580,"wires":[]},{"id":"b09c6110.5bd75","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":640,"wires":[["392c2534.62f7aa"]]},{"id":"bfb6151c.7b4478","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":700,"wires":[["fd53b136.0f8d4"]]},{"id":"5676108d.f0dfd","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":780,"wires":[["e959ed01.4977f"]]},{"id":"5ef6a19f.a5bf9","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":920,"wires":[["6cc68035.f2b4f"]]},{"id":"e12ab7d0.f3ebc8","type":"api-call-service","z":"7503cb14.1c6134","name":"Light Toggle","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"toggle","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1250,"y":460,"wires":[["1572e956.47cea7"]]},{"id":"1572e956.47cea7","type":"debug","z":"7503cb14.1c6134","name":"light toggle","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1450,"y":460,"wires":[]},{"id":"6991393c.28a9e8","type":"inject","z":"7503cb14.1c6134","name":"light toggle","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":360,"y":620,"wires":[["e12ab7d0.f3ebc8"]]},{"id":"6b744a99.331bb4","type":"function","z":"7503cb14.1c6134","name":"Color Switch Left","func":"msg.payload = {\n domain: \"light\",\n service: \"turn_on\",\n data: {\n entity_id: \"light.lab_color_bulb\",\n rgb_color: msg.data.attributes.rgb_color\n }\n};\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":1040,"wires":[["754568a7.1ee2d8"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"290bf86c.154fc8","type":"function","z":"7503cb14.1c6134","name":"Color Switch Right","func":"\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":1160,"wires":[["754568a7.1ee2d8"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"9c611585.784688","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":1160,"wires":[["290bf86c.154fc8"]]},{"id":"7dc4784b.6f2608","type":"inject","z":"7503cb14.1c6134","name":"color switch left","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":340,"y":1140,"wires":[["301221b9.b1bf8e"]]},{"id":"e206a6b6.416668","type":"inject","z":"7503cb14.1c6134","name":"color switch right","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":340,"y":1200,"wires":[["2f0e3533.aa549a"]]},{"id":"2f0e3533.aa549a","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":1160,"wires":[["9c611585.784688"]]},{"id":"ceb52def.1c308","type":"comment","z":"7503cb14.1c6134","name":"Color Switch","info":"Ikea Tradfri Bridge and App offer the possibility to switch between a set of colors. So we replicate that here.\n\nThis is the list of the 20 RGB values pairs","x":590,"y":980,"wires":[]},{"id":"84a95494.728308","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":1240,"wires":[["95bf648d.3d3bf8"]]},{"id":"95bf648d.3d3bf8","type":"function","z":"7503cb14.1c6134","name":"Color Switch Left (continous)","func":"\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1020,"y":1240,"wires":[["ebcd13f5.ca897","754568a7.1ee2d8"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"ebcd13f5.ca897","type":"delay","z":"7503cb14.1c6134","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1270,"y":1240,"wires":[["52352259.3c9f1c"]]},{"id":"52352259.3c9f1c","type":"gate","z":"7503cb14.1c6134","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1450,"y":1240,"wires":[["84a95494.728308"]]},{"id":"3db29909.5835f6","type":"change","z":"7503cb14.1c6134","name":"close","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":1160,"wires":[["52352259.3c9f1c"]]},{"id":"10f966ff.ecdb59","type":"inject","z":"7503cb14.1c6134","name":"open","props":[{"p":"payload","v":"open","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":1450,"y":1160,"wires":[["52352259.3c9f1c"]]},{"id":"a81ce50.89cc618","type":"inject","z":"7503cb14.1c6134","name":"close","props":[{"p":"payload","v":"close","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":1450,"y":1200,"wires":[["52352259.3c9f1c"]]},{"id":"7714f2db.6199ac","type":"change","z":"7503cb14.1c6134","name":"open","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":1200,"wires":[["52352259.3c9f1c"]]},{"id":"1699fcd7.036323","type":"inject","z":"7503cb14.1c6134","name":"color switch left (cont.)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":320,"y":1300,"wires":[["7714f2db.6199ac","962699a1.9df568"]]},{"id":"d4b09b6a.a69aa8","type":"inject","z":"7503cb14.1c6134","name":"color switch stop","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":340,"y":1260,"wires":[["8b943136.803fb","3db29909.5835f6"]]},{"id":"1b1aa9b3.4c43a6","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":1380,"wires":[["ea67884b.b152f8"]]},{"id":"ea67884b.b152f8","type":"function","z":"7503cb14.1c6134","name":"Color Switch Right (continous)","func":"\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1010,"y":1380,"wires":[["45f78569.cfc09c","754568a7.1ee2d8"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"45f78569.cfc09c","type":"delay","z":"7503cb14.1c6134","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1270,"y":1380,"wires":[["556655f6.96b7bc"]]},{"id":"556655f6.96b7bc","type":"gate","z":"7503cb14.1c6134","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","persist":false,"x":1450,"y":1380,"wires":[["1b1aa9b3.4c43a6"]]},{"id":"8b943136.803fb","type":"change","z":"7503cb14.1c6134","name":"close","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":1300,"wires":[["556655f6.96b7bc"]]},{"id":"7dca0d46.f243a4","type":"inject","z":"7503cb14.1c6134","name":"open","props":[{"p":"payload","v":"open","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":1450,"y":1300,"wires":[["556655f6.96b7bc"]]},{"id":"13ebf9cc.579116","type":"inject","z":"7503cb14.1c6134","name":"close","props":[{"p":"payload","v":"close","vt":"str"},{"p":"topic","v":"control","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"close","payloadType":"str","x":1450,"y":1340,"wires":[["556655f6.96b7bc"]]},{"id":"348462b6.16bdee","type":"change","z":"7503cb14.1c6134","name":"open","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1290,"y":1340,"wires":[["556655f6.96b7bc"]]},{"id":"25746a05.125116","type":"inject","z":"7503cb14.1c6134","name":"color switch right (cont.)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"bang","payloadType":"str","x":320,"y":1340,"wires":[["348462b6.16bdee","f805bf5.cd4c64"]]},{"id":"962699a1.9df568","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":1240,"wires":[["84a95494.728308"]]},{"id":"f805bf5.cd4c64","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":1380,"wires":[["1b1aa9b3.4c43a6"]]},{"id":"2b9a28b8.a5d628","type":"switch","z":"7503cb14.1c6134","name":"Remote Commands","property":"payload.event.args[0]","propertyType":"msg","rules":[{"t":"eq","v":"257","vt":"str"},{"t":"eq","v":"256","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":320,"y":1020,"wires":[["301221b9.b1bf8e"],["2f0e3533.aa549a"]],"info":"Normal presses\n\n1 Middle - toggle\n2 Up - step_with_on_off\n3 Down - step\n4 Left - press (args: 257, 13, 0)\nRight - press (args: 256, 13, 0)\n\n\nLong presses\n\n5, 6 Middle - toggle, then release then move_to_level_with_on_off, then press (arg 2, 0, 0)\n\n7 Up - move_with_on_off and then stop (when unpressing the button)\n\n8, 9 Down - move and then stop (when unpressing the button)\n\n10 Left - hold (args: 3329, 0) and then release (arg: 3742)\n\nRight - hold (args: 3328, 0) and then release (arg: 2696)\n\n\n\n\n1 toggle\n2 step_with_on_off\n3 step\n4 press\n5 release\n6 move_to_level_with_on_off\n7 move_with_on_off\n8 move\n9 stop\n10 hold\n"},{"id":"636227e2.7d1ea8","type":"switch","z":"7503cb14.1c6134","name":"Remote Commands","property":"payload.event.command.args","propertyType":"msg","rules":[{"t":"eq","v":"3329","vt":"str"},{"t":"eq","v":"3328","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":300,"y":1400,"wires":[["962699a1.9df568"],["f805bf5.cd4c64"]],"info":"Normal presses\n\n1 Middle - toggle\n2 Up - step_with_on_off\n3 Down - step\n4 Left - press (args: 257, 13, 0)\nRight - press (args: 256, 13, 0)\n\n\nLong presses\n\n5, 6 Middle - toggle, then release then move_to_level_with_on_off, then press (arg 2, 0, 0)\n\n7 Up - move_with_on_off and then stop (when unpressing the button)\n\n8, 9 Down - move and then stop (when unpressing the button)\n\n10 Left - hold (args: 3329, 0) and then release (arg: 3742)\n\nRight - hold (args: 3328, 0) and then release (arg: 2696)\n\n\n\n\n1 toggle\n2 step_with_on_off\n3 step\n4 press\n5 release\n6 move_to_level_with_on_off\n7 move_with_on_off\n8 move\n9 stop\n10 hold\n"},{"id":"301221b9.b1bf8e","type":"api-call-service","z":"7503cb14.1c6134","name":"Turn ON Light","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.lab_color_bulb","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":600,"y":1040,"wires":[["77e09144.04c91"]]},{"id":"e842f2b4.64dd8","type":"comment","z":"7503cb14.1c6134","name":"Remote for lab color bulb","info":"","x":330,"y":420,"wires":[]},{"id":"77e09144.04c91","type":"api-current-state","z":"7503cb14.1c6134","name":"Light State","server":"3ed2660b.f6d8ba","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.lab_color_bulb","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":790,"y":1040,"wires":[["6b744a99.331bb4"]]},{"id":"754568a7.1ee2d8","type":"api-call-service","z":"7503cb14.1c6134","name":"Light Brightness","server":"3ed2660b.f6d8ba","version":1,"debugenabled":false,"service_domain":"","service":"","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1200,"y":1040,"wires":[[]],"info":"{\"brightness\": {{data.attributes.brightness}}}"},{"id":"3ed2660b.f6d8ba","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

// Create an array of arrays
rgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181]];

// Create a variable to hold the array being sent out
let result = rgbPairs[0] < - that is an array.

Then maybe you'll need a string type representation of that array?
let result = rgbPairs[0].join(",") That makes it to be string

But in the end you say again you need an array

So I'm a bit lost.

Hmm. I just tried making an array of strings, but the output comes out as string and not very useful. I should change the string back into an array in this situation.

Considering the code below, how do you think it should look like in order to have 2 things?

  1. cycle through the results instead of randomly choosing what to output. I tried the for loop but it cycles in one go.
  2. output an array instead of a string as it is now
// Create an array of strings
rgbPairs = ["234, 182, 62", "239, 210, 118", "241, 224, 181"];

//make one random integer which is between 0 and count of currentSet elements
let random = Math.floor(Math.random() * rgbPairs.length);

//get an array (3 elements) from currentSet which is at the index of just generated random
let result = rgbPairs[random];
msg.data.attributes.rgb_color = result;

msg.payload = {
 domain: "light",
 service: "turn_on",
 data: {
 entity_id: "light.lab_color_bulb",
 rgb_color: msg.data.attributes.rgb_color
 }
};

return msg;

overall should work with slight changes still needed...

It still outputs as a string and not as an array :confused:

I will add some printscreens

First picture is how it outputs now. Not good. String
Second picture is how it should output. Good. Array

1

That's because you are defined rgbPairs as array of strings

I know. This is also what I was saying. But if I change it back to array, then I don't know how to process it.

I believe it has something to do with nested for loops. I am trying to figure out now

If it must be an array (3 elements, all numbers, representing rgb values) you cant give any other type of data.

If you'll do:

then that requirement is covered.

Now for processing. What is missing?

I thought about simplifying the code as much as possible. So imagine I only had one value. For example for brightness, I have this code that adds 15 points on each "bang" and limits the value between 0 and 254 and outputs the result

// add actual brightness value
var brightness =  msg.data.attributes.brightness;

brightness = brightness + 15;
 

if(brightness >= 255){
   brightness = 254;
    }
        
if(brightness < 0){
   brightness = 0;
	} 
    
msg.data.attributes.brightness = brightness

return msg;

Now getting back to the array for rgb_color, I am trying to replicate the code above where we would limit the array index to 3, and on each "bang" there will be the current index + 1.

The result will be added as seen below to result

msg.payload = {
 domain: "light",
 service: "turn_on",
 data: {
 entity_id: "light.lab_color_bulb",
 rgb_color:result
 }
};
return msg;