# \[feature request\] 23-watch, enable configuration via context variables (global. / flow. / env.)

**URL:** <https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685>\
**Category:** Feature Requests\
**Created:** [4 December 2024 12:04 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685 "2024-12-04T12:04:52Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschlgl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mschlgl/32/55640_2.png) [@mschlgl](https://discourse.nodered.org/u/mschlgl)\
**Post date:** [4 December 2024 12:04 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/1 "2024-12-04T12:04:52Z")

</div>

Currently the watch-list needs to be configured in the node editor. Would be nice to have the possibility to configure via a context variable to enable flow reuse without having to edit individual nodes.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 December 2024 13:35 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/2 "2024-12-04T13:35:50Z")

</div>

Are the fields free text entry? If yes, you can use `${ENV_VAR}`

[https://nodered.org/docs/user-guide/environment-variables](https://nodered.org/docs/user-guide/environment-variables)

---

<div class="post-metadata">

**Author:** ![mschlgl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mschlgl/32/55640_2.png) [@mschlgl](https://discourse.nodered.org/u/mschlgl)\
**Post date:** [5 December 2024 07:23 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/3 "2024-12-05T07:23:47Z")

</div>

Hi Steve, that's a good hint will check on this.  
(although with my particular setup [node-red as a service within IOBroker] I'd need to access a global variable)  
On the other hand would be just a few lines of code to add to use the typed input widget within the watch node editor giving all possibilities (str, env, flow, global):  
(at least this works for me)

changes to 23-watch.html:  
...

```auto
<script type="text/javascript">
    RED.nodes.registerType('watch',{
        category: 'storage',
        defaults: {
            name: {value:""},
            files: {value:"",required:true,
                    label:RED._("node-red:watch.label.files")},
                    // <!-- added to enable context setup -->
                    filesType: {value: "str"},
            recursive: {value:""}
        },
        color:"BurlyWood",
        inputs:0,
        outputs:1,
        icon: "watch.svg",
        label: function() {
            return this.name||this.files||this._("watch.watch");
        },
        labelStyle: function() {
            return this.name?"node_label_italic":"";
        },
        // <!-- added to enable context setup -->
        oneditprepare: function() {
            $("#node-input-files").typedInput({
                default: 'str',
                types: ['str','flow','global','env'],
                typeField: $("#node-input-filesType")
            });
        }
    });
</script>

```

...

changes to 23-watch.js:  
...

```auto
function WatchNode(n) {
        RED.nodes.createNode(this,n);

        this.recursive = n.recursive || false;
        // added to enable context setup
        switch (n.filesType) {
            case "global":
                n.files = this.context().global.get(`${n.files}`);
                break;
            case "flow":
                n.files = this.context().flow.get(`${n.files}`);
                break;
            case "env":
                n.files = this.context().env.get(`${n.files}`);
            break;
        };
        this.files = (n.files || "").split(",");
        for (var f=0; f < this.files.length; f++) {
            this.files[f] = this.files[f].trim();
        }
        this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files);
        const node = this;

        const watcher = watch(this.files, { recursive: this.recursive });

        watcher.on('change', function (event, fpath) {
            const file = path.basename(fpath)
            let stat;
            try {
                stat = fs.statSync(fpath);
            } catch(e) { }
            let type = "none";
            const msg = {
                payload:fpath,
                topic:node.p,
                file:file,
                filename:fpath,
                event: event
            };
            if (stat) {
                if (stat.isFile()) { type = "file"; msg.size = stat.size; }
                else if (stat.isBlockDevice()) { type = "blockdevice"; }
                else if (stat.isCharacterDevice()) { type = "characterdevice"; }
                else if (stat.isSocket()) { type = "socket"; }
                else if (stat.isFIFO()) { type = "fifo"; }
                else if (stat.isDirectory()) { type = "directory"; }
                else { type = "n/a"; }
            }
            msg.type = type;
            node.send(msg);
        });

        watcher.on('error', function (error) {
            const msg = { payload: "" };
            node.error(error,msg);
        });

        this.close = function() {
            watcher.close();
        }
    }

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 December 2024 08:33 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/4 "2024-12-05T08:33:58Z")

</div>

Using context variables would not be appropriate here as their value can change, but the watch node only picks up the value at the point in time the flow starts. We don't use context values for this type of static configuration anywhere else.

Adding support for the env type is certainly feasible, as env values are static. As Steve says, you can already make use of them via the syntax he shared, but adding a typed input to make it more explicit is doable.

---

<div class="post-metadata">

**Author:** ![mschlgl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mschlgl/32/55640_2.png) [@mschlgl](https://discourse.nodered.org/u/mschlgl)\
**Post date:** [5 December 2024 10:00 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/5 "2024-12-05T10:00:12Z")

</div>

OK, agreed. (In fact was expecting that kind of answer 😉 )  
I'm using the same flow setup in multiple projects, reading a set of global variables from a config file at startup. So in my case those context variables will be static.

Maybe should contribute a 'dynamic' watch wich will close and restart the underlying fs.watch on configuration changes? With configuration check triggered by incoming msg or even new configuration coming in by msg?  
I'm just not sure of consequences of configuration changes at a high msg rate. (which shouldn't occur if used in a reasonable way. But you never know)  
Any thoughts?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [5 December 2024 10:18 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/6 "2024-12-05T10:18:59Z")

</div>

If you did make it a typed input then you could add msg. as an option and if selected use that to add an input pin to the node so it could accept inputs. You are right to think about rates but hopefully you should not change what you are watching that rapidly... but you could check to see if the new value is same as old value (If for some reason they get sent in repeatedly) and ignore in that case... but... 🤷  
Would be a good PR.

---

<div class="post-metadata">

**Author:** ![mschlgl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mschlgl/32/55640_2.png) [@mschlgl](https://discourse.nodered.org/u/mschlgl)\
**Post date:** [5 December 2024 10:36 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/7 "2024-12-05T10:36:17Z")

</div>

Would you like to see a new node (e.g. node-red-contrib-dynwatch) or a enhanced core node?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [5 December 2024 10:49 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/8 "2024-12-05T10:49:07Z")

</div>

A pr to node-red is my preference (everyone gains) but the choice is yours.

---

<div class="post-metadata">

**Author:** ![mschlgl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mschlgl/32/55640_2.png) [@mschlgl](https://discourse.nodered.org/u/mschlgl)\
**Post date:** [10 December 2024 09:26 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/9 "2024-12-10T09:26:13Z")

</div>

see commit ID  
95480bc6feb64bac6e640f904da0c3c02e95480d  
"configurable watch node"  
not sure how to do a proper PR

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 December 2024 09:39 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/10 "2024-12-10T09:39:21Z")

</div>

Not sure where that code is  
but this may help.  
[https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request)

---

<div class="post-metadata">

**Author:** ![mschlgl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mschlgl/32/55640_2.png) [@mschlgl](https://discourse.nodered.org/u/mschlgl)\
**Post date:** [10 December 2024 10:46 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/11 "2024-12-10T10:46:00Z")

</div>

OK, see PR  
configurable watch node #4986

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 December 2024 11:10 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/12 "2024-12-10T11:10:20Z")

</div>

Link to PR: [configurable watch node by mschlgl · Pull Request #4986 · node-red/node-red · GitHub](https://github.com/node-red/node-red/pull/4986)

@mschlgl please see the issues noted against the PR.

---

<div class="post-metadata">

**Author:** ![mschlgl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mschlgl/32/55640_2.png) [@mschlgl](https://discourse.nodered.org/u/mschlgl)\
**Post date:** [16 December 2024 08:13 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/13 "2024-12-16T08:13:31Z")

</div>

All resolved

---

<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:** [14 February 2025 08:13 UTC](https://discourse.nodered.org/t/feature-request-23-watch-enable-configuration-via-context-variables-global-flow-env/93685/14 "2025-02-14T08:13:40Z")

</div>

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