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
| Property | Description | Default |
|---|---|---|
| Name | Node name shown in the editor. | "" |
| Group | Dashboard group where the switch is displayed. | (required) |
| Size | Widget width and height; 0 means automatic. | 0 x 0 |
| Button label | Title shown above the two buttons, and msg.topic of the output. When empty, the widget shows Expulsor. | Expulsor |
| False label | Text of the false-state button. When empty, Inactivo. | Inactivo |
| True label | Text of the true-state button. When empty, Activo. | Activo |
| Switch type | Action: blue style. Session: red when false, green when true. | action |
| Invert colors | Shown for Session only. Green when false, red when true. | false |
| Require config | Shown 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 source | Shown 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 field | Shown 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 password | Asks for a password in a dialog before every change of state. | false |
| Password | Shown with Ask password. Password the user must type. | "" |
| Password label | Shown with Ask password. Title of the password dialog. When empty, Password. | Password |
| Cancel text | Shown with Ask password. Text of the dialog button that cancels. When empty, CANCEL. | CANCEL |
| Confirm text | Shown with Ask password. Text of the dialog button that confirms. When empty, CONFIRM. | CONFIRM |
Input
| Property | Type | Effect |
|---|---|---|
msg.payload | boolean | Sets the state. |
msg.payload.status | boolean | Sets the state, when msg.payload is an object. |
msg.enabled | boolean | false 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 asmsg.sessionConfig: after each change the widget replacesmsg.payloadwith{ status: <state> }, so a configuration kept underpayloadis no longer found until a new message brings it. When nothing is found, the switch stays off, the card showsMissing session config: <Config field>and nothing is sent.Global context: the runtime reads the global context variable; nested paths such asline1.sessionConfigare followed. When nothing is found, the switch stays off, every open dashboard showsMissing session config: global.<Config field>and nothing is sent. When it is found, the switch turns on and the value goes out inmsg.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:
{
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: inGlobal contextmode, 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, withsocketId,socketIpand, 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:
| Text | When |
|---|---|
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 config | Under the buttons, when the check runs with an empty Config field. |
Incorrect password | In 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:
| Property | Value |
|---|---|
| Button label | Session |
| False label | Off |
| True label | On |
| Switch type | Session |
| Require config | ticked |
| Config source | Global context |
| Config field | sessionConfig |
A function node elsewhere in the flow stores the parameters when a recipe is loaded:
global.set("sessionConfig", { recipe: "R-12", batch: 4411 });
return msg;When a user turns the switch on, the node sends:
{
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:
msg.payload = false;
return msg;