Using environmental variables (system or user defined)

Environmental variables (enter ‘env’ in a terminal to see which can be used) can be accessed by adding ‘env:require(env)’ to the ‘functionGlobalContext:’ section of the settings.js file. Once entered you can access them in any node that has a text field (a/z) or in a function node.

For example: to use in a text (a/z) field such as an INJECT node. use the format: $(LOGNAME) To use in a FUNCTION use the following:

var env = global.get(‘env’);
msg.payload = env.LOGNAME;

SPECIAL CASES: If you want to use HOSTNAME or declare your own environmental variable, you must make an additional change to the settings.js file. Add the following lines just before the line ‘module.exports = {‘:

process.env.HOSTNAME = require(‘os’).hostname();
process.env.FOO = ‘just another bar’;

Once you have made the changes to the settings.js file, stop/restart node-red to make them avaliable for use. You can now use either as explained above. Examples are in the flows.

[{"id":"2e589c94.441a64","type":"inject","z":"be4e3923.464d7","name":"Test $(LOGNAME)","topic":"","payload":"$(LOGNAME)","payloadType":"str","repeat":"","crontab":"","once":false,"x":170,"y":120,"wires":[["f528fa00.19ef08"]]},{"id":"91bfcd36.2d539","type":"debug","z":"be4e3923.464d7","name":"test 3","active":true,"console":"false","complete":"true","x":670,"y":240,"wires":[]},{"id":"f8eebaf9.5532e8","type":"inject","z":"be4e3923.464d7","name":"Test $(FOO)","topic":"","payload":"$(FOO)","payloadType":"str","repeat":"","crontab":"","once":false,"x":150,"y":240,"wires":[["91bfcd36.2d539"]]},{"id":"efdaef6d.ea98f8","type":"inject","z":"be4e3923.464d7","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":130,"y":300,"wires":[["355647cc.bb5fe8"]]},{"id":"355647cc.bb5fe8","type":"function","z":"be4e3923.464d7","name":"Use $(FOO) in a function","func":"var env = global.get('env');\nmsg.payload = env.FOO;\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":300,"wires":[["c1160b6a.fae14"]]},{"id":"c1160b6a.fae14","type":"debug","z":"be4e3923.464d7","name":"test 4","active":true,"console":"false","complete":"true","x":670,"y":300,"wires":[]},{"id":"1c3579cd.7b607e","type":"inject","z":"be4e3923.464d7","name":"Test $(HOSTNAME)","topic":"","payload":"$(HOSTNAME)","payloadType":"str","repeat":"","crontab":"","once":false,"x":170,"y":180,"wires":[["916ab319.a56118"]]},{"id":"f528fa00.19ef08","type":"debug","z":"be4e3923.464d7","name":"test 1","active":true,"console":"false","complete":"true","x":670,"y":120,"wires":[]},{"id":"916ab319.a56118","type":"debug","z":"be4e3923.464d7","name":"test 2","active":true,"console":"false","complete":"true","x":670,"y":180,"wires":[]},{"id":"765953ba.2a457c","type":"inject","z":"be4e3923.464d7","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":130,"y":360,"wires":[["8394742a.37fd28"]]},{"id":"9570e708.3bb84","type":"debug","z":"be4e3923.464d7","name":"test 5","active":true,"console":"false","complete":"true","x":670,"y":360,"wires":[]},{"id":"8394742a.37fd28","type":"change","z":"be4e3923.464d7","name":"Use $(FOO) in a change","rules":[{"t":"set","p":"payload","pt":"msg","to":"$(FOO)","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":360,"wires":[["9570e708.3bb84"]]},{"id":"42279cff.599694","type":"switch","z":"be4e3923.464d7","name":"Check payload for loged in user","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"$(LOGNAME)","vt":"str"},{"t":"neq","v":"$(LOGNAME)","vt":"str"}],"checkall":"true","outputs":2,"x":430,"y":440,"wires":[["acc12102.9cc3b8"],["30492755.6b095"]]},{"id":"459c87b4.553aa8","type":"comment","z":"be4e3923.464d7","name":"Before using you must make changes to settings.js - see the info bar for details.","info":"Environmental variables (enter 'env' in a terminal to see which can be used) can be accessed by adding 'env:require(env)' to the 'functionGlobalContext:' section of the settings.js file. Once entered you can access them in any node that has a text field (a/z) or in a function node.\n\nFor example: to use in a text (a/z) field such as an INJECT node. use the format: $(LOGNAME)\nTo use in a FUNCTION use the following:\n\tvar env = global.get('env');\n\tmsg.payload = env.LOGNAME;\n\nSPECIAL CASES:\nIf you want to use HOSTNAME or declare your own environmental variable, you must make an additional change to the settings.js file. Add the following lines just before the line 'module.exports = {':\n\tprocess.env.HOSTNAME = require('os').hostname();\n\tprocess.env.FOO = 'just another bar';\n\nOnce you have made the changes to the settings.js file, stop/restart node-red to make them avaliable for use.\nYou can now use either as explained above. Examples are in the flows.\n","x":440,"y":60,"wires":[]},{"id":"a5bbd048.312878","type":"inject","z":"be4e3923.464d7","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":130,"y":520,"wires":[["c6067b95.fc258"]]},{"id":"c6067b95.fc258","type":"function","z":"be4e3923.464d7","name":"Use $(LOGNAME) in a function","func":"var env = global.get('env');\nmsg.payload = env.LOGNAME;\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":520,"wires":[["f14f3512.ccd678"]]},{"id":"f14f3512.ccd678","type":"debug","z":"be4e3923.464d7","name":"test 8","active":true,"console":"false","complete":"true","x":670,"y":520,"wires":[]},{"id":"4f04d7bf.f1313","type":"inject","z":"be4e3923.464d7","name":"Use $(LOGNAME)","topic":"","payload":"$(LOGNAME)","payloadType":"str","repeat":"","crontab":"","once":false,"x":170,"y":420,"wires":[["42279cff.599694"]]},{"id":"f134ccff.5c621","type":"inject","z":"be4e3923.464d7","name":"Use JohnDoe","topic":"","payload":"JohnDoe","payloadType":"str","repeat":"","crontab":"","once":false,"x":150,"y":460,"wires":[["42279cff.599694"]]},{"id":"acc12102.9cc3b8","type":"debug","z":"be4e3923.464d7","name":"test 6 - true","active":true,"console":"false","complete":"true","x":690,"y":420,"wires":[]},{"id":"30492755.6b095","type":"debug","z":"be4e3923.464d7","name":"test 6 - false","active":true,"console":"false","complete":"true","x":690,"y":460,"wires":[]}]
juggledad

Flow Info

created 1 week, 5 days ago

Node Types

Core
  • change (x1)
  • comment (x1)
  • debug (x8)
  • function (x2)
  • inject (x8)
  • switch (x1)

Tags

  • environmental
  • variables
  • process
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option