# Request help why things aren't alway working

**URL:** <https://discourse.nodered.org/t/request-help-why-things-arent-alway-working/79782>\
**Category:** General\
**Created:** [13 July 2023 03:47 UTC](https://discourse.nodered.org/t/request-help-why-things-arent-alway-working/79782 "2023-07-13T03:47:29Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [13 July 2023 03:47 UTC](https://discourse.nodered.org/t/request-help-why-things-arent-alway-working/79782/1 "2023-07-13T03:47:29Z")

</div>

**Foreign node: required**

`cronplus`

This is _complicated_ and this is not the entire flow.  
That is just expecting too much from anyone remote to take this on completely.  
So I've made this which is a cut down version of the _important_ parts of the flow.  
The times and values won't be found if you run the code as is.

code:

```auto
[{"id":"9e68c7a9.1756d8","type":"function","z":"d188b95f33e5f7e4","name":"Cycle next value","func":"switch (msg.next) {\n \n case 3:\n msg.delay = msg.runtime3;\n msg.payload = 3;\n msg.next = 2;\n break; \n\n case 2:\n msg.delay = msg.runtime2;\n msg.payload = 2;\n msg.next = 1;\n break; \n\n case 1:\n msg.delay = msg.runtime1;\n msg.payload = 1; \n msg.next = 0;\n break;\n \n case 0:\n msg.delay = msg.runtime4;\n msg.payload = 0;\n msg.next = \"OFF\";\n break; \n\n case \"OFF\":\n msg.delay = msg.runtime5;\n msg.payload = \"OFF\";\n msg.next = 7;\n break; \n\n case 7:\n msg.reset = true;\n node.status({});\n node.warn(\"RESET MESSAGE send\");\n return msg;\n}\nmsg.who = \"auto off\";\nlet d = msg.delay;\nd = d / 1000;\nif (d > 10)\n{\n d = Math.round(d / 60); // was `parseInt()`\n node.status({ text:\"Next value \" + msg.payload + \" in \" + d + \" minutes\" });\n}\n\nif (msg.payload == \"TEST\")\n{\n node.status({text: msg.payload + \" 20 seconds\"});\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2380,"y":250,"wires":[["c874030e6580e482"]]},{"id":"c47b6829.73f2a","type":"delay","z":"d188b95f33e5f7e4","name":"","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2235,"y":250,"wires":[["9e68c7a9.1756d8"]],"l":false},{"id":"e7a56f9a.9fcfc","type":"delay","z":"d188b95f33e5f7e4","name":"Variable delay","pauseType":"delayv","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2380,"y":310,"wires":[["9e68c7a9.1756d8","dcd5c697.91cc5","15571d211caede5d"]]},{"id":"c874030e6580e482","type":"switch","z":"d188b95f33e5f7e4","name":"","property":"reset","propertyType":"msg","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":2495,"y":260,"wires":[["592a1719.5dd9d"],["e7a56f9a.9fcfc"]],"l":false},{"id":"6ecf218c.caa63","type":"function","z":"d188b95f33e5f7e4","name":"Initialize *","func":"// 2023 04 3 19:00 Working.\n// Code modified to allow better testing.\n\nlet offtime = flow.get(\"off_time\");\nlet interval = 0;\nconst half_hour = 1800000;\nlet msg1 = {};\n\nif (msg.flush == \"blah\")\n{\n //\n node.status({});\n return;\n}\n\n\n//node.warn(\"Now \" + msg.payload);\n//node.warn(\"Off time \" + offtime);\n\nlet check = msg.payload;\n// All being normal, this value is the total time (ms) from *now* to 22:45\n// Need to modify how things happen now.\nif (check == \"TEST\") {\n // Set to 10 seconds for testing.\n //node.warn(\"Ten seconds set\");\n node.status({ text: \"Delay 20 SECONDS - TEST\" });\n interval = 20000;\n var test = true; // Indicate TEST mode.\n}\nelse {\n //\n let tnow = moment()\n msg.payload = moment(offtime, \"HH:mm:ss\").diff(moment(tnow));\n\n node.warn(\"Normal message\");\n node.warn(\"check \" + check);\n //\tChange name to interval as it is the time *interval* between events.\n interval = Math.round((msg.payload - half_hour) / 5); // Was `parseInt()\n flow.set(\"delay_ms\", parseInt(interval/2)); // Set delay for next morning.\n\n var delay = parseInt(interval / 60000);\t//\tConvert ms to minutes\n\n //node.warn(\"interval \" + interval);\n //node.warn(\"Minutes \" + delay);\n\n // let delay = flow.get('delay') || 15; // 15 if not set (*1000)\n // interval = delay * 60000; // convert value to minutes.\n\n node.status({ text: \"Delay \" + delay + \" minutes\" });\n msg1.payload = delay + \" min's.\";\n\n // TEST CODE HERE.\n //msg.payload = interval;\n //return msg;\n //\n // At this point `interval` is the value I want to use.\n}\n\n//node.warn(\"Incoming payload \" + check);\n\n//node.warn(\"test condition \" + test);\n\n// Save this time for DAWN use also.\nflow.set(\"delay_ms\", interval);\n\n//msg.flow_delay = delay; // For debugging what is going on\n\nmsg.delay = half_hour;\n// Define runtime0\nif (check == \"TEST\") {\n msg.delay = interval;\n}\nmsg.runtime0 = interval;\n// Define runtime1\nmsg.runtime1 = interval;\n// Define runtime2\nmsg.runtime2 = interval;\n// Define runtime3\nmsg.runtime3 = interval;\n// Define runtime4\nmsg.runtime4 = interval;\n// Define runtime5\n\n//msg.runtime5 = interval/2;\nmsg.runtime5 = interval; // 2023 05 01 was interval\n// Define runtime6\n//msg.runtime6 = interval/2;\nmsg.runtime6 = interval;\n// Next to be activated is number 1\n\nmsg.next = 3;\nreturn [msg,msg1];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"}],"x":2100,"y":250,"wires":[["c47b6829.73f2a"],[]],"info":"2020 12 24 21:21\nMake it send `msg.next=2` rather than `1`.\n`1` is already set from morning.\n2020 12 25\nRearranged nodes and imported top bit of code from other node"},{"id":"592a1719.5dd9d","type":"function","z":"d188b95f33e5f7e4","name":"","func":"msg = {\n 'flush':'blah',\n 'reset':'blah'\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2235,"y":310,"wires":[["e7a56f9a.9fcfc","c47b6829.73f2a","6ecf218c.caa63"]],"l":false},{"id":"dcd5c697.91cc5","type":"change","z":"d188b95f33e5f7e4","name":"topic -> Dusk","rules":[{"t":"set","p":"topic","pt":"msg","to":"dusk","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1825,"y":470,"wires":[["071174a3fadba903"]],"l":false},{"id":"15571d211caede5d","type":"switch","z":"d188b95f33e5f7e4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"neq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":2565,"y":310,"wires":[["e00a4d08ccfaaa2a"],["374601a730ee844f"]],"l":false},{"id":"47071a7afd8f612b","type":"inject","z":"d188b95f33e5f7e4","name":"Dusk (TEST)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"TEST","payloadType":"str","x":1860,"y":310,"wires":[["6ecf218c.caa63"]]},{"id":"bfb00242.9bfce8","type":"switch","z":"d188b95f33e5f7e4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Dawn","vt":"str"},{"t":"eq","v":"Dusk","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1880,"y":250,"wires":[[],["6ecf218c.caa63"]]},{"id":"61857841.7f224","type":"inject","z":"d188b95f33e5f7e4","name":"Reset","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":2110,"y":310,"wires":[["592a1719.5dd9d"]]},{"id":"071174a3fadba903","type":"switch","z":"d188b95f33e5f7e4","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"dawn","vt":"str"},{"t":"eq","v":"dusk","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":1920,"y":470,"wires":[[],["541535b815070870"],[]]},{"id":"e00a4d08ccfaaa2a","type":"function","z":"d188b95f33e5f7e4","name":"","func":"//msg.payload = {\"payload\":\"rgb,0,0,0,0\",\"wipe\":\"WIPE\"};\nmsg.payload = \"rgb,0,0,0,0\";\n//msg.wipe = \"OLD\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2635,"y":310,"wires":[["aedc64fbdac76f67"]],"l":false},{"id":"374601a730ee844f","type":"function","z":"d188b95f33e5f7e4","name":"","func":"if (msg.apyload != 0)\n{\n msg.payload = \"rgb,0,15,2,0\";\n return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2635,"y":350,"wires":[["0cd462fed476847f","172374226f380791"]],"l":false},{"id":"26a1b7ad.957528","type":"delay","z":"d188b95f33e5f7e4","name":"Rate limit","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"6","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"outputs":1,"x":1870,"y":210,"wires":[["bfb00242.9bfce8"]]},{"id":"541535b815070870","type":"function","z":"d188b95f33e5f7e4","name":"Dusk","func":"const existing = flow.get(\"display_brightness\");\n\nif (msg.payload > existing) return;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2035,"y":470,"wires":[["fccda82d9bf1fcd9"]],"l":false},{"id":"aedc64fbdac76f67","type":"trigger","z":"d188b95f33e5f7e4","name":"","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"6","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":2745,"y":310,"wires":[["fa07635eae2bf842"]],"l":false},{"id":"0cd462fed476847f","type":"trigger","z":"d188b95f33e5f7e4","name":"","op1":"","op2":"","op1type":"pay","op2type":"pay","duration":"3","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":2745,"y":350,"wires":[["fa07635eae2bf842"]],"l":false},{"id":"172374226f380791","type":"switch","z":"d188b95f33e5f7e4","name":"OFF?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":2635,"y":390,"wires":[["bffc636f64672937"]],"l":false},{"id":"1a991232.3450b6","type":"cronplus","z":"d188b95f33e5f7e4","name":"Dawn / Dusk signal","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"Winter Dawn","topic":"schedule1","payloadType":"str","payload":"Dawn","expressionType":"cron","expression":"0 0 6 * 3-8 ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"Summer Dawn","topic":"schedule2","payloadType":"str","payload":"Dawn","expressionType":"cron","expression":"0 0 6 * 9-2 ? *","location":"","offset":"0","solarType":"all","solarEvents":"sunrise,sunset"},{"name":"schedule3","topic":"schedule3","payloadType":"str","payload":"Dusk","expressionType":"solar","expression":"0 * * * * * *","location":"-33.51 151.12","offset":"0","solarType":"selected","solarEvents":"nauticalDusk"}],"x":1840,"y":170,"wires":[["26a1b7ad.957528"]]},{"id":"fccda82d9bf1fcd9","type":"switch","z":"d188b95f33e5f7e4","name":"Dusk","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","repair":false,"outputs":5,"x":2130,"y":470,"wires":[["a097d8aa.e17598"],["838199cc.149728"],["88aacaa5.be4698"],["1eb6854f.240bfb"],["dc77ecc0.c28f68"]]},{"id":"fa07635eae2bf842","type":"function","z":"d188b95f33e5f7e4","name":"MQTT New structure builder","func":"let msg1 = {};\nmsg1.payload = msg;\nreturn msg1;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2805,"y":350,"wires":[["7692145a393cf412"]],"l":false},{"id":"bffc636f64672937","type":"trigger","z":"d188b95f33e5f7e4","name":"","op1":"","op2":"rgb,0,0,0,0","op1type":"nul","op2type":"str","duration":"4500","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":2745,"y":390,"wires":[["0bd9a11036985ca9"]],"l":false},{"id":"a097d8aa.e17598","type":"function","z":"d188b95f33e5f7e4","name":"","func":"if (msg.topic == 'adjustment')\n{\n node.warn(msg.payload);\n context.set('brightness', msg.payload);\n node.status({ text: msg.payload });\n return;\n}\nmsg.payload = (context.get('brightness'));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2310,"y":440,"wires":[["971aec7517b5810e"]]},{"id":"838199cc.149728","type":"function","z":"d188b95f33e5f7e4","name":"","func":"if (msg.topic == 'adjustment')\n{\n node.warn(msg.payload);\n context.set('brightness', msg.payload);\n node.status({ text: msg.payload });\n return;\n}\nmsg.payload = (context.get('brightness'));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2310,"y":520,"wires":[["971aec7517b5810e"]]},{"id":"88aacaa5.be4698","type":"function","z":"d188b95f33e5f7e4","name":"","func":"if (msg.topic == 'adjustment')\n{\n node.warn(msg.payload);\n context.set('brightness', msg.payload);\n node.status({ text: msg.payload });\n return;\n}\nmsg.payload = (context.get('brightness'));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2310,"y":600,"wires":[["971aec7517b5810e"]]},{"id":"1eb6854f.240bfb","type":"function","z":"d188b95f33e5f7e4","name":"","func":"if (msg.topic == 'adjustment')\n{\n node.warn(msg.payload);\n context.set('brightness', msg.payload);\n node.status({ text: msg.payload });\n return;\n}\nmsg.payload = (context.get('brightness'));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2310,"y":680,"wires":[["971aec7517b5810e"]]},{"id":"dc77ecc0.c28f68","type":"function","z":"d188b95f33e5f7e4","name":"","func":"if (msg.topic == 'adjustment')\n{\n node.warn(msg.payload);\n context.set('brightness', msg.payload);\n node.status({ text: msg.payload });\n return;\n}\nmsg.payload = (context.get('brightness'));\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2310,"y":760,"wires":[["971aec7517b5810e"]]},{"id":"0bd9a11036985ca9","type":"function","z":"d188b95f33e5f7e4","name":"function 8","func":"msg.wipe = \"OLD\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2805,"y":390,"wires":[["fa07635eae2bf842"]],"l":false},{"id":"7692145a393cf412","type":"delay","z":"d188b95f33e5f7e4","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":2855,"y":350,"wires":[["9f0ddbefccb866be"]],"l":false},{"id":"971aec7517b5810e","type":"debug","z":"d188b95f33e5f7e4","name":"Working","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2510,"y":600,"wires":[]},{"id":"9f0ddbefccb866be","type":"debug","z":"d188b95f33e5f7e4","name":"NOT ALWAYS SEEING THIS","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2810,"y":480,"wires":[]}]

```

Walk through: (Keep reading more at the bottom)

`cronplus sends me a `dusk` signal.  
Things start to happen.

`30` minutes + a given time a signal is sent to _dim the brightness_ and it gets through.  
This repeats a few times and finally an `OFF` signal is sent to `turn off` the _display_.  
Again: that works.

Only out of curiosity I added the bit of code to the right (after the `change` node where it goes back on itself).

This sends a signal to my _neopixel_ strip and it flashes (twice) the LED to indicate a dulling.  
Twice as said LED is busy doing other things and sending it twice means I will see it if it arrives just before it does a colour change.

Pressing the `dusk (TEST)` button it works. ALL _blinks_ are seen.

Left to do it from the `dusk` signal sent from the `cronplus` node: I only see some _blinks_ now and then.  
I think the first and last, but sometimes others are seen. Other times not.

Am I missing something?

**GIVEN**  
All external variables are set and correct.  
(It wouldn't work in the `test` mode if they were wrong,so it is kind of a given they are correct.)

(more on the walk through)

Picture:

 ![Screenshot from 2023-07-13 13-33-56](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b001ac7161afa1e52347899e470bfc0616644b1.png)

Yellow: Timer part.  
Green: Sends sequential message with a value to control the brightness  
Blue: Working.  
Red: Problem area.  
Orange arrow: Test injection and all works when pressed.

Breakdown of GREEN section:  
Message received, build message with all times in it.  
Send into `DELAY` node (set for **VARIABLE CONTROL** ).  
That then goes to the next `function` node that reads the next time value to wait. (Yeah, they are all the same - I think. Work in progress, maybe.)  
If the message is `not null` (`switch` node after `function` node) to reduced size `function` node.  
This just wipes values and sends a `reset` signal to the next `delay` node.  
5 second `delay` node.  
Then it is off to the BLUE section. That is ok.  
The RED section also gets that message.  
Sorry it is a _dog's breakfast_ here, but it is just how I got the code to handle the messages.  
This constructs a message to send to the _neopixel_ LED strip.  
The message is fine in format, but I seldom see it happen.

Why?

---

<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:** [13 July 2023 07:05 UTC](https://discourse.nodered.org/t/request-help-why-things-arent-alway-working/79782/2 "2023-07-13T07:05:38Z")

</div>

As always the solution is to add debug nodes till you find _exactly_ which node is not doing what you expect. Then you can make a simple example of the problem with just a few nodes so that we can test it and see what is going wrong.

---

<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:** [11 September 2023 07:06 UTC](https://discourse.nodered.org/t/request-help-why-things-arent-alway-working/79782/3 "2023-09-11T07:06:37Z")

</div>

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