# Function node - how I can inject one switch at time

**URL:** <https://discourse.nodered.org/t/function-node-how-i-can-inject-one-switch-at-time/21880>\
**Category:** General\
**Created:** [17 February 2020 18:06 UTC](https://discourse.nodered.org/t/function-node-how-i-can-inject-one-switch-at-time/21880 "2020-02-17T18:06:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpt2007](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@lpt2007](https://discourse.nodered.org/u/lpt2007)\
**Post date:** [17 February 2020 18:06 UTC](https://discourse.nodered.org/t/function-node-how-i-can-inject-one-switch-at-time/21880/1 "2020-02-17T18:06:57Z")

</div>

Hi,

I try to make cycle button. I wann cycle trough modes with press on button.

Here is function node script, but not working correctly (script cycle trough all modes on button press.  
How I can correct loop script to cycle one mode at one press:

```auto
var modes = ["switch.ir_bathroom_light_work_template", "switch.ir_bathroom_light_sun_template", "switch.ir_bathroom_light_star_template", "switch.ir_bathroom_light_moon_template",];
var i = i;

node.warn("1");
for (i = 0; i < modes.length; i++) {
node.warn("2");
  msg.payload = {
  domain: 'switch',
  service: 'turn_on',
  data: { entity_id: modes[i] },
  };
  node.send(msg)
}

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [17 February 2020 18:21 UTC](https://discourse.nodered.org/t/function-node-how-i-can-inject-one-switch-at-time/21880/2 "2020-02-17T18:21:44Z")

</div>

Your for loop is doing just what you have told it to do (don't you hate that 😂)

if you mean that you want to run thru the 'modes' but only when a new msg arrives at the function, you will need to store the index in [context](https://nodered.org/docs/user-guide/writing-functions#storing-data) and incriment it each time a msg arrives and - presumably - reset it when it gets to the end.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [17 February 2020 20:44 UTC](https://discourse.nodered.org/t/function-node-how-i-can-inject-one-switch-at-time/21880/3 "2020-02-17T20:44:43Z")

</div>

The functionality you’re looking for is called a state machine. As an alternative to the function node, have you thought about using one of the specialised state machine nodes for it, such as [`node-red-contrib-dsm`](https://flows.nodered.org/node/node-red-contrib-dsm)

---

<div class="post-metadata">

**Author:** ![lpt2007](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@lpt2007](https://discourse.nodered.org/u/lpt2007)\
**Post date:** [18 February 2020 13:03 UTC](https://discourse.nodered.org/t/function-node-how-i-can-inject-one-switch-at-time/21880/4 "2020-02-18T13:03:51Z")

</div>

I don't know if this is ideal solution but is working:)

In case someone need cycle function with button press - cycle button, here is code:

```auto
var modes = ["switch.ir_bathroom_light_work_template", "switch.ir_bathroom_light_sun_template", "switch.ir_bathroom_light_star_template", "switch.ir_bathroom_light_moon_template",];
var i = context.get('i')||0;

start: while(true) {
node.warn("1");
  msg.payload = {
  domain: 'switch',
  service: 'turn_on',
  data: { entity_id: modes[i] },
  };
  i++;
  context.set('i',i);
  msg.count = i;
  node.send(msg);
  node.warn("2");
if (i == 4) {
    context.set('i',0);
}
if(i > 4) continue start;
  break;
}

```

---

<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:** [18 April 2020 13:03 UTC](https://discourse.nodered.org/t/function-node-how-i-can-inject-one-switch-at-time/21880/5 "2020-04-18T13:03:55Z")

</div>

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