Using variables in exec node commands

5,418 views
Skip to first unread message

Adam S

unread,
Mar 29, 2016, 2:21:24 PM3/29/16
to Node-RED
I did a quick search and am surprised I didn't find anything relating to using variables in the commands executed by the exec node, unless I'm just missing something obvious?  I'm trying to pass the current date as a parameter in a command.  I guess I can always just create a bash script and do what I want there, but I thought this would be a feature of the exec node.

Mark Setrem

unread,
Mar 29, 2016, 2:30:09 PM3/29/16
to Node-RED
If I understand what you want to do I think you are missing something obvious.

If you double click on the exec node you should see that you can append the contents of the msg.payload to the exec command.

Adam S

unread,
Mar 29, 2016, 2:37:06 PM3/29/16
to Node-RED
That appears to work, but now I have an annoying warning that my command is blank.  The time needs to be inserted in the middle of the command so I can't just append it.  Something like the mustache template is what I'm looking for here.

Dave C-J

unread,
Mar 29, 2016, 2:42:59 PM3/29/16
to node...@googlegroups.com
You can add extra parameters after as well - using the extra parameters box...

Inline images 1

Adam S

unread,
Mar 29, 2016, 2:52:06 PM3/29/16
to Node-RED
That doesn't really help me unless you can explain how you're supposed to use it to insert the parameters in the middle of the command.

python3 /usr/local/bin/xlsx2csv '/home/pi/2-8-16 to 2-12-16 Schedule.xlsx' -n 'Sched 2-8' > csv2.csv && echo "$(tail -n +3 /home/pi/csv2.csv)" >/home/pi/csv2.csv

This is the command I am running.  Everything in bold needs to be dynamic.

Mark Setrem

unread,
Mar 29, 2016, 3:12:52 PM3/29/16
to Node-RED
So stick Python in the box and include the rest as the msg.payload

Adam S

unread,
Mar 29, 2016, 3:24:07 PM3/29/16
to Node-RED
That will work I guess.  Still would like to see something along the lines of mustache.  Is that something that can be added in a patch?

Example:
Inject node injecting date on 24 hour schedule > format node creating the date strings/fill an array > exec node with command "python3 /usr/local/bin/xlsx2csv '/home/pi/{{json.monday}} to {{json.friday}} Schedule.xlsx' -n 'Sched {{json.today}}' > csv2.csv && echo "$(tail -n +3 /home/pi/csv2.csv)" >/home/pi/csv2.csv"

Dave C-J

unread,
Mar 29, 2016, 7:15:37 PM3/29/16
to node...@googlegroups.com

Or use the template node (which does do mustache) in front of the exec node.

Julian Knight

unread,
Mar 31, 2016, 4:06:44 PM3/31/16
to Node-RED
Nooooo! Not resorting to Python ;-)

As you suggest, add a template node upstream to format the payload as required for the parameter.

Taha Zouhair

unread,
May 31, 2016, 2:43:37 PM5/31/16
to Node-RED
Anyone knows if I can use a variable (i.e. msg.from) in the extra input parameters?

Thanks!

Dave C-J

unread,
May 31, 2016, 2:54:53 PM5/31/16
to node...@googlegroups.com
Hi,
no.  but msg.payload can contain multiple space separated parameters.

Waqas Aslam

unread,
Dec 4, 2017, 9:12:59 AM12/4/17
to Node-RED
Hi Dave C-J
Is msg.payload always supposed to be appended at the end of command?
How can merge in between the command?

for example:
sha256sum filename.txt | awk '{print $1}'

here filename.txt is coming from msg.payload.

regards

Mark Setrem

unread,
Dec 4, 2017, 9:21:49 AM12/4/17
to Node-RED
why not append  "| awk '{print $1}'" to msg.payload before the exec node

i.e. msg.payload becomes  "filename.txt | awk '{print $1}'"

Dave C-J

unread,
Dec 4, 2017, 9:35:14 AM12/4/17
to node...@googlegroups.com
or indeed use the extra line for parameters after msg.payload...

Inline images 1

Waqas Aslam

unread,
Dec 4, 2017, 10:49:51 AM12/4/17
to Node-RED
the reason why I didn't want to append the later part (awk) with filename is because filenames are coming from Watch node.

Waqas Aslam

unread,
Dec 4, 2017, 10:51:24 AM12/4/17
to Node-RED
seems like that's the path I should follow :)
thanks
Reply all
Reply to author
Forward
0 new messages