How to stop after collecting all the expected messages

I am collecting NMEA data from an UDP connection and would like to retrieve only some of the NMEA sentences, parse them and write them in a log file.
I have figured all out, but how to stop taking additional sentences when I have already collected all the ones I need.
Below the flow I made (please forgive me if it is not well designed but I am not an expert)

[
    {
        "id": "e030ac7d47753673",
        "type": "switch",
        "z": "f989436d61b86384",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "RMC",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "MWV",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "VWVLW",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "DPT",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "Fuel#0",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "Fuel#1",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "EngineHrs",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "EngineOil",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "RPM",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 9,
        "x": 590,
        "y": 360,
        "wires": [
            [
                "468b75d4974a0ca7"
            ],
            [
                "468b75d4974a0ca7"
            ],
            [
                "4e6214c5310a114e"
            ],
            [
                "dee70c5c7a681dd8"
            ],
            [
                "4230bfb7d2f6e25f"
            ],
            [
                "97200db0524d123f"
            ],
            [
                "045dff36dd85d182"
            ],
            [
                "5c1c6e39f53396c0"
            ],
            [
                "51d90f26156dc3a5"
            ]
        ]
    },
    {
        "id": "468b75d4974a0ca7",
        "type": "nmea",
        "z": "f989436d61b86384",
        "name": "",
        "property": "payload",
        "outputProperty": "payload",
        "x": 770,
        "y": 240,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "dee70c5c7a681dd8",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "depth",
        "func": "var data = msg.payload.split(\",\");\nmsg.payload = { \"depth\": +data [1] }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 770,
        "y": 320,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "4230bfb7d2f6e25f",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "fuel#0",
        "func": "var data = msg.payload.split(\",\");\nmsg.payload = { \"tank0\": +data [2] }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 770,
        "y": 360,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "97200db0524d123f",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "fuel#1",
        "func": "var data = msg.payload.split(\",\");\nmsg.payload = { \"tank1\": +data [2] }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 770,
        "y": 400,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "4e6214c5310a114e",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "miles",
        "func": "var data = msg.payload.split(\",\");\nmsg.payload = { \"miles\": +data [1] }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 770,
        "y": 280,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "045dff36dd85d182",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "enginehrs",
        "func": "var data = msg.payload.split(\",\");\nmsg.payload = { \"enginehrs\": +data [2] }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 780,
        "y": 440,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "5c1c6e39f53396c0",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "enginedata",
        "func": "var data = msg.payload.split(\",\");\nmsg.payload = { \"oilpress\": +data[2], \"watertemp\": +data[6], \"voltage\": +data[10] }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 480,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "69eeeb9504fed840",
        "type": "join",
        "z": "f989436d61b86384",
        "name": "",
        "mode": "custom",
        "build": "merged",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "5",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 950,
        "y": 340,
        "wires": [
            [
                "2f41f0de9f974eda"
            ]
        ]
    },
    {
        "id": "8fb77b2635ad3637",
        "type": "file",
        "z": "f989436d61b86384",
        "name": "LogFile",
        "filename": "filename",
        "filenameType": "msg",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 1300,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "2f41f0de9f974eda",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "LogLine",
        "func": "let datetime = new Date().toISOString()\nlet localtime = datetime.toLocaleString().split(\".\")\nmsg.filename = flow.get('FileName');\nmsg.payload = localtime[0] + \",\" + msg.payload.trackTrue + \",\" + msg.payload.speedKnots + \",\" + msg.payload.miles + \",\" + msg.payload.lat + \" \" + msg.payload.latPole + \",\" + msg.payload.lon + \" \" + msg.payload.lonPole + \",\" + msg.payload.angle + \",\" + msg.payload.speed + \",\" + (msg.payload.depth + 0.56) + \",\" + msg.payload.enginehrs + \",\" + msg.payload.rpm / 10 + \",\" + msg.payload.tank0 + \",\" + msg.payload.tank1 + \",\" + msg.payload.oilpress + \",\" + msg.payload.watertemp + \",\" + msg.payload.voltage;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1100,
        "y": 340,
        "wires": [
            [
                "8fb77b2635ad3637"
            ]
        ]
    },
    {
        "id": "51d90f26156dc3a5",
        "type": "function",
        "z": "f989436d61b86384",
        "name": "enginerpm",
        "func": "var data = msg.payload.split(\",\");\nmsg.payload = { \"rpm\": +data[3] }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 790,
        "y": 520,
        "wires": [
            [
                "69eeeb9504fed840"
            ]
        ]
    },
    {
        "id": "8f8a7e014c41a3cb",
        "type": "udp in",
        "z": "f989436d61b86384",
        "name": "Nmea in",
        "iface": "",
        "port": "10110",
        "ipv": "udp4",
        "multicast": "false",
        "group": "",
        "datatype": "utf8",
        "x": 420,
        "y": 360,
        "wires": [
            [
                "e030ac7d47753673"
            ]
        ]
    }
]

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

