CSV transformation example

Suppose you have a CSV file that details the energy consumption of various manufacturing stations in a factory. The file includes the station name, the month, and the total electricity consumed. You want to add a new column that displays the number of parts produced per station, based on the assumption that each part consumes a specific amount of electricity in addition to the machine's base electricity consumption.

[{"id":"1","type":"inject","z":"3a74ad88ecc45bcb","name":"Start","repeat":"","crontab":"","once":false,"onceDelay":0.1,"payload":"","payloadType":"date","x":90,"y":60,"wires":[["2"]]},{"id":"2","type":"template","z":"3a74ad88ecc45bcb","name":"CSV Data","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Station,Month,Consumption\nRio,January,4000\nNew York,January,1000\nTokio,January,3000","x":240,"y":60,"wires":[["3"]]},{"id":"3","type":"csv","z":"3a74ad88ecc45bcb","name":"CSV In","sep":",","hdrin":true,"hdrout":"","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":410,"y":60,"wires":[["4","5"]]},{"id":"4","type":"debug","z":"3a74ad88ecc45bcb","name":"Debug JSON","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":590,"y":60,"wires":[]},{"id":"5","type":"change","z":"3a74ad88ecc45bcb","name":"Add PartsProduced","rules":[{"t":"set","p":"columns","pt":"msg","to":"msg.columns & \", PartsProduced\"","tot":"jsonata"},{"t":"set","p":"payload.PartsProduced","pt":"msg","to":"payload.Consumption / 10","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":140,"wires":[["6"]]},{"id":"6","type":"csv","z":"3a74ad88ecc45bcb","name":"CSV out","sep":",","hdrin":"","hdrout":"all","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":420,"y":140,"wires":[["7"]]},{"id":"7","type":"debug","z":"3a74ad88ecc45bcb","name":"Debug Final CSV","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":140,"wires":[]}]

Flow Info

Created 2 years, 1 month ago
Rating: not yet rated

Actions

Rate:

Node Types

Core
  • change (x1)
  • csv (x2)
  • debug (x2)
  • inject (x1)
  • template (x1)

Tags

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