Inserting timestamp into serial string in Node-RED

I’m relatively new to Node-RED, so this should be a pretty straightforward question.

I have an RS-232 line from an old machine feeding into Node-RED through a serial module on my Epic. Everytime the machine is cycled, a string of data is sent to a Serial In node in my flow, then I’m writing this string to a new line of a .txt file that’s stored on the Epic.

The string from the machine contains an inaccurate, non-updating timestamp (there’s no way to reconfigure this on the machine side, it’s literally from the 80s), so I want to replace the timestamp in the string with a true timestamp generated at the moment the machine is cycled (or generated at the moment a new string comes through the serial module into my flow).

Ignore ‘fx-227.txt’ for now. I have the serial data running there as is so we can continue running our machine without interruption. I’m confirming that my formatting mods are correct with ‘format_test.txt’.


The way I’m replacing the timestamp is by using a function node with a simple Replace command. The string always has that same ‘01/01/00 00:00:90’ timestamp so I’m safe to do that. So I basically want the highlighted portion in the above picture to be replaced with an accurate, updating timestamp.

This is an example of a full string that would come from the serial module: ‘52264 01/01/00 00:00:90 630 450 52.5 1.9594 0.0355 45 E 1.5’

What would be the best way to do this?

let optionsDate = { year: '2-digit', month: '2-digit', day: '2-digit' };
let optionsTime = { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false };
let today  = new Date();

let timeString = today.toLocaleDateString("en-US", optionsDate) + ' ' + today.toLocaleTimeString("en-US", optionsTime);
2 Likes