# Output and delay until condition

**URL:** <https://discourse.nodered.org/t/output-and-delay-until-condition/50181>\
**Category:** General\
**Created:** [25 August 2021 19:48 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181 "2021-08-25T19:48:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [25 August 2021 19:48 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/1 "2021-08-25T19:48:26Z")

</div>

Hi guys - looking for help - something I can't get my head around - can anyone help...

I've an OPPLE switch which sends "HOLD" as payload then when you let go of the button, sends "RELEASE".

I want an NR function that once it gets HOLD payload will iterate an output until it gets a RELEASE in the input - lets say 250ms delay each time.

So on receiving "hold" it might output "red",and after 250ms send "green", third time "blue" and keep on going until the button sends a "release. It is for colour control of lamps.... seems easy but can't quite get my head into it.

---

<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:** [25 August 2021 19:57 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/2 "2021-08-25T19:57:24Z")

</div>

Something like this should get you moving...

![734gKy57OB](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/876811d5422606d56921373f68ccb88f87b5d670.gif)

```auto
[{"id":"bcc8bbbf106ec10f","type":"function","z":"f542fe9c.732af","name":"colour cycle","func":"/ ****colour list - update as required***** /\nconst colours = [\n \"red\", \n \"blue\", \n \"green\",\n];\n\n/ ****dont change***** /\nlet colour = context.get(\"colour\"); //restore last colour\ncolour = colour == null ? 0 : colour + 1;//inc colour\nif(colour >= colours.length) {\n colour = 0;\n}\nmsg.payload = colours[colour];\ncontext.set(\"colour\", colour); //store last colour\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1970,"y":780,"wires":[["fc3d23dad0eccca5"]]},{"id":"fc3d23dad0eccca5","type":"debug","z":"f542fe9c.732af","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1970,"y":820,"wires":[]},{"id":"9678bb8c7ae13572","type":"inject","z":"f542fe9c.732af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"HOLD","payloadType":"str","x":1590,"y":660,"wires":[["743b3f71deec01b4"]]},{"id":"be4e10b6f93b781e","type":"inject","z":"f542fe9c.732af","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"RELEASE","payloadType":"str","x":1600,"y":700,"wires":[["743b3f71deec01b4"]]},{"id":"d91bbe63f60e0448","type":"inject","z":"f542fe9c.732af","name":"250ms","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"0.25","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":1600,"y":760,"wires":[["a5d7fc6c3ed6c6d6"]]},{"id":"a5d7fc6c3ed6c6d6","type":"switch","z":"f542fe9c.732af","name":"HOLD yes/no?","property":"HOLD","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1780,"y":760,"wires":[[],["bcc8bbbf106ec10f"]]},{"id":"743b3f71deec01b4","type":"function","z":"f542fe9c.732af","name":"set flow.HOLD","func":"if(msg.payload === \"HOLD\") {\n flow.set(\"HOLD\", true);\n node.status({fill:\"red\",shape:\"ring\",text:\"HOLD\"});\n} else if (msg.payload === \"RELEASE\") {\n flow.set(\"HOLD\", false);\n node.status({fill:\"green\",shape:\"ring\",text:\"RELEASE\"});\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1780,"y":680,"wires":[[]]}]

```

---

<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:** [25 August 2021 19:58 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/3 "2021-08-25T19:58:42Z")

</div>

Sorry, just re-read - you want the opposite 🙂

![OViUXNbm39](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/215adb995b6c33934b5acf60832b389457631df6.gif)

(just move the wire on the colour cycle function)

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [25 August 2021 19:59 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/4 "2021-08-25T19:59:18Z")

</div>

If that works you are a star. I have trouble getting my head around async nature of NR. I spent decades programming in C 🙂

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [25 August 2021 20:02 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/5 "2021-08-25T20:02:17Z")

</div>

I just pasted in your code - looks like it will do it if it has an incrementing number in there - i'll go look

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [25 August 2021 20:08 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/6 "2021-08-25T20:08:45Z")

</div>

Yup, got to get colours and I want several of these but thats the general idea - thank you.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [25 August 2021 22:42 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/7 "2021-08-25T22:42:36Z")

</div>

At the end of the day - many thanks that worked just fine once I changed "0xaaaaaa" to "#aaaaaa" :-)- I just wasted hours on this because if you HOLD the button for several seconds to cycle through colours, the RELEASE on the Aqara buttons does not reliably occur. SUCH a shame as this was going very well. Possibly holding the button for a while kills the battery... not sure.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [26 August 2021 19:11 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/8 "2021-08-26T19:11:28Z")

</div>

How about using the HOLD to SET the cycle process so that a single press changes the colour and then HOLD again to RESET. Should probably have a RESET delay so that if a single press does not occur the system resets to normal mode

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [26 August 2021 20:09 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/9 "2021-08-26T20:09:59Z")

</div>

Well, Buckskin, you Have started me thinking again - but watch - a short (couple of seconds) hold (then release) and a single click..  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/211031b356c54b6173dd062a9a18201e25eeba5d.png)  
Did you notice that double-press event (not singl) after the release. Now watch a several seconds hold... then release - then single click.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/b/ab6220f5a734f986578f3f30981a7dec0f101c7d.png)

Did you notice that on BOTH occasions, the button gave off a double at the end and the long one never gave a release. I'd have to accept that double - which prevents using double for anything else... but that MIGHT be an option - trust me I'm not losing the lot here - there is something up with the Aqara button (battery says 100%) - I just hope if this works that it is consistent from device to device and across teh rang.. I'll go back nowto Steve-Mcl's suggestion with this new information.

---

<div class="post-metadata">

**Author:** ![scargill](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scargill/32/1118_2.png) [@scargill](https://discourse.nodered.org/u/scargill)\
**Post date:** [27 August 2021 05:43 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/10 "2021-08-27T05:43:42Z")

</div>

My thanks to all for coming in with help. What I settled on - using the node-red-contrib-zigbee2mqtt input node, after realising that the AQARA RELEASE was not going to be reliable, was this: lets call the buttons A&B. Single-press A turns lights off, single press B turns them on. HOLD A resets sets colour to white (but turns lights off). HOLD B moves to the next colour in the sequence and if necessary turns the lights on. That seems to be running 100% reliably and isn't a bad compromise. double-click and triple-click seem obvious to me as a techie but not at ALL obvious to my non-techie wife.

---

<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:** [26 October 2021 05:44 UTC](https://discourse.nodered.org/t/output-and-delay-until-condition/50181/11 "2021-10-26T05:44:06Z")

</div>

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