# Use one function-node for many lights

**URL:** <https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318>\
**Category:** General\
**Created:** [21 September 2021 04:55 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318 "2021-09-21T04:55:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![burton666](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/burton666/32/48143_2.png) [@burton666](https://discourse.nodered.org/u/burton666)\
**Post date:** [21 September 2021 04:55 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/1 "2021-09-21T04:55:11Z")

</div>

Ok, sorry for the bad title but I could not figure out how to formulate myself. I have several lights where I use several separate flows that are almost identical. It checks for motion and for the time of day and the reading of a light-sensor and uses a function to transform the lux (light level) from the sensor to a dimlevel for the light-bulbs.  
I have a function in all these flows and the function is the same. Is it possible to use the same function-node for all flows ? I did some testing and tried to replace all with one global function but after that all light-bulbs seamed to trigger as soon as one motion-detection happened.

Is this supposed to work somehow by checking id's or similar ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 September 2021 06:50 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/2 "2021-09-21T06:50:12Z")

</div>

Are the flows similar enough that you can build a subflow that you configure using the subflow properties, which can then be used for each light?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 September 2021 07:57 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/3 "2021-09-21T07:57:01Z")

</div>

I not then show us how you attempted to replaced it with a global function.

---

<div class="post-metadata">

**Author:** ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)\
**Post date:** [21 September 2021 08:13 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/4 "2021-09-21T08:13:49Z")

</div>

I do not use subflows yet as I'm not able to pass all dynamic properties (~10 variables) down into the subflow

Any advise?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 September 2021 08:31 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/5 "2021-09-21T08:31:04Z")

</div>

The change node allows you to put variables onto the msg. Otherwise, a function node will let you grab any variables.

---

<div class="post-metadata">

**Author:** ![xx\_Nexus\_xx](https://avatars.discourse-cdn.com/v4/letter/x/54ee81/32.png) [@xx\_Nexus\_xx](https://discourse.nodered.org/u/xx_Nexus_xx)\
**Post date:** [21 September 2021 08:43 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/6 "2021-09-21T08:43:07Z")

</div>

I'm using function nodes all the time and aware on how tog grab/manage variables.

But I struggle to pass down the necessary values into a subflow ☹

**Simple example:**

- I have a sublfow to turn on a Lamp (LampID via variable)
- The will switch ON
- The lamp will wait for a specified duration (Duration via variable)
- the lamp switches OFF

It works for the 1st lamp .. but whenever I try a 2nd or 3rd lamp the it forgets the previous ones ☹ .. it looks like the variables are getting overwritten every time

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 September 2021 08:44 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/7 "2021-09-21T08:44:45Z")

</div>

The Trigger node will do that for you as long as each light control has a separate msg.topic.

---

<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:** [20 November 2021 08:45 UTC](https://discourse.nodered.org/t/use-one-function-node-for-many-lights/51318/8 "2021-11-20T08:45:07Z")

</div>

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