logo elektroda
logo elektroda
X
logo elektroda

Node-Red how to group data in an object and then send to payload

cyzoz 1125 3
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 19380901
    cyzoz
    Level 10  
    I need help with grouping variables.
    I have an object called box 1 , I would like to write values into it ( the number of values can change)
    When the next object named box 2 comes I finish writing to box 1 and write to box 2.
    The box 1 object is sent to msg.payload.
    And so the next.

    I have something like this, but no idea how to do a loop of unknown number of values.

    [{"id":"6c21c63f.45b098","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"cc13338b.ba10d","type":"function","z":"6c21c63f.45b098", "name": "Update Array", "func": "let pay = msg.payload ;arr = flow.get(elements)|| [];};Position is the index of element in the array/// If element is not in the table it will be added to the end { arr.indexOf(pay);}.push(pay)} else { arr.splice(pos, 1);}nflow.set(elements,arr);\nmsg.payload = [....arr];\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":430,"y":300,"wires":[["cd124bf7.2e0238"]]},{"id":"cd124bf7.2e0238","type":"debug","z":"6c21c63f.45b098","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":610,"y":300,"wires":[]},{"id":"7f568e1d.58c21","type":"inject","z":"6c21c63f.45b098","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"One","payloadType":"str","x":190,"y":220,"wires":[["cc13338b.ba10d"]]},{"id":"7145ffa.f20ff","type":"inject","z":"6c21c63f.45b098","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Two","payloadType":"str","x":190,"y":260,"wires":[["cc13338b.ba10d"]]},{"id":"f038b7d6.8279f8","type":"inject","z":"6c21c63f.45b098","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Three","payloadType":"str","x":190,"y":300,"wires":[["cc13338b.ba10d"]]},{"id":"48446f7f.89b58","type":"inject","z":"6c21c63f.45b098","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Four","payloadType":"str","x":190,"y":340,"wires":[["cc13338b.ba10d"]]},{"id":"41b04df4.57e294","type":"inject","z":"6c21c63f.45b098","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Five","payloadType":"str","x":190,"y":380,"wires":[["cc13338b.ba10d"]]},{"id":"c5854399.1d46b","type":"inject","z":"6c21c63f.45b098","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Six","payloadType":"str","x":190,"y":420,"wires":[["cc13338b.ba10d"]]},{"id":"f97e5bae.602af8","type":"inject","z":"6c21c63f.45b098","name":"","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"Start","payloadType":"str","x":190,"y":140,"wires":[["90ad31e5.3a953"]]},{"id":"90ad31e5.3a953","type":"change","z":"6c21c63f.45b098","name":"Empty array","rules":[{"t":"set","p":"elements","pt":"flow","to":"[]","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":140,"wires":[[]]}]
  • ADVERTISEMENT
  • #2 19399736
    xury
    Automation specialist
    The flow you pasted does something completely different.
    When a value arrives the first time it is added to the array, and when it arrives the second time it is removed from it.
    As I understand it, you are talking about something else. It has to write to the array until another message arrives. The question is what exactly is the difference?
  • ADVERTISEMENT
  • #3 19402705
    cyzoz
    Level 10  
    Hello
    The 1 number is the rack number, then the elements are put into the rack.
    When the rack is full it leaves and a new one comes with a new number and we put the elements in it.
    I have no idea how to send the set to payload after each rack change.
  • #4 19402820
    xury
    Automation specialist
    Can you give an example of the data that comes in on the payload.
    Is the payload as in the example a single string data type or perhaps an array or object?
    How can you distinguish a new rack and when it is full?
ADVERTISEMENT