Initial state of custom button modelled on Debug node

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:

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:

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?

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

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"}});
    }
}

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

I think I will have that inscribed on my tombstone :slight_smile:

1 Like

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