r/nodered • • Feb 01 '21

JSON PAYLOAD WITH CHANGING VARIABLES

I am trying to create a node-red flow which will simulate a sensor getting data from people entering and leaving a restaurant. I was thinking of developing a code that will create JSON payloads in which one field needs to be changing all the time (ClientsInRestaurant) by adding or removing "1" client at a time, but I cannot figure it out. Is there any nodes where I can give a certain value to a variable, then put that variable inside the JSON field, and then send that JSON Payload to the HTTP POST/PUT request?

Thank you in advance

0 Upvotes

4 comments sorted by

2

u/nlecaude Feb 02 '21

Something like this ?


[{"id":"bbe758f0.7a393","type":"tab","label":"Flow 25","disabled":false,"info":""},{"id":"4a6e084d.e2f028","type":"inject","z":"bbe758f0.7a393","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":190,"y":140,"wires":[["d9f1e5ec.6374b"]]},{"id":"9d29e24b.23fd","type":"inject","z":"bbe758f0.7a393","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-1","payloadType":"num","x":190,"y":200,"wires":[["d9f1e5ec.6374b"]]},{"id":"d9f1e5ec.6374b","type":"change","z":"bbe758f0.7a393","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"$flowContext('count') + payload","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"{\t  \"count\": $flowContext('count')\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[["ad606ac4.c3f5a"]]},{"id":"e19e6e85.22627","type":"debug","z":"bbe758f0.7a393","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":160,"wires":[]},{"id":"5dd33ae1.fa658c","type":"inject","z":"bbe758f0.7a393","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":160,"y":340,"wires":[["81b1df58.b4f5e"]]},{"id":"81b1df58.b4f5e","type":"change","z":"bbe758f0.7a393","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":340,"wires":[[]]},{"id":"ad606ac4.c3f5a","type":"http request","z":"bbe758f0.7a393","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":530,"y":160,"wires":[["e19e6e85.22627"]]}]

1

u/Inevitable_Ad8679 Feb 02 '21

Well I was thinking more like having a function node that adds (or remove) 1 clients at a time to a variable, and then put that variable into the field "availability" of a default JSON that I already have with all the info about the restaurant (location, city, PO, geolocation, Etc), but honestly I did not know how to send the JSON with the updated variable to the node that makes the `POST request.

1

u/nlecaude Feb 02 '21

If you check the change node I sent you can use the second rule to format your JSON and you can retrieve the count using flow context.

1

u/Inevitable_Ad8679 Feb 02 '21

[{"id":"bbe758f0.7a393","type":"tab","label":"Flow 25","disabled":false,"info":""},{"id":"4a6e084d.e2f028","type":"inject","z":"bbe758f0.7a393","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":190,"y":140,"wires":[["d9f1e5ec.6374b"]]},{"id":"9d29e24b.23fd","type":"inject","z":"bbe758f0.7a393","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-1","payloadType":"num","x":190,"y":200,"wires":[["d9f1e5ec.6374b"]]},{"id":"d9f1e5ec.6374b","type":"change","z":"bbe758f0.7a393","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"$flowContext('count') + payload","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"{\t \"count\": $flowContext('count')\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[["ad606ac4.c3f5a"]]},{"id":"e19e6e85.22627","type":"debug","z":"bbe758f0.7a393","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":160,"wires":[]},{"id":"5dd33ae1.fa658c","type":"inject","z":"bbe758f0.7a393","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":160,"y":340,"wires":[["81b1df58.b4f5e"]]},{"id":"81b1df58.b4f5e","type":"change","z":"bbe758f0.7a393","name":"","rules":[{"t":"set","p":"count","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":340,"wires":[[]]},{"id":"ad606ac4.c3f5a","type":"http request","z":"bbe758f0.7a393","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":530,"y":160,"wires":[["e19e6e85.22627"]]}]

Thank you! That is what I was thinking of, but I could not do it :( Thanks