# How to read/get state from ui-switch?

**URL:** https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194
**Category:** Dashboard
**Created:** [15 June 2021 18:36 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194 "2021-06-15T18:36:27Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![tutenchamun](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tutenchamun](https://discourse.nodered.org/u/tutenchamun)
#### Post date: [15 June 2021 18:36 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194/1 "2021-06-15T18:36:27Z")

</div>

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" ?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [15 June 2021 18:53 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194/2 "2021-06-15T18:53:48Z")

</div>

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

> [@tutenchamun](#):
>
> maybe there is an object-tree or "Master list" of elements/objetcs, and properties could be "get".

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)

---

<div class="post-metadata">

### Author: ![tve](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tve/32/59475_2.png) [@tve](https://discourse.nodered.org/u/tve)
#### Post date: [15 June 2021 20:28 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194/3 "2021-06-15T20:28:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![tutenchamun](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tutenchamun](https://discourse.nodered.org/u/tutenchamun)
#### Post date: [16 June 2021 10:10 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194/4 "2021-06-16T10:10:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![tutenchamun](https://avatars.discourse-cdn.com/v4/letter/t/aca169/32.png) [@tutenchamun](https://discourse.nodered.org/u/tutenchamun)
#### Post date: [16 June 2021 10:15 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194/5 "2021-06-16T10:15:38Z")

</div>

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

inject/other  
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 ?)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [16 June 2021 10:21 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194/6 "2021-06-16T10:21:48Z")

</div>

> [@tutenchamun](#):
>
> 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.

---

<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: [16 July 2021 10:22 UTC](https://discourse.nodered.org/t/how-to-read-get-state-from-ui-switch/47194/7 "2021-07-16T10:22:46Z")

</div>

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