# Node config parameter not following default value

**URL:** <https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393>\
**Category:** Developing Nodes\
**Created:** [2 December 2023 21:03 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393 "2023-12-02T21:03:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [2 December 2023 21:03 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/1 "2023-12-02T21:03:26Z")

</div>

Hey there! I'm a bit clueless and hope you may be able to help me.

It concerns one of my nodes, node-red-contrib-smarthome-powerswitch. It's something pretty simple. Until now, the output's topic was always `command` and the payload either boolean `true` or `false`. So in the HTML file, I added:

```javascript
RED.nodes.registerType('powerswitch',{
    category: 'Smart Home',
    color: '#C0DEED',
    defaults: {
        ...
        outputPayloadOnType: {value: "initval"},
        outputPayloadOn: {value: true},
        outputPayloadOffType: {value: "initval"},
        outputPayloadOff: {value: false}
        },
    ...
    oneditprepare: function() {
        ...
        if (this.outputPayloadOnType === 'initval') {
            $("#node-input-outputPayloadOnType").val('bool');
        }
        if (this.outputPayloadOffType === 'initval') {
            $("#node-input-outputPayloadOffType").val('bool');
        }
        ...
        $("#node-input-outputPayloadOn").typedInput({
            default: 'bool',
            typeField: $("#node-input-outputPayloadOnType"),
            types:['str','num','bool']
        });
        $("#node-input-outputPayloadOff").typedInput({
            default: 'bool',
            typeField: $("#node-input-outputPayloadOffType"),
            types:['str','num','bool']
        });
        ...

```

The idea was to have the initial values the same as the hardcoded values have been before the update. That is: boolean `true` for payload On and boolean `false` for payload Off.

Basically everything works. The only thing bothering me is that as soon as I update the node to the latest development version, it shows both boolean `true` for payload On **and** payload Off, as shown below:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/9/c958c279a4a2770787ec2d04b89db01ef017727b.png)

This is inacceptable since after an update, the behaviour would be different than before the update.

The question is: Why does the field "Off Payload" also show `true`, eventhough it has a declared `false`as default value (`outputPayloadOff: {value: false}`)?

