How to create a filename using jsonata in file write node

I'm relatively new to node-red. I've searched and cannot seem to find a "simple" solution to what I want to do. It seems like I should be able to use a jsonata expression to create a filename in my write file node. I want the filename to be of the format:
/user/public/documents/battery/[msg.topic]_[YYYY-MM-DD].txt
where msg.topic is the message topic and YYYY-MM-DD is the current year, month and day.

I cannot seem to figure out to correct syntax to do this. I know I can do it with code in a function node and assign it to msg.filename, but I'd like to understand how to do it with jsonata. I "asked" ChatGPT how to do it and it came up with the following line, which does not work (for a number of reasons)

/users/public/documents/battery/[[msg.topic]]_[[moment().format('YYYY-MM-DD')]].txt

Can this be easily done? If so, how?

"/users/public/documents/battery/" & $$.topic & "_" & $moment().format('YYYY-MM-DD') & ".txt"

E1cid:
Thanks for your very rapid response. I have node-red-contrib-moment installed, but your solution fails with the error on moment "attempted to invoke a non-function". Do I need to do something else to get moment() to work?

I copied your text but failed to notice there was no $ before moment
edit post above.
p.s. I would add hours mins and seconds, other wise you could end up with all files on same day overwriting each other, unless that is what you want.

I want to append to the file and have one file for each topic for each day. Thanks.

Is there someplace where the jsonata syntax/semantics for node-red is described simply?

Why $$.topic and not msg.topic in this context?

Google JSONata and one of the top one's will be jsonata.org docs

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