Subflow - extract value from json

Hello,
I'm a beginner to node red. I'm trying to create subflow and its environment variables. I have problem with extracting value from json payload. The flow is as follow:

[{"id":"9b706329.bc293","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":60,"y":80,"wires":[{"id":"70ccbc5b.6212d4"}]}],"out":[{"x":340,"y":80,"wires":[{"id":"70ccbc5b.6212d4","port":0}]}],"env":[{"name":"msgTopic","type":"str","value":"MBLightTopic"},{"name":"msgValue","type":"env","value":"msg.paylad.MBLight"}],"color":"#DDAA99"},{"id":"70ccbc5b.6212d4","type":"function","z":"9b706329.bc293","name":"MBLight","func":"var msgTopic = env.get(\"msgTopic\");\nvar val_MBLight = msg.payload.MBLight;  // <-- How to decleare in Environment Variables\n\nif(msg.topic == msgTopic)        { msg.payload = val_MBLight; return msg; }\nelse                             { return null; } ","outputs":1,"noerr":0,"x":200,"y":80,"wires":[[]]},{"id":"fc8310a7.85ada","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"dd849f09.d85a9","type":"inject","z":"fc8310a7.85ada","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":220,"wires":[["ef283d9c.701cc"]]},{"id":"ef283d9c.701cc","type":"function","z":"fc8310a7.85ada","name":"MBLight","func":"var msgTopic = \"MBLightTopic\";\nvar val_MBLight = msg.payload.MBLight;\nif(msg.topic == msgTopic)        { msg.payload = val_MBLight; return msg; }\nelse                             { return null; } \n\n","outputs":1,"noerr":0,"x":440,"y":220,"wires":[["e97c1575.5e9718"]]},{"id":"e97c1575.5e9718","type":"debug","z":"fc8310a7.85ada","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":220,"wires":[]},{"id":"d1a4c569.e21068","type":"inject","z":"fc8310a7.85ada","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":380,"wires":[["9b5eba1f.eef108"]]},{"id":"6bfdad95.bf0a64","type":"debug","z":"fc8310a7.85ada","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":380,"wires":[]},{"id":"9b5eba1f.eef108","type":"subflow:9b706329.bc293","z":"fc8310a7.85ada","name":"","env":[],"x":440,"y":380,"wires":[["6bfdad95.bf0a64"]]},{"id":"f35567a5.940118","type":"inject","z":"fc8310a7.85ada","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["ef283d9c.701cc"]]},{"id":"af85c38.08a914","type":"inject","z":"fc8310a7.85ada","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":340,"wires":[["9b5eba1f.eef108"]]}]

2020-03-10 22_52_44-Node-RED

How to declare "msg.payload.MBLight" in environment variable of subflow?

Best regards,
Aung

Why would you want to do so ? You already have access to all msg properties inside the subflow. The second line in your function node is already using msg.payload.MBLight, right ? We use "environmental variables" (I prefer to use the wording instance variables) when we need to inject values that does not exist as properties of the msg object. These values allow us to customise the many subflows instances for a different behaviour.

Do you want to specify a particular key name in subflow's environment variables?

That's my guess, so if correct, then:

  • use string value for the name of the key,
  • use bracket notation to access the key inside the msg.payload.
[{"id":"b87b90dd.a04a3","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"af477576.468718","type":"subflow","name":"Subflow 1","info":"","category":"","in":[{"x":60,"y":80,"wires":[{"id":"471337c2.b35128"}]}],"out":[{"x":340,"y":80,"wires":[{"id":"471337c2.b35128","port":0}]}],"env":[{"name":"msgTopic","type":"str","value":"MBLightTopic"},{"name":"msgValue","type":"str","value":"msg.payload.MBLight"}],"color":"#DDAA99"},{"id":"471337c2.b35128","type":"function","z":"af477576.468718","name":"MBLight","func":"var msgTopic = env.get(\"msgTopic\");\nvar val_Light = msg.payload[env.get(\"msgValue\")];  // <-- How to decleare in Environment Variables\n\nif(msg.topic == msgTopic)        { msg.payload = val_Light; return msg; }\nelse                             { return null; } ","outputs":1,"noerr":0,"x":200,"y":80,"wires":[[]]},{"id":"95fd80d5.94b35","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":220,"wires":[["99115b7.f606ca8"]]},{"id":"99115b7.f606ca8","type":"function","z":"b87b90dd.a04a3","name":"MBLight","func":"var msgTopic = \"MBLightTopic\";\nvar val_MBLight = msg.payload.MBLight;\nif(msg.topic == msgTopic)        { msg.payload = val_MBLight; return msg; }\nelse                             { return null; } \n\n","outputs":1,"noerr":0,"x":440,"y":220,"wires":[["597bbf8a.d54df"]]},{"id":"597bbf8a.d54df","type":"debug","z":"b87b90dd.a04a3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":220,"wires":[]},{"id":"e89a4d70.f904","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"MBLightTopic","payload":"{\"MBLight\":12}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":380,"wires":[["a06d5b92.476078"]]},{"id":"8004c464.4ad7d8","type":"debug","z":"b87b90dd.a04a3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":620,"y":380,"wires":[]},{"id":"a06d5b92.476078","type":"subflow:af477576.468718","z":"b87b90dd.a04a3","name":"","env":[{"name":"msgValue","value":"MBLight","type":"str"}],"x":440,"y":380,"wires":[["8004c464.4ad7d8"]]},{"id":"ef0b979e.d52548","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":180,"wires":[["99115b7.f606ca8"]]},{"id":"4908c60d.935248","type":"inject","z":"b87b90dd.a04a3","name":"","topic":"CRLightTopic","payload":"{\"CRLight\":15}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":340,"wires":[["a06d5b92.476078"]]}]

@Andrei, I'd like to create subflow as reusable function with input parameters like c++ function. thanks

@techraf, thank you for help

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