Function to go through an array of arrays?

Simply put, how do you add 1 to an index?

How do you translate this brightness = brightness + 1; to an array where you say I know I had for example rgbPairs[0] last time so make it rgbPairs[1] now and on the second "bang" make it rgbPairs[2]? Then restart from rgbPairs[0]?

As for brightness case you have incoming data and you can take it as base value, change it and outgoing message can have that changed value.

Do you have incoming value for that rgb_color? If so, which kind of data it is? Is it array with 3 values?

Yes. That is correct.

This would be a simple code where the rgb_color data would just pass through

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

return msg;

Now I have this, but as it is, it will reset to 0 whenever new info is received. I was thinking for loops but from my understanding, for loops are going to give me all the available subarrays in one go.

And it also does not limit the index from 0 to 2.

I did not test the code below yet

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

var arrayIndex = 0;

let result = rgbPairs[arrayIndex];

arrayIndex = arrayIndex + 1;

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

That index must be managed to be in array boundaries and must be stored in context.

1 Like

OMG!!!!

I really have to learn how to use context.set
Thank you so much!!

Tomorrow I will try to understand what is going on. There are lots of things to learn from this.

Thank you!!

Ok. I believe I got it. This is how the code looks like for when I want to go backwards on the array. I put a flow.get and flow.set instead of context.set and get so that the 2 function flows can communicate.

Thank you so so much for all your help!!!


// Add an array of arrays
rgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181], [241, 236, 206], [245, 250, 246], 
            [231, 136, 52], [228, 145, 175], [231, 190, 220], [213, 228, 75], [235, 246, 252], 
            [230, 115, 70], [217, 51, 123], [201, 132, 187], [168, 214, 43], [220, 240, 247],
            [218, 93, 65], [220, 74, 49], [143, 38, 133], [75, 65, 138], [108, 131, 185]];

// define variable that holds current set (array index) (if doesn't exist, make it the lenght of the array)     
let rgbindex = flow.get('rgbindex')
if(!rgbindex){
    rgbindex = rgbPairs.length 
}

// add 1 to the index
rgbindex ++
// if index is > then the lenght of the array, then make index = 1
if(rgbindex > rgbPairs.length){
    rgbindex = 1
}

// update the current set with the new value of the index
flow.set('rgbindex',rgbindex);

// substract 1 from index so it is from 0 to index-1
let result = rgbPairs[rgbindex -1];

// create payload to include the new rgb color
msg.payload = {
 domain: "light",
 service: "turn_on",
 data: {
 entity_id: "light.lab_color_bulb",
 rgb_color: result
 }
};
return msg;
[{"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":[["9c611585.784688"]]},{"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":[["77e09144.04c91"]]},{"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":[["77c075dc.9fe4dc","c0678e0b.382d6"]]},{"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":"c0678e0b.382d6","type":"function","z":"7503cb14.1c6134","name":"Color Switch Left","func":"\n// Add an array of arrays\nrgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181], [241, 236, 206], [245, 250, 246], \n            [231, 136, 52], [228, 145, 175], [231, 190, 220], [213, 228, 75], [235, 246, 252], \n            [230, 115, 70], [217, 51, 123], [201, 132, 187], [168, 214, 43], [220, 240, 247],\n            [218, 93, 65], [220, 74, 49], [143, 38, 133], [75, 65, 138], [108, 131, 185]];\n\n// define variable that holds current set (array index) (if doesn't exist, make it the lenght of the array)     \nlet rgbindex = flow.get('rgbindex')\nif(!rgbindex){\n    rgbindex = rgbPairs.length \n}\n\n// substract 1 from the index\nrgbindex --\n// if index is < then 1, then make the lenght of the array\nif(rgbindex < 1){\n    rgbindex = rgbPairs.length\n}\n\n// update the current set with the new value of the index\nflow.set('rgbindex',rgbindex);\n\n// substract 1 from index so it is from 0 to index-1\nlet result = rgbPairs[rgbindex -1];\n\n// create payload to include the new rgb color\nmsg.payload = {\n domain: \"light\",\n service: \"turn_on\",\n data: {\n entity_id: \"light.lab_color_bulb\",\n rgb_color: result\n }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":1000,"wires":[["754568a7.1ee2d8","8e4932b4.e3656"]],"info":"// Create an array of arrays\nrgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181]];\n\nvar arrayIndex = 0;\n\nlet result = rgbPairs[arrayIndex];\n\narrayIndex = arrayIndex + 1;\nprocessedArrayIndex = arrayIndex;\n\nmsg.payload = {\n domain: \"light\",\n service: \"turn_on\",\n data: {\n entity_id: \"light.lab_color_bulb\",\n rgb_color:result\n }\n};\nreturn msg;"},{"id":"290bf86c.154fc8","type":"function","z":"7503cb14.1c6134","name":"Color Switch Right","func":"\n// Add an array of arrays\nrgbPairs = [[234, 182, 62], [239, 210, 118], [241, 224, 181], [241, 236, 206], [245, 250, 246], \n            [231, 136, 52], [228, 145, 175], [231, 190, 220], [213, 228, 75], [235, 246, 252], \n            [230, 115, 70], [217, 51, 123], [201, 132, 187], [168, 214, 43], [220, 240, 247],\n            [218, 93, 65], [220, 74, 49], [143, 38, 133], [75, 65, 138], [108, 131, 185]];\n\n// define variable that holds current set (array index) (if doesn't exist, make it the lenght of the array)     \nlet rgbindex = flow.get('rgbindex')\nif(!rgbindex){\n    rgbindex = rgbPairs.length \n}\n\n// add 1 to the index\nrgbindex ++\n// if index is > then the lenght of the array, then make index = 1\nif(rgbindex > rgbPairs.length){\n    rgbindex = 1\n}\n\n// update the current set with the new value of the index\nflow.set('rgbindex',rgbindex);\n\n// substract 1 from index so it is from 0 to index-1\nlet result = rgbPairs[rgbindex -1];\n\n// create payload to include the new rgb color\nmsg.payload = {\n domain: \"light\",\n service: \"turn_on\",\n data: {\n entity_id: \"light.lab_color_bulb\",\n rgb_color: result\n }\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":990,"y":1160,"wires":[["754568a7.1ee2d8","8e4932b4.e3656"]],"info":"min brightness 0\nmax brightness 255\n\nmin color_temp 0 \nmax color_temp 250"},{"id":"8e4932b4.e3656","type":"debug","z":"7503cb14.1c6134","name":"complete","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1420,"y":1040,"wires":[]},{"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}]

There is no reason to make multiple copies of same function.
To use same function for going forwards or backwards, the incoming payload should carry some kind of property which tells to even add 1 to index or subtract 1 from index. Also the "keep index in array boundaries" functionality needs the to be smarter a bit.

As the payload is coming from custom node I don't know what it is. But you can put debug node just before the function node and see what is the payload, is there anything to make such decisions or it needs to be added somehow to the incoming payload.

I will try that for sure. Thank you very very much for all your help!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.