[SOLVED] Newbe struggling with JSON Global Arrays

Dear All,

I am on a very steep learning curve here. I am experienced in C and .NET (the VB kind), however have now ventured into the world of micro-controllers and over the last few weeks have had to learn:

  • Linux/Python
  • Arduino IDE/CSharp (+ Libraries)
  • ESP8266
  • MQTT
  • NodeRed

And now JSCRIPT! :confounded:

I have managed to overcome most hurdles (thank you communities/google/YouTube/etc) but am really struggling with this bit.

Please see the flow I am struggling with below (and attached flow).

I have managed to get the selection list populating correctly in [F:Produce list of Colours], and it is this I am trying to do a better way. I want to setup a Global (or could be Flow) scope JSON array, which is what I have been attempting to do in the two Config nodes:

image

The end game being to actually define these values within a config file that will be read at startup. However that is another thing to learn about another day. Now I just want to be able to:

  1. Initialize the Global/Flow JSON array
  2. Access it from the [F:Produce Global List of Colours] function to produce a msg={"options":colourOptions} message for feeding the Dashboard List box.

Once I can do this, I will then be able to start randomizing the selected list item and sending this out to MQTT.

image

Very much appreciate any support here, and before anyone says RTFM/Google is your friend, I have and know! :grin: :beers:

BR,

Stephen

WORKING PART:

