# Queueing messages - need help - can this flow be optimised?

**URL:** <https://discourse.nodered.org/t/queueing-messages-need-help-can-this-flow-be-optimised/27105>\
**Category:** General\
**Created:** [22 May 2020 23:14 UTC](https://discourse.nodered.org/t/queueing-messages-need-help-can-this-flow-be-optimised/27105 "2020-05-22T23:14:55Z")\
**Posts on this page:** 4\
**Page:** 2

<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:** [23 May 2020 10:16 UTC](https://discourse.nodered.org/t/queueing-messages-need-help-can-this-flow-be-optimised/27105/21 "2020-05-23T10:16:12Z")

</div>

> [@Trying\_to\_learn](#):
>
> I haven't left any nasty things in there that may bite me later.

All the nested if's will bite you at some point.

Isn't the goal to store everything and only pass when the state is on ?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [23 May 2020 10:51 UTC](https://discourse.nodered.org/t/queueing-messages-need-help-can-this-flow-be-optimised/27105/22 "2020-05-23T10:51:05Z")

</div>

> [@bakman2](#):
>
> Isn't the goal to store everything and only pass when the state is on ?

correct. That is the idea

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [24 May 2020 06:42 UTC](https://discourse.nodered.org/t/queueing-messages-need-help-can-this-flow-be-optimised/27105/23 "2020-05-24T06:42:27Z")

</div>

This is the latest version after a bit of tweaking and bug fixing.

It seems to work as expected.

```auto
[{"id":"993cfacc.ee679","type":"function","z":"26262ba1.62dcbc","name":"New version","func":"// flag: 0 - block 1 - pass.\n// hold - stored colour when bulb is `off`.\n// held - retreived name of colour when bulb was `off`.\nvar flag = context.get(\"flag\") || 0;\nvar held = \"\";\nvar x = \"\";\nif (msg.topic == \"control\")\n{\n //\n if (msg.payload == \"On\")\n {\n context.set(\"flag\",1);\n node.status({fill:\"green\",shape:\"dot\",text:\"Pass\"});\n \n held = context.get(\"hold\") || 0;\n \n if (held)\n {\n x = context.get(\"hold\") || msg.payload;\n msg = {payload: \"On\", colour: x};\n // Wipe stored value.\n context.set(\"hold\",0);\n }\n }\n if (msg.payload == \"Off\")\n {\n context.set(\"flag\",0)\n node.status({fill:\"red\",shape:\"dot\",text:\"Block\"});\n msg.payload = \"Off\";\n }\n return msg;\n}\n\n//node.warn(\"Here\");\n\nif (flag)\n{\n // Pass message.\n// node.warn(\"Pass\");\n \n // Get stored values.\n held = context.get(\"hold\") || 0;\n if (held)\n {\n // Build message.\n x = context.get(\"hold\");\n msg = {payload: \"On\", colour: x};\n context.set(\"hold\",0); // not needed?\n }\n else\n {\n x = msg.payload;\n msg = {payload: \"On\", colour: x};\n }\n \n \n return msg;\n //\n} else\n{\n // Block (store anything).\n context.set(\"hold\",msg.payload);\n return;\n}\n","outputs":1,"noerr":0,"x":2450,"y":4390,"wires":[["451d442e.3fbdec"]]},{"id":"451d442e.3fbdec","type":"debug","z":"26262ba1.62dcbc","name":"OUTPUT 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":2620,"y":4390,"wires":[]},{"id":"88e2a0a2.e0bc4","type":"inject","z":"26262ba1.62dcbc","name":"Red","topic":"","payload":"red","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2270,"y":4390,"wires":[["993cfacc.ee679"]]},{"id":"a8c208f.2f449f8","type":"change","z":"26262ba1.62dcbc","name":"control","rules":[{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2280,"y":4340,"wires":[["993cfacc.ee679"]]},{"id":"d6efcd50.bba668","type":"inject","z":"26262ba1.62dcbc","name":"Green","topic":"","payload":"green","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2270,"y":4440,"wires":[["993cfacc.ee679"]]},{"id":"72ebb868.b4fbf8","type":"inject","z":"26262ba1.62dcbc","name":"","topic":"","payload":"Off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2280,"y":4250,"wires":[["a8c208f.2f449f8"]]},{"id":"e6dce07c.c37c88","type":"inject","z":"26262ba1.62dcbc","name":"","topic":"","payload":"On","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":2280,"y":4290,"wires":[["a8c208f.2f449f8"]]}]

```

Until you press the `on` button messages (colours) won't get through.  
When you press the `on` button an `On` message will be sent.  
If a colour button has been pressed before hand, it will add the colour. Otherwise it will be simply `On`.

Pressing the colour buttons you get them going through.

Pressing the `Off` button turns the bulb off. (to say)

If while off the colour is changed, when the `on` button is pressed an `On` signal will be sent with the new colour.

Subsequent pressing of the colour button will have them pass through.

---

<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:** [23 July 2020 06:42 UTC](https://discourse.nodered.org/t/queueing-messages-need-help-can-this-flow-be-optimised/27105/24 "2020-07-23T06:42:28Z")

</div>

Closed on 23 July 2020 06:42 UTC

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

[Previous page](https://discourse.nodered.org/t/queueing-messages-need-help-can-this-flow-be-optimised/27105.md?page=1)
