Skip to content

Button Switch ​

The ui-btn-switch widget shows a card with a title (Button label) above two buttons, one for the false state and one for the true state; the button of the current state is highlighted. Clicking one of the two buttons selects that state; clicking elsewhere on the card, or pressing Enter or Space on it, toggles the state. Clicking the button of the current state does nothing. Each change made by a user is sent to the output and shown in every open dashboard.

Two styles are available: Action (blue) and Session (red when false, green when true, or the reverse with Invert colors). A Session switch can be required to find a session configuration, in the last message or in the global context, before it can be turned on. Any switch can ask for a password before each change.

Palette: category Utils, label button switch. 1 input, 1 output.

Configuration ​

PropertyDescriptionDefault
NameNode name shown in the editor.""
GroupDashboard group where the switch is displayed.(required)
SizeWidget width and height; 0 means automatic.0 x 0
Button labelTitle shown above the two buttons, and msg.topic of the output. When empty, the widget shows Expulsor.Expulsor
False labelText of the false-state button. When empty, Inactivo.Inactivo
True labelText of the true-state button. When empty, Activo.Activo
Switch typeAction: blue style. Session: red when false, green when true.action
Invert colorsShown for Session only. Green when false, red when true.false
Require configShown for Session only. The switch can be turned on only when a session configuration is found: see Session configuration check. Ticking it sets Config source to Global context.false
Config sourceShown with Require config. Message field: looked up in the last message the widget received. Global context: looked up in the Node-RED global context.global
Config fieldShown with Require config. Dot path of the configuration, for example sessionConfig. A leading msg. is ignored in Message field mode, and a leading global. in Global context mode.""
Ask passwordAsks for a password in a dialog before every change of state.false
PasswordShown with Ask password. Password the user must type.""
Password labelShown with Ask password. Title of the password dialog. When empty, Password.Password
Cancel textShown with Ask password. Text of the dialog button that cancels. When empty, CANCEL.CANCEL
Confirm textShown with Ask password. Text of the dialog button that confirms. When empty, CONFIRM.CONFIRM

Input ​

PropertyTypeEffect
msg.payloadbooleanSets the state.
msg.payload.statusbooleanSets the state, when msg.payload is an object.
msg.enabledbooleanfalse dims the card and disables both buttons; true enables them again.

A state set by a message is shown in every open dashboard and is not sent to the output; the password and the session configuration check do not apply to it. Values that are not booleans, such as the string "true", leave the state unchanged.

The widget keeps the last message it received, and each change made by a user is merged into it. A browser that opens or reloads the page shows the current state.

Session configuration check ​

The check runs only when Switch type is Session and Require config is ticked, and only when turning the switch on; turning it off is always allowed. The configuration is found when the value at Config field exists and is not null, an empty string, an empty array or an empty object. With an empty Config field it is never found.

  • Message field: the widget looks the path up in the last message it received. Use a top-level property such as msg.sessionConfig: after each change the widget replaces msg.payload with { status: <state> }, so a configuration kept under payload is no longer found until a new message brings it. When nothing is found, the switch stays off, the card shows Missing session config: <Config field> and nothing is sent.
  • Global context: the runtime reads the global context variable; nested paths such as line1.sessionConfig are followed. When nothing is found, the switch stays off, every open dashboard shows Missing session config: global.<Config field> and nothing is sent. When it is found, the switch turns on and the value goes out in msg.state.sessionConfig.

The error text disappears with the next change or message.

Password ​

With Ask password, every change opens a dialog titled with Password label. The change is applied only when the typed text equals Password; otherwise the dialog shows Incorrect password and stays open. Enter confirms; the cancel button or the close button in the header discards the change.

Output ​

One message each time a user changes the state, after the password and the session configuration check when they apply:

js
{
    payload: true,                // new state
    topic: "Expulsor",            // Button label
    state: {
        value: true,              // new state
        label: "Activo",          // label of the new state
        name: "Expulsor",         // Button label
        sessionConfig: { ... }    // see below
    },
    _client: { socketId: "...", socketIp: "..." }
}
  • state.sessionConfig: in Global context mode, when turning on with Require config, the value read from the global context. Otherwise, the value at Config field in the last message the widget received; left out when there is none.
  • _client: the browser connection that made the change, with socketId, socketIp and, when the Audit Manager has a logged-in dashboard user, user. Left out when client data is disabled in the dashboard base configuration.

Status and errors ​

The node sets no status. The widget shows these texts:

TextWhen
Missing session config: <Config field>Under the buttons, when the Message field check fails.
Missing session config: global.<Config field>Under the buttons, when the Global context check fails.
Missing session configUnder the buttons, when the check runs with an empty Config field.
Incorrect passwordIn the password dialog, when the password does not match.

The node logs the error No group configured when it has no group.

Usage example ​

A switch that starts and stops an inspection session, and refuses to start until the session parameters exist in the global context:

PropertyValue
Button labelSession
False labelOff
True labelOn
Switch typeSession
Require configticked
Config sourceGlobal context
Config fieldsessionConfig

A function node elsewhere in the flow stores the parameters when a recipe is loaded:

js
global.set("sessionConfig", { recipe: "R-12", batch: 4411 });
return msg;

When a user turns the switch on, the node sends:

js
{
    payload: true,
    topic: "Session",
    state: { value: true, label: "On", name: "Session", sessionConfig: { recipe: "R-12", batch: 4411 } },
    _client: { socketId: "...", socketIp: "..." }
}

When the session ends on its own, the flow turns the switch off in every dashboard:

js
msg.payload = false;
return msg;