# Help with how to better construct this message using GLOBAL context

**URL:** https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332
**Category:** General
**Created:** [7 February 2026 03:37 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332 "2026-02-07T03:37:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [7 February 2026 03:37 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/1 "2026-02-07T03:37:24Z")

</div>

Ok: me being anal, or not. 🤷

I have this in an `exec` node:  
`tail -n11 /media/pi/9020-9C27/events/commands_received.txt | cat`  
Which is fair enough.

But I also have this in `global context`:  
`event_paths` as `/media/pi/9020-9C27/events/` and  
`log_paths` as `/media/pi/9020-9C27/logs/` (Ok not needed here, but....)

So I am wanting to _translate_ the `event_paths` into the command.

(As best I can explain it showing the _structure_)

`tail -n11 *globalContext_even_paths*commands_received.txt | cat`

The nodes:

```auto
[{"id":"74647831.e6bf18","type":"inject","z":"a3bd09a.b852978","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":210,"wires":[["88ec09d0.6e1c28"]]},{"id":"88ec09d0.6e1c28","type":"exec","z":"a3bd09a.b852978","command":"ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Cat","x":470,"y":210,"wires":[["17917965.d43657","87a8c9d9.4f7f58"],[],[]]}]

```

Any ideas please?

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [7 February 2026 09:12 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/2 "2026-02-07T09:12:48Z")

</div>

Why not construct your command line in a function or template node and pass the whole lot to exec as msg.payload, leaving `Command` in the exec node blank?

Incidentally, I'm pretty sure that your trailing `| cat` does nothing useful, though it does no harm either.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [7 February 2026 09:28 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/3 "2026-02-07T09:28:29Z")

</div>

I guess that would be the simple way.

Thanks.

I seem to have this ability to make simple things difficult for myself more times than I should.

I'll give it a go.

---

<div class="post-metadata">

### Author: ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)
#### Post date: [7 February 2026 14:49 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/4 "2026-02-07T14:49:22Z")

</div>

> [@Trying\_to\_learn](#):
>
> /media/pi/9020-9C27/events/

Or

```auto
[{"id":"88ec09d0.6e1c28","type":"exec","z":"53a4dcf62732d6d9","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"Cat","x":1000,"y":1260,"wires":[["e3a8afb54bab0451"],["e3a8afb54bab0451"],["e3a8afb54bab0451"]],"info":"\"ls -1 --file-type \" & $globalContext(reboot) & \" | grep -v '/' | sed s/@$// | cat\""},{"id":"f0ea16b4c993cc86","type":"inject","z":"53a4dcf62732d6d9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"ls -1 --file-type \" & $globalContext(\"reboot\") & \" | grep -v '/' | sed s/@$// | cat\"\t","payloadType":"jsonata","x":830,"y":1260,"wires":[["88ec09d0.6e1c28"]],"info":"\"ls -1 --file-type \" & $globalContext(reboot) & \" | grep -v '/' | sed s/@$// | cat\"\r\n\"tail -n11 \" & $globalContext(\"event_paths\") & \"commands_received.txt | cat\""}]

```

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [7 February 2026 19:50 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/5 "2026-02-07T19:50:01Z")

</div>

(Mind blown.)

But it is (at this point in time) _MAGIC_.

The command  
`"ls -1 --file-type " & $globalContext("reboot") & " | grep -v '/' | sed s/@$// | cat"	`  
is way beyond my skill set. (Not your problem)

But I can't draw a comparison to the _original_ command:  
`tail -n11 *globalContext_even_paths*commands_received.txt | cat`

`ls` vs `tail` They are two very different commands.

I'll run off and do some digging and see what I learn.

But thanks.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [7 February 2026 20:05 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/6 "2026-02-07T20:05:12Z")

</div>

# IGNORE THIS REPLY Sorry, I goofed.

Left in because I'm dumb.

Ok, sorry I didn't declare this at the start.

The command is being _used_ in a `watch` node.  
Monitoring file activity in the given directory.

Alas the `watch` node (both file and directory) doesn't have an input.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [7 February 2026 20:12 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/7 "2026-02-07T20:12:31Z")

</div>

> [@Trying\_to\_learn](#):
>
> `ls` vs `tail` They are two very different commands.

In your original post you talked about  
`tail -n11 /media/pi/9020-9C27/events/commands_received.txt | cat`  
which as you know prints the last 11 lines in the file.

But in your sample flow the exec node is set to run  
`ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat`  
which lists the files in the reboot directory, ignoring subdirectories and stripping any @ symbols off the end of the filename.

So they are indeed very different commands. 😄  
Which one are you trying to parameterise with the global context variable?

Maybe you should share a bit more of the flow (not all of it please!)

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [7 February 2026 20:26 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/8 "2026-02-07T20:26:02Z")

</div>

You are right.

I got confused. So many things happening.

Ignoring my incorrect post (number 5)....

I am wanting to display the last few lines of the reboot log file.

The _rub_ (for me) is that I am entering the entire path - which is fixed.

Small part of the code:

```auto
[{"id":"74647831.e6bf18","type":"inject","z":"a3bd09a.b852978","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":210,"wires":[["88ec09d0.6e1c28"]]},{"id":"88ec09d0.6e1c28","type":"exec","z":"a3bd09a.b852978","command":"ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Cat","x":470,"y":210,"wires":[["17917965.d43657","87a8c9d9.4f7f58"],[],[]]},{"id":"17917965.d43657","type":"split","z":"a3bd09a.b852978","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":670,"y":200,"wires":[["f8e85ffb.27a12","a2f4ed08.9b76d"]]},{"id":"f8e85ffb.27a12","type":"switch","z":"a3bd09a.b852978","name":"","property":"payload","propertyType":"msg","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":670,"y":250,"wires":[["8d28b379.4fc7b","4df38dde.8dd85c"]]},{"id":"4df38dde.8dd85c","type":"debug","z":"a3bd09a.b852978","name":"3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":840,"y":250,"wires":[]}]

```

The `switch` node may not be needed. It is included .... yeah. I'm not sure.  
But as it, it works.  
I think it is to handle the case if the file is empty. But I 'm not sure it is the right way of doing it.

## So....

The _problem_ is that if ever I change the USB stick to another one.

I just edit the `global context` and don't need to also edit this node. (And others like it)

Probably as suggested in reply 2 I do it that way.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [7 February 2026 20:35 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/9 "2026-02-07T20:35:44Z")

</div>

Ok, expansion on things.

Here is another (sorry) bit of the code.

```auto
[{"id":"74647831.e6bf18","type":"inject","z":"a3bd09a.b852978","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":310,"y":210,"wires":[["88ec09d0.6e1c28"]]},{"id":"88ec09d0.6e1c28","type":"exec","z":"a3bd09a.b852978","command":"ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"Cat","x":470,"y":210,"wires":[["17917965.d43657","87a8c9d9.4f7f58"],[],[]]},{"id":"17917965.d43657","type":"split","z":"a3bd09a.b852978","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":670,"y":200,"wires":[["f8e85ffb.27a12","a2f4ed08.9b76d"]]},{"id":"a2f4ed08.9b76d","type":"debug","z":"a3bd09a.b852978","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":840,"y":200,"wires":[]},{"id":"f416f19bd14acba1","type":"inject","z":"a3bd09a.b852978","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1010,"y":140,"wires":[["3b8e8db1663c4cfe"]]},{"id":"3b8e8db1663c4cfe","type":"change","z":"a3bd09a.b852978","name":"command","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"ls -1 --file-type \" & $globalContext(\"reboot\") & \" | grep -v '/' | sed s/@$// | cat\"\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1180,"y":140,"wires":[["f84266302b2910d0"]]},{"id":"f84266302b2910d0","type":"exec","z":"a3bd09a.b852978","command":"","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":1400,"y":140,"wires":[["17917965.d43657"],[],[]]}]

```

Desired.

 ![Screenshot from 2026-02-08 07-31-39](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/3/53bab27a0b8e083c92ea1d07600d0fbd32f40eff.png)

What the new code gives me.

 ![Screenshot from 2026-02-08 07-31-49](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/1/b14fae9357837792a00a5bcc81d3db9df8a82081.png)

I'll see if I can work out where the problem is.  
I'm guessing it is getting a list of all files rather than the one I want.

# AH!

Removing the wire between the `change` and the `exec` node (on the right) and replacing it with a `debug` I get this strange output:

 ![Screenshot from 2026-02-08 07-41-09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e91fb628eb7ee23b9e66cbeabde78403af47499.png)

But the _original_ command in the_original_ `exec` node is this command:  
`ls -1 --file-type /media/pi/9020-9C27/logs/reboot/ | grep -v '/' | sed s/@$// | cat`

Slight difference.

Confused.

# Ah2!

Is this this what should be in the `change` node?  
`"ls -1 --file-type " & $globalContext("log_path") & "reboot | grep -v '/' | sed s/@$// | cat"	`

That seems to be giving me the expected result.

## RESOLVED!

Thanks folks.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [21 February 2026 20:35 UTC](https://discourse.nodered.org/t/help-with-how-to-better-construct-this-message-using-global-context/100332/10 "2026-02-21T20:35:46Z")

</div>

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