Dynamically retrieve global values based on msg keys

Hi all...
Node-Red is supposed to be all about doing the 'programing' visually, but I'm stuck on some function coding.
I'm used to processing data with Excel, so this is the the mind-set/tool-set I'm coming from. I also have very limited coding knowledge. I guess the learning curve is a bit to steep for me:

I am trying to develop linear flows that read and write from the global context dynamically.

The objective is to complement a message with the related values (i.e.: msg.domain, msg.area, msg.effect), based on a given variable in the message (i.e.: msg.reference).

Once a flow runs, it should be able to query the global context automatically and update the message. Then the message could be forwarded and directed by switches based on the newly updated values.

In Excel I would use a =vlookup function and extract the data from one table to another, but given that I started using Node-Red one week ago, I have no clue what to do.

From my 'googling', I was lead to believe that this would work, but I got nothing, not even an error message :thinking:

var data = global.get("sample");

var dataResults = data.find(key => msg.reference === key.reference)

if(lookupResults) {
    msg.domain = lookupResults.domain;
    msg.area = lookupResults.area;
    msg.effect = lookupResults.effect;
}

return msg;

Any help is much appreciated,

Cheers!

Sample flow:

[{"id":"9a594d4859060636","type":"inject","z":"8598d2c602e77545","name":"Inject Variables","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Reference\":\"Kitchen Buttons press_1\",\"payload\":\"change_value\",\"domain\":\"light\",\"area\":\"Kitchen\",\"device\":\"Buttons\",\"entity\":\"Kitchen Buttons Doorway\",\"mode.profile\":\"0\",\"mode.status\":\"0\",\"mode.lighting\":\"0\",\"mode.heating\":\"0\",\"mode.override\":\"0\",\"data\":\"press_1\",\"state\":\"On/Off\",\"attributes\":\"0\",\"effect\":\"Toggle\",\"scene\":\"0\",\"scene.night\":\"0\",\"scene.night_alt\":\"0\",\"guidance.destination\":\"light.kitchen_light_group_minimal\",\"number\":\"0\"},{\"Reference\":\"Kitchen Buttons press_2\",\"payload\":\"change_value\",\"domain\":\"light\",\"area\":\"Kitchen\",\"device\":\"Buttons\",\"entity\":\"Kitchen Buttons Doorway\",\"mode.profile\":\"0\",\"mode.status\":\"0\",\"mode.lighting\":\"0\",\"mode.heating\":\"0\",\"mode.override\":\"0\",\"data\":\"press_2\",\"state\":\"On/Off\",\"attributes\":\"0\",\"effect\":\"Toggle\",\"scene\":\"0\",\"scene.night\":\"0\",\"scene.night_alt\":\"0\",\"guidance.destination\":\"light.fireplace_light_group_ceiling\",\"number\":\"1\"},{\"Reference\":\"Kitchen Buttons press_3\",\"payload\":\"change_value\",\"domain\":\"light\",\"area\":\"Kitchen\",\"device\":\"Buttons\",\"entity\":\"Kitchen Buttons Doorway\",\"mode.profile\":\"0\",\"mode.status\":\"0\",\"mode.lighting\":\"0\",\"mode.heating\":\"0\",\"mode.override\":\"0\",\"data\":\"press_3\",\"state\":\"On/Off\",\"attributes\":\"0\",\"effect\":\"Toggle\",\"scene\":\"0\",\"scene.night\":\"0\",\"scene.night_alt\":\"0\",\"guidance.destination\":\"light.fireplace_light_group_ambient\",\"number\":\"2\"},{\"Reference\":\"Kitchen Buttons press_4\",\"payload\":\"change_value\",\"domain\":\"light\",\"area\":\"Kitchen\",\"device\":\"Buttons\",\"entity\":\"Kitchen Buttons Doorway\",\"mode.profile\":\"0\",\"mode.status\":\"0\",\"mode.lighting\":\"0\",\"mode.heating\":\"0\",\"mode.override\":\"0\",\"data\":\"press_4\",\"state\":\"On/Off\",\"attributes\":\"0\",\"effect\":\"Toggle\",\"scene\":\"0\",\"scene.night\":\"0\",\"scene.night_alt\":\"0\",\"guidance.destination\":\"light.kitchen_light_group_all\",\"number\":\"3\"},{\"Reference\":\"Kitchen Shortcut on\",\"payload\":\"change_value\",\"domain\":\"switch\",\"area\":\"Kitchen\",\"device\":\"Shortcut\",\"entity\":\"Kitchen Shortcut Counter Grinder\",\"mode.profile\":\"0\",\"mode.status\":\"0\",\"mode.lighting\":\"0\",\"mode.heating\":\"0\",\"mode.override\":\"0\",\"data\":\"on\",\"state\":\"On/Off\",\"attributes\":\"0\",\"effect\":\"Toggle\",\"scene\":\"0\",\"scene.night\":\"0\",\"scene.night_alt\":\"0\",\"guidance.destination\":\"switch.kitchen_plug_grinder\",\"number\":\"4\"}]","payloadType":"json","x":840,"y":480,"wires":[["96944296d03915a7"]]},{"id":"c6f2b7a2bc7f0328","type":"debug","z":"8598d2c602e77545","name":"Get results to process further","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1375,"y":615,"wires":[]},{"id":"96944296d03915a7","type":"function","z":"8598d2c602e77545","name":"to create Global Database - \"Sample\"","func":"global.set(\"sample\", msg.payload)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1095,"y":480,"wires":[[]]},{"id":"c1ae37b19761cdf3","type":"inject","z":"8598d2c602e77545","name":"Run Example Flow","props":[{"p":"payload"},{"p":"reference","v":"Kitchen Buttons press_3","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":830,"y":580,"wires":[["47b81534fe9ae4cd"]]},{"id":"47b81534fe9ae4cd","type":"function","z":"8598d2c602e77545","name":"with a function","func":"var lookup = global.get(\"sample\");\n\nvar lookupMatch = lookup.find(key => msg.reference === key.reference)\n\nif(lookupMatch) {\n    msg.domain = lookupMatch.domain;\n    msg.area = lookupMatch.area;\n    msg.effect = lookupMatch.effect;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1095,"y":615,"wires":[["c6f2b7a2bc7f0328"]]},{"id":"25448fe5525af38a","type":"comment","z":"8598d2c602e77545","name":"Step 1 - Setup problem","info":"","x":740,"y":445,"wires":[]},{"id":"40cf31f175d28ae8","type":"comment","z":"8598d2c602e77545","name":"Step 2","info":"","x":690,"y":545,"wires":[]},{"id":"0b1728e22ec80fd9","type":"debug","z":"8598d2c602e77545","name":"Actual desired results","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1115,"y":775,"wires":[]},{"id":"a16d908342c3f79b","type":"comment","z":"8598d2c602e77545","name":"Step 3","info":"","x":690,"y":730,"wires":[]},{"id":"8717ad8b903efab5","type":"change","z":"8598d2c602e77545","name":"Magic","rules":[{"t":"set","p":"domain","pt":"msg","to":"light","tot":"str"},{"t":"set","p":"area","pt":"msg","to":"Kitchen","tot":"str"},{"t":"set","p":"effect","pt":"msg","to":"Toggle","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":800,"wires":[["0b1728e22ec80fd9"]]},{"id":"6ffff2f3d5af1c49","type":"inject","z":"8598d2c602e77545","name":"Example Results","props":[{"p":"payload"},{"p":"reference","v":"Kitchen Buttons press_3","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":840,"y":765,"wires":[["8717ad8b903efab5"]]},{"id":"227beeca6cca8cf4","type":"change","z":"8598d2c602e77545","name":"change node?","rules":[{"t":"set","p":"domain","pt":"msg","to":"light","tot":"str"},{"t":"set","p":"area","pt":"msg","to":"Kitchen","tot":"str"},{"t":"set","p":"effect","pt":"msg","to":"Toggle","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1095,"y":685,"wires":[[]]},{"id":"c0ea64250f7ce28e","type":"comment","z":"8598d2c602e77545","name":"or maybe a","info":"","x":1075,"y":650,"wires":[]},{"id":"940847cb2bb37149","type":"comment","z":"8598d2c602e77545","name":"Lookup data based on input","info":"","x":1105,"y":580,"wires":[]},{"id":"ae68692738dea59a","type":"switch","z":"8598d2c602e77545","name":"With a switch, for example","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1585,"y":650,"wires":[[]]},{"id":"6ad8294593ac6a19","type":"comment","z":"8598d2c602e77545","name":"Im not even sure why this isn't throwing an error?","info":"","x":1645,"y":580,"wires":[]}]

You've got a small but relevant typo in this line.

Reference is defined w/ a capital letter.

1 Like

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