Enable - disable node output

Hi ,
I want to be able to connect a fetch statement on the disabling or enabling of a node. Is there something similar like oneditprepare or oneditsave where I can check if this node is been disabled or enabled?

My asset management system is registering if a certain "asset" is active or inactive. It would be super sweet if I could connect the enable , disable functionality in stead of making another dropdown menu for it.

I understand that the fetch action only gets executed on deploy but that's ok for me.

Can you be more specific where you want to check the status of a node. e.g. Do you have a plugin? You can check the status in oneditsave. (By status I mean enabled/disabled).

Yeah I think I have the variable:
RED.settings.runtimeState right?

that should give me the true or false.

This won't help you. What are you trying to do?

Ok there is a couple of things that I want to do that wont seem to work atm:

  • Get variables from the settings.js file
  • Get the runtimeState so I can pass the state on to another cloud system that can see that this node is actually enabled or disabled.
  • get the name of the activeTab

all of those don't work haha undefined or non existing. In my settings I have

    functionGlobalContext: {
        locationId: "1",
        uuid: "1",
        // os:require('os'),
    },
exportGlobalContextKeys: true,

The variables from the settings.js file are fetched using:

oneditprepare: function() {

      const node = this;

    // Fetch settings
    $.getJSON('settings', function(settings) {
      try {
      console.log(settings.functionGlobalContext);
      // Check if UUID exists in settings
      const uuid = (settings.functionGlobalContext && settings.functionGlobalContext.uuid) ? settings.functionGlobalContext.uuid : 'default-uuid';
      
      // Use UUID
      console.log('UUID:', uuid);
      } catch (error) {
      console.error('Error fetching settings:', error);
      }
    });


      // Define API URLs for single source of truth configuration but also provide a backup for local testing and situations where the cloud is not available
      // Find the active tab name by inspecting the DOM
      const activeTabElement = $('.red-ui-workspace-active');
      node.processId = activeTabElement.attr('data-id') || activeTabElement.text();

      node.configUrls = assetUtils.getAssetVariables();

      console.log("assetLocationId", node.locationId);
      console.log("assetProcessId", node.processId);
      console.log("configUrls", node.configUrls);

      //make node specific uuid
      node.uuid = uuid+node.id;
  
      console.log(`------------ Preparing to edit measurement node with uuid : ${node.uuid}------------`);

Are you trying to read runtimeState directly from settings? That's a static prop only essentially enabling this API call: GET /flows/state : Node-RED that will tell you if flows are running or not.
If you'd be interested in runtimeState you'd have to set this prop to true in settings.json but then use API to get the state value.

But this is not something you want I presume. I also have a bit of trouble understanding what exactly are you trying to achieve?
You have a custom node and you want to detect if that node gets disabled? or if some other node gets disabled?

Yes I have a custom node and I want this node to see if it gets disabled or enabled and do some kind of action when it is.

On top of that I want to access a global declared static variable from inside the settings.js and read it in my html environment of my custom node. I got as far as being able to access the variable in my .js file by calling:

    const globalContext = this.context().global;
    const locationId = globalContext.get("locationId") || "No location ID set";
      
    // Store it in the node instance?
    config.locationId = locationId;

however when I access this in my HTML file under this.locationId it remains undefined.

So if someone can refer me to a manual on how to get those values please do.

This won't work, your node at runtime (server) part and your node in editor are different objects. They don't share properties. So if you assign something to node object serves side it's not available on node object in browser.

Easiest is to create an API endpoint in your server side node code and and fetch required data from client side.
Check core nodes for this approach, e.g. debug node.

Or check this example where I create a GET endpoint in runtime: node-red-log-io/log-io.js at master · sebenik/node-red-log-io · GitHub
And call it from editor to retrieve json object: node-red-log-io/log-io.html at master · sebenik/node-red-log-io · GitHub

1 Like

To check if node is disabled, it should had this d prop set to true.

How do I access this then from within my oneditprepare function its not clear from your snippet.
There is no prop called this.d
Ah I assume this is from the output from the json structure. I tried to access d from within the html file but helas no success.

thanks for the clarification.

If prop is not there it means it's enabled.
d: false is not on the object - you'll only get d: true

so just do

if (node.d) {
  // it's disabled
}
1 Like

I was reading your post again and I realised why it was undefined the whole time... my god thanks for your patience.

I just understood that offcourse that its not there so its undefined when its enabled...

console.log("Is this node disabled?", node.d);

This works when its disabled but yields undefined when its enabled... haha
ugh just wasted good a good portion of my life realizing this.

But we learn most from mistakes and time we invest into something, so you certainly didn't waste it :wink:

1 Like

Ive done this differently btw. Im importing a custom settings file into the html to fetch and build my uuid etc...
This doesnt then load on runtime but I dont need it to. All my nodes create and fetch the needed vars when registered / prepared which is also fine.
So in a sense it works like suggested through a fetch but not using the node red core method. I find it too rigid/complex.
In the end im using this:

<script type="module">
  import * as menuUtils from "/generalFunctions/helper/menuUtils.js";
  import * as assetUtils from "/generalFunctions/helper/assetUtils.js";
  import * as projectSpecificSettings from "/generalFunctions/settings/projectSpecificSettings.js";

  RED.nodes.registerType("measurement", {

    category: "digital twin",
    color: "#e4a363",

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