# Cycle through outputs

**URL:** <https://discourse.nodered.org/t/cycle-through-outputs/57261>\
**Category:** General\
**Created:** [26 January 2022 13:57 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261 "2022-01-26T13:57:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![vantech](https://avatars.discourse-cdn.com/v4/letter/v/b38774/32.png) [@vantech](https://discourse.nodered.org/u/vantech)\
**Post date:** [26 January 2022 13:57 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261/1 "2022-01-26T13:57:18Z")

</div>

Hi,

I am trying to program the below scenario which in essence fires a different output every time a button is pressed, in a loop - the purpose is for scene selection with a Shelly device.

Button press 1 (this is a shelly button press) = Scene 1  
Button press 2 = scene 2  
3 = scene 3  
4 = scene 4  
loop returns to the top and the next button press = Scene 1

The idea is that node red holds the logic and record as to which scene was selected last. I am not overly concerned about reboots etc, just need this logic.

Any ideas?

Thanks.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [26 January 2022 14:23 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261/2 "2022-01-26T14:23:36Z")

</div>

Have you looked at context saving? Button press one context.set('button',1) button press two Button = context.get('button') + 1 and so on?

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 January 2022 14:23 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261/3 "2022-01-26T14:23:49Z")

</div>

The easiest way is to store the 'scene' into a variable and compare against it.

gerry was quicker.

---

<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:** [26 January 2022 14:24 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261/4 "2022-01-26T14:24:40Z")

</div>

You could easily adapt this to output "Scene x" instead of colours:

> [@MQTT - Single button press - multiple different actions (in order/sequence)](https://discourse.nodered.org/t/mqtt-single-button-press-multiple-different-actions-in-order-sequence/50125/2):
>
> You need to remember last state. for this there are several solutions. The easiest (for me) is a function node... [image] [{"id":"75f941daf629c2ba","type":"inject","z":"219b0be6.926804","name":"fake button","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":1570,"y":320,"wires":[["bcc8bbbf106ec10f"]]},{"id":"bcc8bbbf106ec10f","type":"function","z":"219b0be6.926804","name":"colour cycle","…

---

<div class="post-metadata">

**Author:** ![vantech](https://avatars.discourse-cdn.com/v4/letter/v/b38774/32.png) [@vantech](https://discourse.nodered.org/u/vantech)\
**Post date:** [26 January 2022 14:43 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261/5 "2022-01-26T14:43:28Z")

</div>

Thanks,

The shelly device sends an MQTT command which is the same trigger each time. What this trigger needs to do is ramp up / step through outputs that are then connected to actions/light outputs.

I need to create a way to recall a scene (or whatever it may be) and store this.

I am not sure where to start on a function node.

Thanks.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [26 January 2022 18:26 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261/6 "2022-01-26T18:26:54Z")

</div>

Google for context.set as a start. Try to program a function node and if you get stuck come back for help, we'll be around

---

<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:** [27 March 2022 18:27 UTC](https://discourse.nodered.org/t/cycle-through-outputs/57261/7 "2022-03-27T18:27:27Z")

</div>

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