# Simple step through outputs on trigger

**URL:** <https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490>\
**Category:** General\
**Created:** [31 October 2023 13:13 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490 "2023-10-31T13:13:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![pabutterworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pabutterworth/32/58515_2.png) [@pabutterworth](https://discourse.nodered.org/u/pabutterworth)\
**Post date:** [31 October 2023 13:13 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/1 "2023-10-31T13:13:01Z")

</div>

Hi,  
I have what I think should be really simple challenge, but am struggling.

I want to setup a flow where each time the flow is triggered it triggers the next output in a sequence

I can do with a complex if then else in a function node, but it is not very elegant.

The use case is using a single button to trigger different radio channels to be played.  
so each time you press, it switches to the next channel in a set list and when it gets to the last it restarts.

I'm sure there must be cone neat way to achieve this.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [31 October 2023 13:16 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/2 "2023-10-31T13:16:49Z")

</div>

If your list of channels is in an array, you just need to increment a counter (and reset it when it points to a nonexistant array element)

Probably use a flow context variable for the counter.

---

<div class="post-metadata">

**Author:** ![pabutterworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pabutterworth/32/58515_2.png) [@pabutterworth](https://discourse.nodered.org/u/pabutterworth)\
**Post date:** [31 October 2023 13:51 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/3 "2023-10-31T13:51:54Z")

</div>

Thanks, I could lok at that, i'm surprised that there isn't a state machine module that works like this.

---

<div class="post-metadata">

**Author:** ![pabutterworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pabutterworth/32/58515_2.png) [@pabutterworth](https://discourse.nodered.org/u/pabutterworth)\
**Post date:** [31 October 2023 14:24 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/4 "2023-10-31T14:24:13Z")

</div>

Solved it with a function variable followed by a switch node.

```auto
if (flow.get("channel") == 6){
    flow.set("channel",4);
}else if (flow.get("channel") == 4){
    flow.set("channel",3);
}else if(flow.get("channel") == 3){
    flow.set("channel",6);
}
else{
    flow.set("channel",6);
}  
return msg;

```

---

<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:** [31 October 2023 14:29 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/5 "2023-10-31T14:29:10Z")

</div>

> [@pabutterworth](#):
>
> i'm surprised that there isn't a state machine module that works like this.

There are several state machine nodes in the [node-red flows library](https://flows.nodered.org/).

---

<div class="post-metadata">

**Author:** ![pabutterworth](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pabutterworth/32/58515_2.png) [@pabutterworth](https://discourse.nodered.org/u/pabutterworth)\
**Post date:** [31 October 2023 15:57 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/6 "2023-10-31T15:57:24Z")

</div>

I know, but cant get my head around how I would achieve this functionality

---

<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:** [31 October 2023 16:19 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/7 "2023-10-31T16:19:30Z")

</div>

Use the button trigger to step you through the states in whatever machine you are using. Personally I would just use a Function node, I think it is easier than using a state machine node.

```auto
const channels = [6, 4, 3]
let currentIndex = context.get("currentIndex") || 0
currentIndex = currentIndex + 1
if (currentIndex >= channels.length) {
    currentIndex = 0
}
msg.payload = channels[currentIndex]
node.status( `Channel ${msg.payload}` )
context.set("currentIndex", currentIndex)
return msg;

```

```auto
[{"id":"804aa6bce11220c3","type":"inject","z":"bdd7be38.d3b55","name":"Button simulator","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":3280,"wires":[["a1d8ae260177640c"]]},{"id":"a1d8ae260177640c","type":"function","z":"bdd7be38.d3b55","name":"Cycle channels","func":"const channels = [6, 4, 3]\nlet currentIndex = context.get(\"currentIndex\") || 0\ncurrentIndex = currentIndex + 1\nif (currentIndex >= channels.length) {\n currentIndex = 0\n}\nmsg.payload = channels[currentIndex]\nnode.status( `Channel ${msg.payload}` )\ncontext.set(\"currentIndex\", currentIndex)\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":3280,"wires":[["8b1f2296c8b345b7"]]},{"id":"8b1f2296c8b345b7","type":"debug","z":"bdd7be38.d3b55","name":"debug 106","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":3280,"wires":[]}]

```

> [@pabutterworth](#):
>
> Solved it with a function variable followed by a switch node.

What did you need a Switch node for?

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [31 October 2023 16:27 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/8 "2023-10-31T16:27:46Z")

</div>

I think using a state machine would be an overkill since you are just incrementing or decrementing by unity.

---

<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:** [30 December 2023 16:28 UTC](https://discourse.nodered.org/t/simple-step-through-outputs-on-trigger/82490/9 "2023-12-30T16:28:29Z")

</div>

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