# Best practice for ui\_switch

**URL:** <https://discourse.nodered.org/t/best-practice-for-ui-switch/62677>\
**Category:** Dashboard\
**Created:** [16 May 2022 08:21 UTC](https://discourse.nodered.org/t/best-practice-for-ui-switch/62677 "2022-05-16T08:21:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [16 May 2022 08:21 UTC](https://discourse.nodered.org/t/best-practice-for-ui-switch/62677/1 "2022-05-16T08:21:10Z")

</div>

I have tenths of ui\_switches in my Dashboard, controlled by mqtt (the switch is set as "_Switch icon shows state of the input_"), schematically represented by this flow:

 ![Schermata 2022-05-16 alle 10.19.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/b/7bd9204a088256529f6640fea4c9a1fec7c41773.png)

Now, when I need to restart NR, all the ui\_switches go into an undetermined state, so in order to get the state of the switch I am modifying those flows with this one (saving a flow variable):

 ![Schermata 2022-05-16 alle 10.20.28](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cff418d96df9985d040012056335c66a02194a00.png)

Is this the best practice? With this modified flow, each time I start the Dashboard I have a peak of CPU consumption .

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [16 May 2022 09:06 UTC](https://discourse.nodered.org/t/best-practice-for-ui-switch/62677/2 "2022-05-16T09:06:56Z")

</div>

There are a few ways to do this, you could have an inject node set to trigger once at startup instead of ui\_control, since the ui\_conrol may be triggering when not needed eg change tab etc.

Or you could set the mqtt in messages to be retained so they get re-sent when when NR restarts.

But do you really need to restart NR that often ? Have you tried only deploying changed nodes ?

---

<div class="post-metadata">

**Author:** ![fmarzocca](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fmarzocca/32/35901_2.png) [@fmarzocca](https://discourse.nodered.org/u/fmarzocca)\
**Post date:** [16 May 2022 09:15 UTC](https://discourse.nodered.org/t/best-practice-for-ui-switch/62677/3 "2022-05-16T09:15:33Z")

</div>

> [@Sean-McG](#):
>
> But do you really need to restart NR that often ? Have you tried only deploying changed nodes ?

Indeed, I don't need to restart so often. But when it happens, I lose the status of too many devices...I have tried with the inject node to trigger at startup, but it seems not always working.

---

<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:** [15 June 2022 09:15 UTC](https://discourse.nodered.org/t/best-practice-for-ui-switch/62677/4 "2022-06-15T09:15:52Z")

</div>

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