Filename with dynamic date in node red

I am calling the camera snapshot service in nodered and specifying the following as Data :

{ "filename": "/config/www/bedroom{{ now().strftime(\"%Y%m%d-%H%M%S\") }}.jpg" }

This works fine if I call the service directly from HA and it creates a file with timestamp but from node red it doesnt take the timestamp into account and creates bedroom.jpg as the file name. what am I missing?

This part of the syntax is python specific, it evaluates the current date time into a yyyy-mm-dd-hhmmss syntax. So you’ll need to replace that part by something that works in JavaScript/Node-RED. The double braces syntax here is specifically to the Jinja2 library in Python. Note that this might execute in JavaScript as Mustache, however the syntax that follows is meant for Python/Jinja2.

ah! of course, thanks for pointing that out. I'll find a way to do it in javascript.

@afelix Looking around it seems it isnt straight forward to specify a datetime within json , can you give me some pointers please.
I could do a new date , format it and return if it was a function but not sure how to do it within a json string

You’re not doing it in a json string.

Your doing it in a javascript object.
If you format the date as a veritable you can then easily append that using the syntax

msg.whatever= “stuff”+variable+”otherstuff”

var now = new Date();
// Create formatted time
var yyyy = now.getFullYear();
var mm = now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based
var dd  = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
var hh = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
var mmm  = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
var ss  = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();

// file path with / at the end
var path = "/home/pi/node-red-static/";                     // This is the path
var filename = "cam3_"+yyyy+mm+dd+"-"+hh+mm+ss+".jpg";     // file name

This one works for me

Thanks @edje11 , I was hoping to put something within the Data field of the call service node but then I can always include a function node before that and pass on the filename as an input to the call service node. Thanks

FYI, here is another thread with a solution that requires no programming...

This is great but I still have a question. After constructing this file name I need to use the Cal, Service Snapshot node. How do I pass on the path and filename in my filename definition in this node ?

So the standard is :

{
"filename": "/config/www/somefilename.jpg"
}

How can I modify this and use var path and filename here ?

Many Thanks !!!

@Tokn59 Plese don't post the same thing in multiple threads. I will close this thread since it is over a year old.