# Initial state of custom button modelled on Debug node

**URL:** <https://discourse.nodered.org/t/initial-state-of-custom-button-modelled-on-debug-node/52603>\
**Category:** Developing Nodes\
**Created:** [23 October 2021 08:18 UTC](https://discourse.nodered.org/t/initial-state-of-custom-button-modelled-on-debug-node/52603 "2021-10-23T08:18:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris.willing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chris.willing/32/76579_2.png) [@chris.willing](https://discourse.nodered.org/u/chris.willing)\
**Post date:** [23 October 2021 08:18 UTC](https://discourse.nodered.org/t/initial-state-of-custom-button-modelled-on-debug-node/52603/1 "2021-10-23T08:18:45Z")

</div>

N.B nothing to do with dashboard!

I've created a node to work like a push button switch which (de)activates a facility in a connected physical device. The idea comes from the switch of the common Debug node which deactivates or activates the debug output. My node is configured as a toggle button whose onclick function injects a custom "start" or "stop" message back to js code using the RED.httpAdmin.post facility.

It all works perfectly. My problem is that when the flow restarts, the initial state of the button is always "in" i.e. like the deactivated state of a Debug node.

How can I make it start in the activated state?

Here's the relevant code from the node's configuration:

```auto
buttonState: {value: true},
button: {
    toggle: "buttonState",
    onclick: function() {
        console.log("ClICK " + this.buttonState);
        doInject(this, {"payload":{"action": this.buttonState?"stop":"start"}});
    }
}

```

where doInject() is an abbreviated version of the doInject function from the Inject node:

```auto
function doInject(node, customMsg) {
    console.log("doInject: " + JSON.stringify(customMsg));
    $.ajax({
        url: "xkeys_led_inject/" + node.id,
        type: "POST",
        data: JSON.stringify(customMsg),
        contentType: "application/json; charset=utf-8"
    });
}

```

I don't think there's anything controversial in there. Of course I've tried both true and false as the initial value of buttonState but the problem remains that the initial state always appears as deactivated.

Any ideas on how to set the initial state?

---

<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:** [23 October 2021 09:39 UTC](https://discourse.nodered.org/t/initial-state-of-custom-button-modelled-on-debug-node/52603/2 "2021-10-23T09:39:54Z")

</div>

> [@chris.willing](#):
>
> `buttonState: {value: true},`

That needs to be inside the `defaults` object of your node...

```auto
defaults: {
   buttonState: {value: true},
   // your other node properties
},
button: {
    toggle: "buttonState",
    onclick: function() {
        console.log("ClICK " + this.buttonState);
        doInject(this, {"payload":{"action": this.buttonState?"stop":"start"}});
    }
}

```

---

<div class="post-metadata">

**Author:** ![chris.willing](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chris.willing/32/76579_2.png) [@chris.willing](https://discourse.nodered.org/u/chris.willing)\
**Post date:** [23 October 2021 09:51 UTC](https://discourse.nodered.org/t/initial-state-of-custom-button-modelled-on-debug-node/52603/3 "2021-10-23T09:51:12Z")

</div>

Thanks, works well now. I should have read the docs more carefully.

---

<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:** [23 October 2021 10:14 UTC](https://discourse.nodered.org/t/initial-state-of-custom-button-modelled-on-debug-node/52603/4 "2021-10-23T10:14:40Z")

</div>

> [@chris.willing](#):
>
> I should have read the docs more carefully.

I think I will have that inscribed on my tombstone 🙂

---

<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:** [6 November 2021 10:15 UTC](https://discourse.nodered.org/t/initial-state-of-custom-button-modelled-on-debug-node/52603/5 "2021-11-06T10:15:10Z")

</div>

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