How to read/get state from ui-switch?

I intend to use ui-switches to set certain states/options on the fly from within the UI.
Now the flow (partly automated through timers) should behave differently according to these settings.

For this purpose, I would need to check the switches states right before the workflow starts.
... how can this be done ?

I have fiddled with a context store, but accessing the UI-elemts states would be way more intuitive.

If it cannot be done,

  • maybe there is an object-tree or "Master list" of elements/objetcs, and properties could be "get".
  • maybe there is an implementation(node of a state "object" ?

as node-red is event based & the UI is client side (node-red is server side) there is no real way to "query" the ui state. You simply store the state of the ui switch in context & read that state whenever you need to know its value.

store state

ui switch1 → change node (set global/flow context var1)
ui switch2 → change node (set global/flow context var2)
ui switchx → change node (set global/flow context varx)

read state

inject/other → change node (set msg.payload to flow/global context item varx) → other nodes

No, but you can create an object (in flow or global) that holds the state of ALL you UI elements.

e.g. global.ui.sw1 global.ui.sw2 global.ui.text1 etc

they would be like an "object tree" (an object with sub properties)

You may also want to remember that there can be multiple browsers active on the dashboard, so there may well be multiple instances of "that switch" out there. You need to think a bit about message flow and state so they're all consistent, unless you don't want that.

indeed great answers ... thank you all a lot for contribution

thank you ... but can you pls. elaborate a little bit more on your "read state" answers ...

change node

... as in: "My other automated flow is triggered via a timer, calls a change node to read to read the variable, which then gets injected to message that is sent to the final workflow function ...,

... there is nothing wrong, reading the variable programmatically within a function ?
(or is this bad style, and not the "node red" way of doing things ?)

The function node has a bit of overhead so if you are using it just to get values from context it's a bit overkill but nothing to write home about.

However if you are doing other things in the function node then it makes sense not to use a change node & simply use the get functions within the function note to grab the context values.

As for the "node-red" way, doing everything in a function node is obviously going to hide cues and clues away vs a visual flow.

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