Adding a timestamp to data sent to csv file

I’ve created a data logger pulling analog points from my Opto Epic using the following in Node-RED:

inject (msg.payload = timestamp) > groov i/o read node > change node (msg.payload to the value of “value_name,” & msg.payload) > write file node (path with a file .csv)

Right now I get the proper analog value coming in to the csv file as expected but there’s no timestamp allocated to each interval. I suspect I need to add something in the change node but what??

Thanks,
Spencer

You need to append the time to the payload with a , between.
So I’d set your inject to something unique like msg.tstamp (ie something other than msg.payload which is getting over written).
Then in a function node, you want msg.data = msg.tstamp + "," + msg.value_name;
So now you have msg.data is the start of your CSV format with the time up front.

Some screenshots would be more helpful. You can paste in code between ``` (three back ticks) or just drop a screen snip in we can help build out the CSV.

1 Like

Thanks once again Beno. Here’s a screenshot of what I’ve done in the function node…clearly I did something wrong. The injector is set to msg.tstamp = timestamp

‘’’
image

That is a change node, not a function node, where the option you selected is JSONata, not JavaScript, so it uses different notation.

In this case you would need to use the & operator, not +.

msg.tstamp & "," & msg.value_name

You can always test this out by clicking the three dots ••• to the right of the to the value dropdown to open the expression editor. Once there you can switch to the Test tab and put in examples of your values and see the result.


The solution @Beno shared using a function node is typically recommended since JavaScript is a LOT more common than JSONata, but both will achieve the same thing with the right syntax.

1 Like

Thanks Terry, my bad - forgot to switch over to a function node rather than using a change node.

Thanks to both you and @Beno for your help - it worked! I ended up using the change node - tried the function code but for some reason it only sent the analog value to the csv, whereas, it showed the timestamp, name and value on the debug.

When I used the change node, basically the same code but formatted to suit the change node: msg.tstamp & “,DiffPress_CarbonFilter,” & msg.payload

This method properly sends all needed info to the csv file.

Thanks,
Spencer

2 Likes