How to create a Node-RED flow with simulated PackML data

Learn how to set up Node-RED flows to monitor a simulated production line's state, machine processing speed, and production counts using MQTT and UMH data model conversion.

How to create a Node-RED flow with simulated PackML data
ℹ️

Instructions

  1. Connect to Node-RED using UMHLens and create a new flow.
    empty_nodered_flow

  2. Drag the following nodes into your flow: mqtt-in→filter→json→switch→function(10x)→mqtt-out.
    10functionnodespng

  3. Open the filter-node and set it to block unless value changes.
    blockunlessvaluechanges

  4. Open the switch-node and configure it as shown below. The switch node will check if the incoming message has one of the values we’ve configured and pass it to the correct function-node.
    switchnode

  5. Now connect all nodes.
    connectallnodespng

  6. Next configure the 10 function-nodes, which will convert the incoming message (Pack-ML state) into the UMH state. If you want to see all UMH states click here. Paste in the following code for each function-node starting from top→bottom.

    msg.payload = {"state":40000, "timestamp_ms":Date.now()}
    return msg;
    
    msg.payload = {"state":40000, "timestamp_ms":Date.now()}
    return msg;
    
    msg.payload = {"state":90000, "timestamp_ms":Date.now()}
    return msg;
    
    msg.payload = { "state":10000, "timestamp_ms":Date.now()} 
    return msg;
    
    msg.payload = {"state":20000, "timestamp_ms":Date.now()}
    return msg;
    
    msg.payload = {"state":220000, "timestamp_ms":Date.now()}
    return msg;
    
    msg.payload = {"state":20000, "timestamp_ms":Date.now()}
    return msg;
    
    msg.payload = {"state":20000, "timestamp_ms":Date.now()}
    return msg;
    
    msg.payload = {"state":20000, "timestamp_ms":Date.now()}
    return msg;
    
  7. Configure the mqtt-in node with the servicename of hivemq and the topic testLocation/DefaultArea/DefaultProductionLine/Status/StateCurrent.

  8. For the mqtt-out node, select the created broker and use the topic ia/factoryinsight/Aachen/testAsset/state
    mqtt

  9. The next set of nodes will return the speed at which the machine is operating. Drag the following nodes into your flow: mqtt-in→json→function→mqtt-out. Configure the mqtt-in node with the topic testLocation/DefaultArea/DefaultProductionLine/Status/CurMachSpeed and the mqtt-out node with the topic ia/factoryinsight/aachen/maschine/processValue.
    image-7-1

  10. Next convert the current machine speed into a percentage number. Therefore, open the function node and paste in the following:

msg.payload = {                                //creating an object with key:value pairs
    "timestamp_ms":Date.now(),
    "CurrentState":parseFloat(msg.payload/102) //maximum speed is 102
}
return msg;
  1. Click deploy on the top right, and you are done with this set of nodes.
  2. The last set of nodes will count the good and defective produced products. Since this simulator already gives us the total number of defective and good products, we have to convert that, so that every time a product is produced (defective or good) we get a count signal. Import two mqtt-in, two json, three function, and one mqtt-out node into your flow.
    image-8-1
  3. Paste this code into function1 → On Start. The code will be run once on start.
context.set("current", 0);
context.set("previous", 0);
  1. Paste this code into function1 → On Message. The code will be run when a new message appears.
context.set("previous", context.get("current"))
context.set("current", msg.payload)
msg.payload = {
    "count":parseInt(context.get("current")-context.get("previous"))
}
return msg;
  1. Now repeat these two steps for function2. Code for function2 → On Start:
context.set("scrapcurrent", 0);
context.set("scrapprevious", 0);
  1. Code for function2 → On Message:
context.set("scrapprevious", context.get("scrapcurrent"));
context.set("scrapcurrent", msg.payload);
msg.payload = {
    "scrap":parseInt(context.get("scrapcurrent")-context.get("scrapprevious"))
}
return msg;
  1. For function3 you only need to paste code into On Message:
msg.payload = {
    "timestamp_ms":Date.now(),
    "count":parseInt(msg.payload.count ? msg.payload.count : 0),
    "scrap":parseInt(msg.payload.scrap ? msg.payload.scrap : 0),

}
msg.topic = "ia/factoryinsight/aachen/mach/count"; //mqtt-out topic
return msg;
  1. Since we already specified our mqtt-out topic in the last function node, we only need to configure our mqtt in nodes, with the upper one having the topic: testLocation/DefaultArea/DefaultProductionLine/Admin/ProdProcessedCount/0/Count and the lower one: testLocation/DefaultArea/DefaultProductionLine/Admin/ProdDefectiveCount/0/Count .To make our changes effective, open the mqtt-out node and click done. Finally click deploy.
  2. If you want to display your created data, make sure to check out our guides fo Grafana. This flow will look something like this in Grafana:
    grafanaaaaa

Read next

Node-RED meets Benthos!
benthos · Featured

Node-RED meets Benthos!

Yes, we’ve made it possible to bring Node-RED-style simplicity into Benthos pipelines, enabling your chosen LLM to handle tedious manufacturing connectivity and contextualization tasks—whether you’re prototyping or managing production-grade data flows.

Share, Engage, and Contribute!

Discover how you can share your ideas, contribute to our blog, and connect with us on other platforms.