See this post for more details - How to share code or flow json

I hope to have made it right :grinning:

Can you add examples of incoming data from the UDP node and the NMEA node.
Also what are the expected messages and why is count in join 5 and switch has 9 outputs?

@mudwalker I do not see where OP asks about influx, is he not trying to log to a csv file?

That will teach me to have Tea in the middle of replying!!

Is it the tea or are you adding a wee dram.

1 Like

No comment. :rofl:

2 Likes

20230410 - Nmea Capture.txt (348.7 KB)
this is a capture of the onboard NMEA sentences from which I need to parse only the ones present into the switch node.
The NMEA node is just handy because it parses automatically the RMC and the MWV sentences (it cannot parse the other sentences of interest) but can be replaced with function nodes like the other ones.
The 5 instead of 9 is just an error, I forgot to increase that after adding the last 4 rules.
Yes I am writing to a csv log file, no database.

When using a merged object it counts each of the properties of the object you are merging... so the NMEA node produces a whole load - and the engine data function produces 3 - so the join needs to be set to wait for 28 parts I think.. eg

[{"id":"8f8a7e014c41a3cb","type":"udp in","z":"dbef29b93f753e7e","name":"Nmea in","iface":"","port":"10110","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":100,"y":400,"wires":[["e030ac7d47753673"]]},{"id":"e030ac7d47753673","type":"switch","z":"dbef29b93f753e7e","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"RMC","vt":"str"},{"t":"cont","v":"MWV","vt":"str"},{"t":"cont","v":"VWVLW","vt":"str"},{"t":"cont","v":"DPT","vt":"str"},{"t":"cont","v":"Fuel#0","vt":"str"},{"t":"cont","v":"Fuel#1","vt":"str"},{"t":"cont","v":"EngineHrs","vt":"str"},{"t":"cont","v":"EngineOil","vt":"str"},{"t":"cont","v":"RPM","vt":"str"}],"checkall":"true","repair":false,"outputs":9,"x":270,"y":400,"wires":[["468b75d4974a0ca7"],["468b75d4974a0ca7"],["4e6214c5310a114e"],["dee70c5c7a681dd8"],["4230bfb7d2f6e25f"],["97200db0524d123f"],["045dff36dd85d182"],["5c1c6e39f53396c0"],["51d90f26156dc3a5"]]},{"id":"468b75d4974a0ca7","type":"nmea","z":"dbef29b93f753e7e","name":"","property":"payload","outputProperty":"payload","x":450,"y":280,"wires":[["69eeeb9504fed840"]]},{"id":"4e6214c5310a114e","type":"function","z":"dbef29b93f753e7e","name":"miles","func":"var data = msg.payload.split(\",\");\nmsg.payload = { \"miles\": +data [1] }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":320,"wires":[["69eeeb9504fed840"]]},{"id":"dee70c5c7a681dd8","type":"function","z":"dbef29b93f753e7e","name":"depth","func":"var data = msg.payload.split(\",\");\nmsg.payload = { \"depth\": +data [1] }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":360,"wires":[["69eeeb9504fed840"]]},{"id":"4230bfb7d2f6e25f","type":"function","z":"dbef29b93f753e7e","name":"fuel#0","func":"var data = msg.payload.split(\",\");\nmsg.payload = { \"tank0\": +data [2] }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":400,"wires":[["69eeeb9504fed840"]]},{"id":"97200db0524d123f","type":"function","z":"dbef29b93f753e7e","name":"fuel#1","func":"var data = msg.payload.split(\",\");\nmsg.payload = { \"tank1\": +data [2] }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":440,"wires":[["69eeeb9504fed840"]]},{"id":"045dff36dd85d182","type":"function","z":"dbef29b93f753e7e","name":"enginehrs","func":"var data = msg.payload.split(\",\");\nmsg.payload = { \"enginehrs\": +data [2] }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":480,"wires":[["69eeeb9504fed840"]]},{"id":"5c1c6e39f53396c0","type":"function","z":"dbef29b93f753e7e","name":"enginedata","func":"var data = msg.payload.split(\",\");\nmsg.payload = { \"oilpress\": +data[2], \"watertemp\": +data[6], \"voltage\": +data[10] }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":520,"wires":[["69eeeb9504fed840"]]},{"id":"51d90f26156dc3a5","type":"function","z":"dbef29b93f753e7e","name":"enginerpm","func":"var data = msg.payload.split(\",\");\nmsg.payload = { \"rpm\": +data[3] }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":560,"wires":[["69eeeb9504fed840"]]},{"id":"69eeeb9504fed840","type":"join","z":"dbef29b93f753e7e","name":"","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"28","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":630,"y":380,"wires":[["2f41f0de9f974eda"]]},{"id":"2f41f0de9f974eda","type":"function","z":"dbef29b93f753e7e","name":"LogLine","func":"let datetime = new Date().toISOString()\nlet localtime = datetime.toLocaleString().split(\".\")\nmsg.filename = flow.get('FileName');\nmsg.payload = localtime[0] + \",\" + msg.payload.trackTrue + \",\" + msg.payload.speedKnots + \",\" + msg.payload.miles + \",\" + msg.payload.lat + \" \" + msg.payload.latPole + \",\" + msg.payload.lon + \" \" + msg.payload.lonPole + \",\" + msg.payload.angle + \",\" + msg.payload.speed + \",\" + (msg.payload.depth + 0.56) + \",\" + msg.payload.enginehrs + \",\" + msg.payload.rpm / 10 + \",\" + msg.payload.tank0 + \",\" + msg.payload.tank1 + \",\" + msg.payload.oilpress + \",\" + msg.payload.watertemp + \",\" + msg.payload.voltage;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":380,"wires":[["8fb77b2635ad3637"]]},{"id":"8fb77b2635ad3637","type":"file","z":"dbef29b93f753e7e","name":"LogFile","filename":"filename","filenameType":"msg","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":980,"y":380,"wires":[[]]}]

PS - if you add a change node like this

[{"id":"6c59b798e0138577","type":"change","z":"dbef29b93f753e7e","name":"","rules":[{"t":"set","p":"payload.name","pt":"msg","to":"Pacmanmt","tot":"str"},{"t":"set","p":"payload.icon","pt":"msg","to":"ship","tot":"str"},{"t":"set","p":"payload.hdg","pt":"msg","to":"payload.trackTrue","tot":"msg"},{"t":"move","p":"payload.speedKnots","pt":"msg","to":"payload.speed","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":500,"wires":[["b37fa8f4e89f79c4"]]}]

to the output of the join you can then feed that into a node-red-contrib-web-worldmap to map it easily :slight_smile:

I like your suggestion for the worldmap, i will look into it :point_up:

Alright, I amended the count of parts and it work perfectly in terms that the logfile single line is correct. Thank you so much for pointing this mistake.
The issue is that, since the NMEA sentences come in continuosly, the flow keeps generating logfile lines.
What I would like to achieve is that, after completing a logfile line, it stops completely.
To give you more background, the aim is to register a single logfile line (with the collected data at that instant) not more (and this was my initial question).
I will then take care of adding an inject node that triggers again the capture of another line after each 1 hour and so on.

In that case I would feed the output of the line to a change node and move it into a flow variable... so it keeps updating. And then just use the inject to another change node to retrieve that data back and write to the file whenever you want.

Thanks for the suggestion but I would rather like to find a more efficient way and avoid to keep the processor working on data that are to throw away except once every hour. :thinking:

OK. Is really not much load. But you could add a simple-gate node after the udp in node and use the inject to fire a trigger node to open the gate for 10 secs or whatever then close it again and on closing read the latest value and save it

Yes, it is what I was thinking yesterday evening after answering you.
Thank you so much for the valuable help, I really appreciate. :smiley:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.