NR Conundrum sett flow context changes global context

I'm trying to understand how it could be that after reading global context into a flow context, checking for the existence of a value ("on") in the flow context in order to determine what groups I need to serve, that action in itself changes the global context. I've asked a friend and we are getting no where.

Try the code I hope it is exemplary as a debugging routine.....I can't see where it would go as it does.....

This will allow you to build the global, array and fish for data...

[{"id":"73542c331aa93af6","type":"function","z":"973ac71228b5a79f","name":"Adaptive Lighting Execution(selection)","func":"/** get actual brigness and color levels */\nvar brightness = global.get('light[0].brightness'); /** not stored in HA only NR */\nvar color_temp = global.get('light[0].color_temp'); /** not stored in HA only NR */\nflow.set('brightness', brightness); /**debug */\nflow.set('color_temp', color_temp); /** debug */\n\n\n/** automations woonkmr (room[1])] */\n/** automations badkamer (room[2])] */\n/** automations slaapkamer (room[3])] */\n/** automations werkkamer (room[4])] */\n/** automations logeerkamer (room[5])] */\nvar room_data = global.get(\"house[0].room[5]\");\nflow.set(\"room_data\", room_data);\nif (room_data === null) {\n    node.warn(\"room data leeg\");\n} else {\n\n    var max_bright = parseInt(room_data.max_bright);/**max applicable bright level*/\n    var lightgroup = room_data.lightgroup; /** get lighting groups of room6 */\n    var nummer = Number(lightgroup.length); /** equals number of lighting groups */\n\n    for (let index = 0; index < nummer; index++) { /** run through array */\n\n        if (lightgroup[index].adaptive = \"on\") { /** if LG is set to adaptive, save ent_id */\n            flow.set(\"set_group[\" + index + \"]\", lightgroup[index].entity_id); /** array with all groups to serve */\n        }\n    }\n\n    var entity_to_set = flow.get(\"set_group\");\n    if (brightness > max_bright) {\n        var set_brightness = max_bright\n    } else set_brightness = brightness;\n\n\n    let sep = \"\";\n    for (let x of entity_to_set) {\n        sep += x;\n        var msg1 = { topic: entity_to_set, payload: set_brightness };\n        flow.set(\"msg5_1\", msg1);\n        var msg2 = { topic: entity_to_set, payload: color_temp };\n        flow.set(\"msg5_2\", msg2);\n    }\n    node.send([[msg1, msg2]]);\n    msg1 = null;\n    msg2 = null;\n}    \n/** automations greenroom (room[6])] */\nvar room_data = global.get(\"house[0].room[6]\");\nflow.set(\"room_data\", room_data);\nif (room_data === null){\n    node.warn(\"room data leeg\");\n}else {\n    \n    var max_bright = parseInt(room_data.max_bright);/**max applicable bright level*/\n    var lightgroup = room_data.lightgroup; /** get lighting groups of room6 */\n    var nummer = Number(lightgroup.length); /** equals number of lighting groups */\n    \n        for (let index = 0; index < nummer; index++) { /** run through array */\n\n            if (lightgroup[index].adaptive == \"on\") { /** if LG is set to adaptive, save ent_id */\n               flow.set(\"set_group[\" + index + \"]\", lightgroup[index].entity_id); /** array with all groups to serve */\n            }\n    }\n    \n    var entity_to_set = flow.get(\"set_group\");\n        if (brightness > max_bright) {\n         var set_brightness = max_bright\n         } else set_brightness = brightness;\n    \n\n    let sep = \"\";\n    for (let x of entity_to_set) {\n        sep += x;\n        var msg1 = { topic: entity_to_set, payload : set_brightness};\n        flow.set(\"msg6_1\", msg1);\n        var msg2 = { topic: entity_to_set, payload: color_temp};\n        flow.set(\"msg6_2\", msg2);\n    }\n    node.send([[msg1, msg2]]);\n    msg1 = null;\n    msg2 = null;\n}    \nvar newMsg = { payload: null};\nreturn newMsg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":990,"y":320,"wires":[["a14529924219d322"]]},{"id":"9ab1fe67824a661e","type":"debug","z":"973ac71228b5a79f","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":200,"wires":[]},{"id":"476239f30574de72","type":"inject","z":"973ac71228b5a79f","name":"This is magic","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":670,"y":320,"wires":[["73542c331aa93af6"]]},{"id":"43f81f7af4c4ab4c","type":"inject","z":"973ac71228b5a79f","name":"0. Basics, create global array","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":120,"wires":[["2075fb99476e283e"]]},{"id":"2075fb99476e283e","type":"change","z":"973ac71228b5a79f","name":"Set Area Names","rules":[{"t":"set","p":"house[0].name","pt":"global","to":"hellingstraat","tot":"str"},{"t":"set","p":"house[0].room[0].name","pt":"global","to":"entree","tot":"str"},{"t":"set","p":"house[0].room[1].name","pt":"global","to":"woonkmr","tot":"str"},{"t":"set","p":"house[0].room[2].name","pt":"global","to":"badkmr","tot":"str"},{"t":"set","p":"house[0].room[3].name","pt":"global","to":"slpkmr","tot":"str"},{"t":"set","p":"house[0].room[4].name","pt":"global","to":"werkkmr","tot":"str"},{"t":"set","p":"house[0].room[5].name","pt":"global","to":"logeerkmr","tot":"str"},{"t":"set","p":"house[0].room[5].max_bright","pt":"global","to":"200.0","tot":"str"},{"t":"set","p":"house[0].room[6].name","pt":"global","to":"greenrm","tot":"str"},{"t":"set","p":"house[0].room[6].max_bright","pt":"global","to":"196.0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":120,"wires":[[]]},{"id":"9f45eb1249979b4f","type":"change","z":"973ac71228b5a79f","name":"Setup adaptive groups (entity_id for recog)","rules":[{"t":"set","p":"house[0].room[5].lightgroup[0].entity_id","pt":"global","to":"light.group_logeerkmr","tot":"str"},{"t":"set","p":"house[0].room[5].lightgroup[0].adaptive","pt":"global","to":"on","tot":"str"},{"t":"set","p":"house[0].room[5].lightgroup[1].adaptive","pt":"global","to":"off","tot":"str"},{"t":"set","p":"house[0].room[5].lightgroup[1].entity_id","pt":"global","to":"light.group_tvlogeer","tot":"str"},{"t":"set","p":"house[0].room[6].lightgroup[0].entity_id","pt":"global","to":"light.group_greenrm","tot":"str"},{"t":"set","p":"house[0].room[6].lightgroup[0].adaptive","pt":"global","to":"on","tot":"str"},{"t":"set","p":"house[0].room[6].lightgroup[1].entity_id","pt":"global","to":"light.group_tvbas","tot":"str"},{"t":"set","p":"house[0].room[6].lightgroup[1].adaptive","pt":"global","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":240,"wires":[["10540fc2a95c1f53"]]},{"id":"2f8eb8ae8beffd00","type":"inject","z":"973ac71228b5a79f","name":"2. Reset those globals","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":700,"y":240,"wires":[["9f45eb1249979b4f"]]},{"id":"e3a566f603015b8e","type":"inject","z":"973ac71228b5a79f","name":"3. Show Result in Flow context pane","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":740,"y":280,"wires":[["137f3fa22e189897"]]},{"id":"137f3fa22e189897","type":"change","z":"973ac71228b5a79f","name":"Get Globals into Flow context for easy viewing","rules":[{"t":"set","p":"test_adapt_5_0","pt":"flow","to":"house[0].room[5].lightgroup[0].adaptive","tot":"global"},{"t":"set","p":"test_adapt_5_1","pt":"flow","to":"house[0].room[5].lightgroup[1].adaptive","tot":"global"},{"t":"set","p":"test_adapt_6_0","pt":"flow","to":"house[0].room[6].lightgroup[0].adaptive","tot":"global"},{"t":"set","p":"test_adapt_6_1","pt":"flow","to":"house[0].room[6].lightgroup[1].adaptive","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":280,"wires":[["4b6b2d2f563f4493"]]},{"id":"40b8bf2374d2f14d","type":"inject","z":"973ac71228b5a79f","name":"1. Empty Flow Var","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":680,"y":200,"wires":[["94418f116645a299"]]},{"id":"94418f116645a299","type":"change","z":"973ac71228b5a79f","name":"Delete Flow Context","rules":[{"t":"delete","p":"lightgroup","pt":"flow"},{"t":"delete","p":"max_brightness","pt":"flow"},{"t":"delete","p":"nummer","pt":"flow"},{"t":"delete","p":"room_data","pt":"flow"},{"t":"delete","p":"stap","pt":"flow"},{"t":"delete","p":"set_group","pt":"flow"},{"t":"delete","p":"brightness","pt":"flow"},{"t":"delete","p":"color_temp","pt":"flow"},{"t":"delete","p":"test_adapt_5_0","pt":"flow"},{"t":"delete","p":"test_adapt_5_1","pt":"flow"},{"t":"delete","p":"test_adapt_6_0","pt":"flow"},{"t":"delete","p":"test_adapt_6_1","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1130,"y":200,"wires":[["9ab1fe67824a661e"]]},{"id":"a976231e65855035","type":"inject","z":"973ac71228b5a79f","name":"3. SLook the Global changed too(check it!)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":760,"y":360,"wires":[["3c06ef5f6ebffada"]]},{"id":"3c06ef5f6ebffada","type":"change","z":"973ac71228b5a79f","name":"Show changed globals in flow context","rules":[{"t":"set","p":"test_adapt_5_0","pt":"flow","to":"house[0].room[5].lightgroup[0].adaptive","tot":"global"},{"t":"set","p":"test_adapt_5_1","pt":"flow","to":"house[0].room[5].lightgroup[1].adaptive","tot":"global"},{"t":"set","p":"test_adapt_6_0","pt":"flow","to":"house[0].room[6].lightgroup[0].adaptive","tot":"global"},{"t":"set","p":"test_adapt_6_1","pt":"flow","to":"house[0].room[6].lightgroup[1].adaptive","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":1110,"y":360,"wires":[["6f010e8c87ec456d"]]},{"id":"10540fc2a95c1f53","type":"debug","z":"973ac71228b5a79f","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":240,"wires":[]},{"id":"4b6b2d2f563f4493","type":"debug","z":"973ac71228b5a79f","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":280,"wires":[]},{"id":"a14529924219d322","type":"debug","z":"973ac71228b5a79f","name":"debug 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":320,"wires":[]},{"id":"6f010e8c87ec456d","type":"debug","z":"973ac71228b5a79f","name":"debug 5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1340,"y":360,"wires":[]},{"id":"7988833ea484bf48","type":"comment","z":"973ac71228b5a79f","name":"Do this once","info":"","x":650,"y":80,"wires":[]},{"id":"8e86570abfed463b","type":"comment","z":"973ac71228b5a79f","name":"Repeat at will","info":"","x":650,"y":160,"wires":[]}]

Good night y'all :slight_smile:

Javascript store object by reference.
So setting flow to a variable sets the variable to reference the same memory. If you then alter either the other will reference the new value.

you will need to clone it using RED.util.cloneMessage(flow.get("string"))

1 Like

Well, i tried doing that object.assign and that does work, not sure if this is the way but hey

var room_data = global.get("house[0].room[5]");
Object.assign(room_data, global.get("house[0].room[5]") ); 

I AM still wondering why a simple

for (let index = 0; index < nummer; index++) { /** run through array */
        if (lightgroup[index].adaptive == "on") { /** if LG is set to adaptive, save ent_id */
            flow.set("set_group[" + index + "]", lightgroup[index].entity_id); /** array with all groups to serve */
        }
    }

and the if (lightgroup[index].adaptive == "on")part of it specifically actual changes the values....

single equal a = b, set value of b to a.
double equal a==b (false), a==a (true), 10 == twelve (false), 12 == twelve (true), checks (with string conversion)
triple equal  a === a(false) is a NOT statement.

Which one checks AND changes the values to check? Where does that happen?

this is the array before the above code runs (value for adapt are on, off,

const lightgroup = [{"entity_id":"light.group_greenrm","adaptive":"on"},{"entity_id":"light.group_tvbas","adaptive":"off"}]

but aftwards the off changed to on

const lightgroup = [{"entity_id":"light.group_greenrm","adaptive":"on"},{"entity_id":"light.group_tvbas","adaptive":"on"}]

So where does checking if equal, make equal?