[{"id":"1cec7e4b.bebfd2","type":"mqtt in","z":"ed360b01.bc40b8","name":"","topic":"home/office/randomColour","qos":"1","datatype":"auto","broker":"cf483460.015248","x":190,"y":160,"wires":[["55d0e56d.c0702c"]]},{"id":"cc6bdc79.8b812","type":"inject","z":"ed360b01.bc40b8","name":"Force MQTT to initialise RandomColour ","topic":"","payload":"true","payloadType":"str","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":1180,"y":60,"wires":[["590c62c0.33ec2c","809ccf7f.2b889"]]},{"id":"45bcb1.db45835","type":"ui_button","z":"ed360b01.bc40b8","name":"","group":"2f7f31cd.b5f6fe","order":2,"width":"0","height":"0","passthru":false,"label":"{{payload}}","tooltip":"","color":"","bgcolor":"{{colour}}","icon":"","payload":"randomColour","payloadType":"flow","topic":"","x":890,"y":160,"wires":[["58be382e.fb6c78"]]},{"id":"f91f5b89.0a40b8","type":"change","z":"ed360b01.bc40b8","name":"Random Colour Off State","rules":[{"t":"set","p":"colour","pt":"msg","to":"#C0C0C0","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Random Colour On","tot":"str"},{"t":"set","p":"randomColour","pt":"flow","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":180,"wires":[["45bcb1.db45835"]]},{"id":"1a44efc7.22a3e","type":"change","z":"ed360b01.bc40b8","name":"Random Colour On State","rules":[{"t":"set","p":"colour","pt":"msg","to":"#FFAAAA","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"Random Colour Off","tot":"str"},{"t":"set","p":"randomColour","pt":"flow","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":690,"y":120,"wires":[["45bcb1.db45835"]]},{"id":"55d0e56d.c0702c","type":"switch","z":"ed360b01.bc40b8","name":"Set Switch Status","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"str"},{"t":"eq","v":"false","vt":"str"},{"t":"else"}],"checkall":"false","repair":false,"outputs":3,"x":450,"y":160,"wires":[["1a44efc7.22a3e"],["f91f5b89.0a40b8"],[]]},{"id":"58be382e.fb6c78","type":"switch","z":"ed360b01.bc40b8","name":"Switch State","property":"randomColour","propertyType":"flow","rules":[{"t":"eq","v":"true","vt":"str"},{"t":"eq","v":"false","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1060,"y":160,"wires":[["d72a11b4.b26a9"],["84072a17.a281b8"]]},{"id":"d72a11b4.b26a9","type":"change","z":"ed360b01.bc40b8","name":"Send false msg","rules":[{"t":"set","p":"payload","pt":"msg","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1260,"y":120,"wires":[["590c62c0.33ec2c","38b1d1cf.d6988e"]]},{"id":"84072a17.a281b8","type":"change","z":"ed360b01.bc40b8","name":"Send true msg","rules":[{"t":"set","p":"payload","pt":"msg","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1260,"y":200,"wires":[["590c62c0.33ec2c","809ccf7f.2b889"]]},{"id":"6ef0bc7c.4c6c14","type":"comment","z":"ed360b01.bc40b8","name":"Manage LED Random Colour","info":"","x":200,"y":100,"wires":[]},{"id":"590c62c0.33ec2c","type":"mqtt out","z":"ed360b01.bc40b8","name":"","topic":"home/office/randomColour","qos":"1","retain":"true","broker":"cf483460.015248","x":1820,"y":160,"wires":[]},{"id":"d03f768f.81fa08","type":"ui_dropdown","z":"ed360b01.bc40b8","name":"Office Colour Scheme","label":"Colour Scheme","tooltip":"Select colour scheme","place":"","group":"2f7f31cd.b5f6fe","order":1,"width":0,"height":0,"passthru":false,"options":[],"payload":"","topic":"","x":1800,"y":360,"wires":[["c672e23d.d9fb2"]]},{"id":"d4fab2f1.e61af","type":"function","z":"ed360b01.bc40b8","name":"Produce list of Colours","func":"var colourOptions = [   {\"Warm\":\"red\"},\n                        {\"Cool\":\"blue\"},\n                        {\"Earth\":\"green\"},\n                        {\"Spring\":\"yellow\"},\n                        {\"Summer\":\"pink\"},\n                        {\"Autumn\":\"purple\"},\n                        {\"Winter\":\"white\"},\n                        {\"GB\":\"red-blue\"},\n                        {\"Ireland\":\"green-yellow\"},\n                        {\"Ocean\":\"blue-green\"},\n                        {\"Rainbow\":\"rainbow\"},\n                        {\"Pastel\":\"pastel\"}];\n\nmsg={\"options\":colourOptions};\nreturn msg;","outputs":1,"noerr":0,"x":1520,"y":400,"wires":[["d03f768f.81fa08"]]},{"id":"9d9e8fa5.41214","type":"inject","z":"ed360b01.bc40b8","name":"Force the Colour Array to Load","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":1250,"y":400,"wires":[["d4fab2f1.e61af"]]},{"id":"c672e23d.d9fb2","type":"mqtt out","z":"ed360b01.bc40b8","name":"","topic":"home/office/ledColour","qos":"2","retain":"true","broker":"cf483460.015248","x":2040,"y":360,"wires":[]},{"id":"2bb910f0.5c364","type":"mqtt in","z":"ed360b01.bc40b8","name":"","topic":"home/office/ledColour","qos":"1","datatype":"auto","broker":"cf483460.015248","x":180,"y":360,"wires":[["d03f768f.81fa08"]]},{"id":"b8a3e55f.faad48","type":"comment","z":"ed360b01.bc40b8","name":"Manage Loading Colour List","info":"","x":200,"y":240,"wires":[]},{"id":"809ccf7f.2b889","type":"change","z":"ed360b01.bc40b8","name":"Disable the Node","rules":[{"t":"set","p":"enabled","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1510,"y":300,"wires":[["d03f768f.81fa08"]]},{"id":"38b1d1cf.d6988e","type":"change","z":"ed360b01.bc40b8","name":"Enable the Node","rules":[{"t":"set","p":"enabled","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1510,"y":240,"wires":[["d03f768f.81fa08"]]},{"id":"ba4a427a.f2999","type":"function","z":"ed360b01.bc40b8","name":"Produce Global List of Colours","func":"\nvar colourOptions;\nvar myObj = global.colourPallette1;\n\n    for (var i in myObj) {\n        // colourOptions += myObj.colours[i].desc + \", \";  \n        print (i);\n        \n    }\n\nmsg={\"options\":colourOptions};\nreturn msg;","outputs":1,"noerr":0,"x":750,"y":520,"wires":[["9b387eb1.54905"]]},{"id":"6b7cf263.11766c","type":"config","z":"ed360b01.bc40b8","name":"Setup Global Colour Pallette","properties":[{"p":"colourPallette","pt":"global","to":"{\"colours\":[{\"desc\":\"Warm\",\"col\":\"red\"},{\"desc\":\"Cool\",\"col\":\"blue\"},{\"desc\":\"Earth\",\"col\":\"green\"},{\"desc\":\"Spring\",\"col\":\"yellow\"},{\"desc\":\"Summer\",\"col\":\"pink\"},{\"desc\":\"Autumn\",\"col\":\"purple\"},{\"desc\":\"Winter\",\"col\":\"white\"},{\"desc\":\"GB\",\"col\":\"red-blue\"},{\"desc\":\"Ireland\",\"col\":\"green-yellow\"},{\"desc\":\"Ocean\",\"col\":\"blue-green\"},{\"desc\":\"Rainbow\",\"col\":\"rainbow\"},{\"desc\":\"Pastel\",\"col\":\"pastel\"}]}","tot":"json"}],"active":true,"x":200,"y":520,"wires":[]},{"id":"9b387eb1.54905","type":"ui_dropdown","z":"ed360b01.bc40b8","name":"Office Colour Scheme (JSON)","label":"Colour Scheme (JSON Driven)","tooltip":"Select colour scheme","place":"","group":"2f7f31cd.b5f6fe","order":1,"width":0,"height":0,"passthru":false,"options":[],"payload":"","topic":"","x":1050,"y":520,"wires":[[]]},{"id":"cdc9b1ab.20084","type":"inject","z":"ed360b01.bc40b8","name":"Trigger JSON List Build","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":480,"y":520,"wires":[["ba4a427a.f2999"]]},{"id":"cf483460.015248","type":"mqtt-broker","z":"","name":"piMQTT","broker":"piMQTT","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"piMQTT/status","birthQos":"2","birthPayload":"Online","closeTopic":"piMQTT/status","closeQos":"2","closePayload":"Offline","willTopic":"piMQTT/status","willQos":"1","willPayload":"Disconnected"},{"id":"2f7f31cd.b5f6fe","type":"ui_group","z":"","name":"Office","tab":"bccad758.8b2e98","order":4,"disp":true,"width":"6","collapse":false},{"id":"bccad758.8b2e98","type":"ui_tab","z":"","name":"House","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

You need to surround your flow in 3 backticks in order for the forum software not to modify your flow

1 Like
  1. javascript not JSCRIPT :upside_down_face:
  2. Your flow is not impotable. Please read this thread and edit your prior post.
1 Like

Cheers zenofmd, lets try this one! :slight_smile:

PS: Please ignore the code in the [F:Produce Global List of Colours] as I've been hacking and hacking at it. Nothing is working for me.

Are you talking about the function 'Produce Global List of Colours'? Because theere are some issues with it.

  1. don't use 'print' if you want to debug a function node use node.warn(...) (see https://nodered.org/docs/user-guide/writing-functions#api-reference) so if you wanted to display a variable
var foo = 'bar';
node.warn("foo="+foo);

will do the trick.

also use global.get() and global.set() so instead of
var myObj = global.colourPallette1;
use
var myObj = global.get('colourPallette1');
or better yet you might consider using
var myObj = global.get('colourPallette1')||"does not exist";
to get the global and set it to a value if it doesn't exist (see https://nodered.org/docs/user-guide/writing-functions#storing-data) and to save a global use
global.set('colourPallette1', myObj);

1 Like

Don't struggle, just read infopages of nodes and configure objects and nodes. Basic cases as this one are described in info pages very clearly.

[{"id":"b914b808.ae73f8","type":"config","z":"37e0d918.b92d36","name":"Setup Global Colour Pallette","properties":[{"p":"colours","pt":"global","to":"[{\"Warm\":\"red\"},{\"Cool\":\"blue\"},{\"Earth\":\"green\"},{\"Spring\":\"yellow\"}]","tot":"json"}],"active":true,"x":200,"y":150,"wires":[]},{"id":"f93a60ef.a352e","type":"inject","z":"37e0d918.b92d36","name":"Force the Colour Array to Load","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":210,"y":200,"wires":[["8f37059e.a4c398"]]},{"id":"8f37059e.a4c398","type":"function","z":"37e0d918.b92d36","name":"Produce list of Colours","func":"var colourOptions =global.get('colours')\nnode.warn(colourOptions)\nmsg={\"options\":colourOptions};\nreturn msg;","outputs":1,"noerr":0,"x":310,"y":270,"wires":[["66681021.6434d"]]},{"id":"66681021.6434d","type":"ui_dropdown","z":"37e0d918.b92d36","name":"Office Colour Scheme","label":"Colour Scheme","tooltip":"Select colour scheme","place":"","group":"fb925276.017ec","order":1,"width":0,"height":0,"passthru":false,"options":[],"payload":"","topic":"","x":540,"y":270,"wires":[["c1da6634.c29fe8","170617a2.c51c98"]]},{"id":"170617a2.c51c98","type":"ui_text","z":"37e0d918.b92d36","group":"fb925276.017ec","order":4,"width":0,"height":0,"name":"","label":"selected value","format":"{{msg.payload}}","layout":"row-spread","x":750,"y":310,"wires":[]},{"id":"c1da6634.c29fe8","type":"debug","z":"37e0d918.b92d36","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":720,"y":220,"wires":[]},{"id":"fb925276.017ec","type":"ui_group","z":"","name":"Office","tab":"2d61ed7c.6ea692","order":4,"disp":true,"width":"6","collapse":false},{"id":"2d61ed7c.6ea692","type":"ui_tab","z":"","name":"House","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
1 Like

Thank you both and hotNipi, you are a star!!! :slight_smile:

Its easy when you know how.

I tried this zeno, and can see I missed out the ' around the variable name. Lesson learned! :slight_smile:

1 Like