General support for newbie

Hi

for some time I'm using node-red and have managed to setup some flows. I watched the Node-RED Essentials Videos, downloades & tested many examples and analyzed the debug-output in detail.
Unfortunately I'm still struggling with proper handling of node-red messages (topic, payload, array, object,...). With lots of try & error, I managed to create this flow.

[{"id":"c26fccb5be338e02","type":"group","z":"4b82e3cc342b356d","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["73339ead1924d2c8","9b5135f3c68cd689","77b0696eec233433","0528be875be31158","cf2ee961021ae563","8ed6ec5671466c90","19517d09aee7d992","276a32133241c4fa","39a0877b0bdcbeb0","2e45dcabc0bba4bf","257775974f6de189","5b2a44c745774f2a","a4d463f1fe6a071d"],"x":74,"y":1159,"w":1132,"h":282},{"id":"73339ead1924d2c8","type":"modbus-getter","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"Modbus P_Ch1","showStatusActivities":false,"showErrors":false,"showWarnings":true,"logIOActivities":false,"unitid":"1","dataType":"HoldingRegister","adr":"19020","quantity":"2","server":"9782f823cc54f17a","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":400,"y":1280,"wires":[["2e45dcabc0bba4bf","a4d463f1fe6a071d"],[]]},{"id":"9b5135f3c68cd689","type":"debug","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"debug 2578","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1090,"y":1400,"wires":[]},{"id":"77b0696eec233433","type":"modbus-getter","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"Modbus P_Ch2","showStatusActivities":false,"showErrors":false,"showWarnings":true,"logIOActivities":false,"unitid":"1","dataType":"HoldingRegister","adr":"19022","quantity":"2","server":"9782f823cc54f17a","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":400,"y":1340,"wires":[["257775974f6de189"],[]]},{"id":"0528be875be31158","type":"modbus-getter","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"Modbus P_Ch3","showStatusActivities":false,"showErrors":false,"showWarnings":true,"logIOActivities":false,"unitid":"1","dataType":"HoldingRegister","adr":"19024","quantity":"2","server":"9782f823cc54f17a","useIOFile":false,"ioFile":"","useIOForPayload":false,"emptyMsgOnFail":false,"keepMsgProperties":false,"delayOnStart":false,"startDelayTime":"","x":400,"y":1400,"wires":[["5b2a44c745774f2a"],[]]},{"id":"cf2ee961021ae563","type":"join","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"join","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":",","joinerType":"str","useparts":true,"accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":790,"y":1400,"wires":[["39a0877b0bdcbeb0","276a32133241c4fa"]]},{"id":"8ed6ec5671466c90","type":"debug","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"debug 2579","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":830,"y":1260,"wires":[]},{"id":"19517d09aee7d992","type":"inject","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"iso","payloadType":"date","x":180,"y":1340,"wires":[["73339ead1924d2c8","77b0696eec233433","0528be875be31158"]]},{"id":"276a32133241c4fa","type":"debug","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"debug 2580","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":970,"y":1320,"wires":[]},{"id":"39a0877b0bdcbeb0","type":"change","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"sum","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":1400,"wires":[["9b5135f3c68cd689"]]},{"id":"2e45dcabc0bba4bf","type":"buffer-parser","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatbe","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"0.001","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":590,"y":1280,"wires":[["cf2ee961021ae563","8ed6ec5671466c90"]]},{"id":"257775974f6de189","type":"buffer-parser","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatbe","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"0.001","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":590,"y":1340,"wires":[["cf2ee961021ae563"]]},{"id":"5b2a44c745774f2a","type":"buffer-parser","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"","data":"payload","dataType":"msg","specification":"spec","specificationType":"ui","items":[{"type":"floatbe","name":"item1","offset":0,"length":1,"offsetbit":0,"scale":"0.001","mask":""}],"swap1":"","swap2":"","swap3":"","swap1Type":"swap","swap2Type":"swap","swap3Type":"swap","msgProperty":"payload","msgPropertyType":"str","resultType":"value","resultTypeType":"return","multipleResult":true,"fanOutMultipleResult":false,"setTopic":true,"outputs":1,"x":590,"y":1400,"wires":[["cf2ee961021ae563"]]},{"id":"a4d463f1fe6a071d","type":"debug","z":"4b82e3cc342b356d","g":"c26fccb5be338e02","name":"debug 2581","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":670,"y":1200,"wires":[]},{"id":"9782f823cc54f17a","type":"modbus-client","name":"Janitza_Test","clienttype":"tcp","bufferCommands":true,"stateLogEnabled":true,"queueLogEnabled":true,"failureLogEnabled":true,"tcpHost":"172.20.39.200","tcpPort":"502","tcpType":"DEFAULT","serialPort":"/dev/ttyUSB","serialType":"RTU-BUFFERD","serialBaudrate":"9600","serialDatabits":"8","serialStopbits":"1","serialParity":"none","serialConnectionDelay":"100","serialAsciiResponseStartDelimiter":"0x3A","unit_id":1,"commandDelay":1000,"clientTimeout":3000,"reconnectOnTimeout":true,"reconnectTimeout":2000,"parallelUnitIdsAllowed":true,"showWarnings":true,"showLogs":true}]