Link to the code before the update: [GitHub - danube/node-red-contrib-smarthome-powerswitch at v1.0.11](https://github.com/danube/node-red-contrib-smarthome-powerswitch/tree/v1.0.11)  
Link to the node after the update: [GitHub - danube/node-red-contrib-smarthome-powerswitch at v1.1.0](https://github.com/danube/node-red-contrib-smarthome-powerswitch/tree/v1.1.0)

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [3 December 2023 09:16 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/2 "2023-12-03T09:16:00Z")

</div>

Hi @danube,  
Perhaps I misunderstood this.

If you want `bool` as the default type why not define it directly in `node.defaults`?

I don't really understand what you mean by update? Did you remove your old node and use a new one (after upgrading), or do you just update? In the latter case, the default values no longer work because your node properties already exists. If Off payload is true before updating, the value will still be true after updating.

---

<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:** [3 December 2023 10:44 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/3 "2023-12-03T10:44:11Z")

</div>

> [@danube](#):
>
> ```auto
> RED.nodes.registerType('powerswitch',{
> category: 'Smart Home',
> color: '#C0DEED',
> defaults: {
> ...
> outputPayloadOnType: {value: "bool"},
> outputPayloadOn: {value: true},
> outputPayloadOffType: {value: "bool"},
> outputPayloadOff: {value: false}
> },
> ...
> oneditprepare: function() {
> ...
> /* get rid of this ↓
> if (this.outputPayloadOnType === 'initval') {
> $("#node-input-outputPayloadOnType").val('bool');
> }
> if (this.outputPayloadOffType === 'initval') {
> $("#node-input-outputPayloadOffType").val('bool');
> }
> get rid of this ↑ */
> ...
> $("#node-input-outputPayloadOn").typedInput({
> // default: 'bool', try without this
> typeField: $("#node-input-outputPayloadOnType"),
> types:['str','num','bool']
> });
> $("#node-input-outputPayloadOff").typedInput({
> // default: 'bool', try without this
> typeField: $("#node-input-outputPayloadOffType"),
> types:['str','num','bool']
> });
> 
> ```

That should make it work "normally" (or I am misunderstanding your intent)

---

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [3 December 2023 20:36 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/4 "2023-12-03T20:36:04Z")

</div>

@GogoVega with "updating" I mean updating from a previous node version (1.0.10) to a newer one (1.1.0). Where the new version contains configuration parameters `outputPayloadOn` and `outputPayloadOff` which simply did not exist in the previous version but have been introduced in the new version.

Both of you @GogoVega and @Steve-Mcl suggest to clean the code accordingly. I don't know where I had the intention to use "initval". I remember when I learned to develop nodes, I looked into existing ones, so maybe I copied that from somewhere.

Whatsoever, the problem persists. The new code now is:

```javascript
defaults: {
  ...
  outputPayloadOnType: {value: "bool"},
  outputPayloadOn: {value: true},
  outputPayloadOffType: {value: "bool"},
  outputPayloadOff: {value: false}
}
...
oneditprepare: function() {
  ...
  $("#node-input-outputPayloadOn").typedInput({
    typeField: $("#node-input-outputPayloadOnType"),
    types:['bool','str','num']
  });
  $("#node-input-outputPayloadOff").typedInput({
    typeField: $("#node-input-outputPayloadOffType"),
    types:['bool','str','num']
  });
  ...
}

```

Still, the dropdown in the UI configuraion shows `true` in both `outputPayloadOn` and `outputPayloadOff` parameters. Any clue?? 🤔🤔

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [3 December 2023 20:48 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/5 "2023-12-03T20:48:08Z")

</div>

If you add a new node, is Off Payload still true?

---

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [3 December 2023 20:50 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/6 "2023-12-03T20:50:44Z")

</div>

I should have mentioned that already, cause that seems to be a point... If I add it as a new node from the palette, `outputPayloadOff` indeed is false. Hence, this seems to affect only existing (and so, updated) nodes.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [3 December 2023 20:55 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/7 "2023-12-03T20:55:54Z")

</div>

If `outputPayloadOn` is a new property, you don't have to do anything, the default value will be used. But you can check if `node.outputPayloadOn` exists then act accordingly  
I believe your value is true because you tested in dev, so you added this value to your node

---

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [3 December 2023 21:07 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/8 "2023-12-03T21:07:07Z")

</div>

> [@GogoVega](#):
>
> If `outputPayloadOn` is a new property, you don't have to do anything, the default value will be used.

The thing is, this seems not to be true! The new parameters have both been given a default value.

```auto
  outputPayloadOn: {value: true},
  outputPayloadOff: {value: false}

```

Now these default values are present if the node is being newly created from the palette. But if you're coming from a previous version of the node (where those two new parameters have not yet been introduced) and updating to the new version (with the two new parameters), it shows `true` in both parameters. Now I'm concerned about those who update and end up with a non working node unless the parameter has been changed manually.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2023 21:21 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/9 "2023-12-03T21:21:37Z")

</div>

> [@GogoVega](#):
>
> If `outputPayloadOn` is a new property, you don't have to do anything, the default value will be used

Not quite

Default values are only applied for newly created nodes introduced into the flow - not for nodes that are already deployed.

Defaults are applied when a node is dragged into the editor - at least that is my experience.

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [3 December 2023 21:25 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/10 "2023-12-03T21:25:03Z")

</div>

If there are new properties, the default values (of these properties) will be used but a deployment (and therefore a modification of the node) is necessary.

You can test it by exporting your node: look before then after modifying it.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2023 21:39 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/11 "2023-12-03T21:39:13Z")

</div>

No.

Defaults are only used when a new node has not had its configuration written yet.

Here is an example.

1. Drag in a `delay` node you can see the default is 5 seconds.
2. Import the below.  
Its a `delay` node where it doesn't have (lets pretend a timeout set) - I removed it from the JSON

```auto
[{"id":"e041bf92aea566c9","type":"delay","z":"2d7bf6e3.84c97a","name":"","pauseType":"delay","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":160,"y":695,"wires":[[]]}]

```

You will see the default of 5 is not set, Evan though it is default, in the HTML definition.  
Evan if you open up the node after a deploy - it still does not set the default.

I have only witnessed the defaults being set when dragged into the editor, and it seems that is the only time defaults are used

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [3 December 2023 21:47 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/12 "2023-12-03T21:47:02Z")

</div>

Strange, need to check why 🤔

So check if `node.outputPayloadOn` exists then set value

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2023 21:50 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/13 "2023-12-03T21:50:31Z")

</div>

> [@GogoVega](#):
>
> So check if `node.outputPayloadOn` exists then set value

Yup!

For new properties I have introduced in my nodes, after they have been deployed, I have to do this - by asking users to open up the node, then save/deploy - so I can check and populate the value in `oneditprepare` if its not set.

But for new users, they don't - as the defaults will kick in when dragged into the flow.

But in reality - I tend to save these changes for major version bumps, as technically they are breaking changes - especially if the new values are depended on

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [3 December 2023 22:18 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/14 "2023-12-03T22:18:58Z")

</div>

> [@danube](#):
>
> Now I'm concerned about those who update and end up with a non working node unless the parameter has been changed manually

If these new properties are required for your logic, the “correct” way is to major version bump, and provide the documentation that explains how to fix/address the breaking change.

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [3 December 2023 22:59 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/15 "2023-12-03T22:59:32Z")

</div>

> [@danube](#):
>
> Now these default values are present if the node is being newly created from the palette

Hi @danube,  
Indeed when you add a new property to a node, that won't exist on existing nodes. So you always have to deal with 2 cases:

- html file: When the user opens the config screen of such an old node, you have to set explicit (with a js statement) the default value if the property does not exist yet, and show that default value.
- js file: When the user does _NOT_ open the config screen, then the backend of your code also needs to set the default value if the property does not exist yet. To make sure that existing nodes process input messages correctly.

See more info [here](https://discourse.nodered.org/t/upgrading-nodes-specifically-my-own/2641/11).

Bart

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [4 December 2023 09:13 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/16 "2023-12-04T09:13:43Z")

</div>

This has transitioned to a Feature Request for further discussion.

[FR: Populate default value to new node property even if the node already exists - Core Development / Feature Requests - Node-RED Forum (nodered.org)](https://discourse.nodered.org/t/fr-populate-default-value-to-new-node-property-even-if-the-node-already-exists/83414)

---

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [4 December 2023 21:13 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/17 "2023-12-04T21:13:30Z")

</div>

I did some crossreading through the links you folks provided, thank you so much for that. I accept that thinks are as they are, at least for the moment. Nevertheless, I strongly vote for the statement that defaults should be given priority for newly introduced properties.

Now, I simply don't understand yet, why both my parameters show `true` as selected value after I open the node properties the first time after updating. They both may also show `false`, so there's for sure some explaination (which at this point I'm secondarily interested in...).

Now I'm forced to find a way around this behavior anyways, no matter if there will ever be a core change regarding that. @GogoVega in one of your posts you provided an approach I also had in mind:

```javascript
oneditprepare: function() {
    console.log("DEBUG: VALUE IS " + this.outputPayloadOff)
    if (this.outputPayloadOff === undefined) {
        $('#node-input-outputPayloadOff').val('FALSE');
    }
}

```

I expected that if the configuration has been opened the first time after an update (when the newly introduced parameter still does not exist hence is `undefined`) the appropriate parameter value will be set to `false`. I added a console.log to verify this.

Now the first time I open the configuration, I (weehaa!) get:

```plaintext
DEBUG: VALUE IS undefined

```

JQuery should set the value to `false` but in the boolean dropdown it still shows `true`. I acknowledged that by okaying the dialogue and at second opening I (boooh!) get:

```plaintext
DEBUG: VALUE IS true

```

I understand that I also have to capture that undefined in JS in the case the node properties have never been opened, but seems I'm stuck here already. Any clue, why my value is not being set to `false`?

---

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [5 December 2023 07:42 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/18 "2023-12-05T07:42:02Z")

</div>

I slept on it. The behavior makes sense. With `$('#node-input-outputPayloadOff').val('FALSE');` I'm trying to set the value of the new property `outputPayloadOff`, which simply doesn't exist yet at the time of `oneditprepare`. It is only created when the user confirms the configuration dialog, but then it simply makes no more sense to check it against `undefined` as it has been set to `true` by whatever mechanism.

I'm really stuck here. If I introduce this update, **careless users** will end up in a situation where the installation doesn't work.

Now I am not afraid of those who simply update without opening the configuration. In JS I can check if the parameters are available or not and work accordingly.

I am more concerned about those who update the node and, at some later point, open the configuration and edit something differently without paying attention about those new parameters at the very bottom. Once deployed, the installation **will** behave problematically.

Raising the major version feels more like a poor excuse to me.

Any idea how to work around this?

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [5 December 2023 07:45 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/19 "2023-12-05T07:45:57Z")

</div>

Hi,  
Should be:

```js
$('#node-input-outputPayloadOff').val('false');
//or
$('#node-input-outputPayloadOff').typedInput('value', 'false');

```

EDIT: It's a string

---

<div class="post-metadata">

**Author:** ![danube](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/danube/32/34348_2.png) [@danube](https://discourse.nodered.org/u/danube)\
**Post date:** [5 December 2023 08:03 UTC](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393/20 "2023-12-05T08:03:44Z")

</div>

Are you up 24/7??

`$('#node-input-outputPayloadOff').typedInput('value', 'false')` is the working solution where the upper suggestion `$('#node-input-outputPayloadOff').val('false')`, for whatever reason, does not work. Now that's the workaround of my choice. Still, it feels like a workaround and I honestly think that defaults should apply also to newly introduced parameters.

Thanks to all of you for your efforts, @GogoVega for the workaround code.

EDIT: Doesn't matter if `false` or `'false'`, both work. 😉

[Next page](https://discourse.nodered.org/t/node-config-parameter-not-following-default-value/83393.md?page=2)