Since the modbus-nodes requires corresponding hardware, I tried to replace them by a function giving identical output, so that other usesers can test the flow. Unfortunately I did not succeed doing this. Here is the output of "debug 2581".

{"topic":"73339ead1924d2c8","messageId":"6790f19e55e41801585b00f1","payload":[18109,25696],"queueLengthByUnitId":{"unitId":1,"queueLength":0},"queueUnitId":1,"unitId":1,"modbusRequest":{"value":"2025-01-22T13:24:46.008Z","unitid":"1","fc":3,"address":"19020","quantity":"2","messageId":"6790f19e55e41801585b00f1"},"responseBuffer":{"data":[18109,25696],"buffer":[70,189,100,96]},"_msgid":"8a99291638398420"}

Can someone please advice how to do this?

After that I would like to ask more questions regarding messages and optimization.

Hi Egge,

If I understand correctly, you want to "simulate" the Modbus node's output so that you don't need to connect to actual hardware.

You could try to use a function node and force msg to be as you require.

Example

[
    {
        "id": "0144e009f69528bb",
        "type": "function",
        "z": "e9cf7aa393301396",
        "name": "Simulate MODBUS node",
        "func": "msg = {\"topic\":\"73339ead1924d2c8\",\"messageId\":\"6790f19e55e41801585b00f1\",\"payload\":[18109,25696],\"queueLengthByUnitId\":{\"unitId\":1,\"queueLength\":0},\"queueUnitId\":1,\"unitId\":1,\"modbusRequest\":{\"value\":\"2025-01-22T13:24:46.008Z\",\"unitid\":\"1\",\"fc\":3,\"address\":\"19020\",\"quantity\":\"2\",\"messageId\":\"6790f19e55e41801585b00f1\"},\"responseBuffer\":{\"data\":[18109,25696],\"buffer\":[70,189,100,96]},\"_msgid\":\"8a99291638398420\"};\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 454,
        "y": 512,
        "wires": [
            [
                "153576280fb7c962"
            ]
        ]
    },
    {
        "id": "6882b42b0965f6ec",
        "type": "inject",
        "z": "e9cf7aa393301396",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 226,
        "y": 512,
        "wires": [
            [
                "0144e009f69528bb"
            ]
        ]
    },
    {
        "id": "153576280fb7c962",
        "type": "debug",
        "z": "e9cf7aa393301396",
        "name": "debug 83",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 652,
        "y": 512,
        "wires": []
    }
]

or you could put it in a core change node...

[{"id":"a4105a37b00a23a4","type":"change","z":"37ef9ab5d60ffd43","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"topic\":\"73339ead1924d2c8\",\"messageId\":\"6790f19e55e41801585b00f1\",\"payload\":[18109,25696],\"queueLengthByUnitId\":{\"unitId\":1,\"queueLength\":0},\"queueUnitId\":1,\"unitId\":1,\"modbusRequest\":{\"value\":\"2025-01-22T13:24:46.008Z\",\"unitid\":\"1\",\"fc\":3,\"address\":\"19020\",\"quantity\":\"2\",\"messageId\":\"6790f19e55e41801585b00f1\"},\"responseBuffer\":{\"data\":[18109,25696],\"buffer\":[70,189,100,96]},\"_msgid\":\"8a99291638398420\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":3220,"wires":[["b2324e70e036f53d"]]},{"id":"5e1bfb23be476daf","type":"inject","z":"37ef9ab5d60ffd43","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":3220,"wires":[["a4105a37b00a23a4","7f2c3bdc5407e982"]]},{"id":"b2324e70e036f53d","type":"debug","z":"37ef9ab5d60ffd43","name":"debug 2849","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":760,"y":3220,"wires":[]}]

Yes, but in the example you provided, the entire content is within msg.payload. This approach doesn't emulate the behavior of a Modbus node since the msg structure is not formatted in the same way. Correct?

would it be possible to make it with change node?

yes, exactly this I want to do...

and I already tried it with a function similar to your proposal, but always got errors or other outputs than required

I also tried change node


and put the output of my debug

but this gave a different debug-response and also an error on buffer parser

You seem to have set msg.payload to an object which includes msg.payload.topic, some other stuff and presumably msg.payload.payload.
No surprise the buffer parses chokes on it.

You can have multiple lines in the change node, setting just one of msg.payload, msg.topic, etc per line.

In contrast, in a function node you can do something like this

const newmsg = {
"payload": 42,
"topic": "Ion Density"
}

Incidentally, I don't think it's a good idea for your simulated modbus data to include messageid. Every message will automatically be assigned a message id on creation. It should be unique.