# This code works differently when I test it than when it gets a real message

**URL:** <https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900>\
**Category:** General\
**Created:** [18 September 2022 23:27 UTC](https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900 "2022-09-18T23:27:46Z")\
**Posts on this page:** 6\
**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:** [18 September 2022 23:27 UTC](https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900/1 "2022-09-18T23:27:46Z")

</div>

I am confused with what is happening here.

Depending in the CPU temperature and a bit of other stuff, I want a RGB LED to blink depending on the message received.

```auto
[{"id":"ca5e1e7f2227fa14","type":"subflow","name":"Blinker","info":"2021 12 7 \nA subflow to send pulsing messages at a given interval to make something *flash*.\n\nConfigurable by setting `environment` variables in the node's settings.\n\n\n# **Output toggle node to send alternating messages at a specified frequency.** \n# \n\n## 3 things need to be set to configure this subflow\n1 - `delay`: milliseconds between toggles (override with msg.delay)\n\n2 - `msg1`: text message for state 1 \n\n3 - `msg2`: text message for state 2 \n\n`msg.topic` is output and alternates between `msg1` and `msg2` to indicate which message is being sent.\n\n## Optional:\n`offmsg` - sent when it is reset.\n\nMay be handy to make sure things are *off*.\n\n## Input message\nTo start send `msg` (anything)\n\nTo stop, send `msg.reset`","category":"","in":[{"x":50,"y":80,"wires":[{"id":"3a95f6976aee6fd0"}]}],"out":[{"x":920,"y":70,"wires":[{"id":"f3b2b2634e2662cd","port":1},{"id":"a655faf0bc379507","port":0}]}],"env":[{"name":"delay","type":"num","value":""},{"name":"msg1","type":"str","value":""},{"name":"msg2","type":"str","value":""},{"name":"offmsg","type":"str","value":""}],"meta":{"module":"Toggler","version":"0.1.1","desc":"Toggles output between `msg1` and `msg2` set in node config"},"color":"#A6BBCF","icon":"node-red-contrib-bool-gate/switch.png","status":{"x":920,"y":170,"wires":[{"id":"502c279ae9a6f385","port":0}]}},{"id":"c53ebcc9e62a7552","type":"trigger","z":"ca5e1e7f2227fa14","name":"resend every x","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-1","extend":false,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":430,"y":80,"wires":[["f3b2b2634e2662cd"]]},{"id":"f3b2b2634e2662cd","type":"function","z":"ca5e1e7f2227fa14","name":"toggle","func":"delete msg.delay;\nconst msg1 = {};\nlet c = context.get(\"counter\") ||0;\n\nif (c == 0) {\n // 0\n msg.payload = env.get(\"msg1\");\n msg.topic = \"msg1\";\n} else if (c == 1) {\n // 1\n msg.payload = env.get(\"msg2\");\n msg.topic = \"msg2\";\n};\n\nmsg1.payload = c;\nc = (c + 1) % 2;\ncontext.set(\"counter\",c);\nreturn [msg1,msg];","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":600,"y":80,"wires":[["502c279ae9a6f385"],[]]},{"id":"c183245c1a6a7fac","type":"switch","z":"ca5e1e7f2227fa14","name":"is reset?","property":"reset","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":410,"y":170,"wires":[["a9be61803cd67654","6e3a0cdb44f6a994"]]},{"id":"a9be61803cd67654","type":"switch","z":"ca5e1e7f2227fa14","name":"off msg?","property":"offmsg","propertyType":"env","rules":[{"t":"nempty"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":130,"wires":[["a655faf0bc379507"]]},{"id":"a655faf0bc379507","type":"change","z":"ca5e1e7f2227fa14","name":"set off message","rules":[{"t":"set","p":"payload","pt":"msg","to":"offmsg","tot":"env"},{"t":"delete","p":"reset","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":130,"wires":[[]]},{"id":"502c279ae9a6f385","type":"function","z":"ca5e1e7f2227fa14","name":"status","func":"const c = msg.payload;\n\nif (c == 0) {\n msg.payload = ({text: \"msg1\"});\n} else if (c == 1) {\n msg.payload = ({text: \"msg2\"});\n} else {\n msg.payload = ({text:\"\"});\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":170,"wires":[[]]},{"id":"6e3a0cdb44f6a994","type":"change","z":"ca5e1e7f2227fa14","name":"reset","rules":[{"t":"set","p":"payload","pt":"msg","to":"2","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":170,"wires":[["502c279ae9a6f385"]]},{"id":"3a95f6976aee6fd0","type":"function","z":"ca5e1e7f2227fa14","name":"set msg.delay","func":"msg.delay = msg.delay || env.get(\"delay\") || 1000;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":230,"y":80,"wires":[["c53ebcc9e62a7552","c183245c1a6a7fac"]]},{"id":"9126c47905fb62d6","type":"switch","z":"a1911aa3.c45be8","name":"`0`, `1` or `2` or `3`?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":4080,"y":3510,"wires":[["01d195b3bb026ad5","5a9373335f02567c"],["bffe8e032a5daccc","334c2f69d774311f"],["8bb0e3cfc93dbcff","01834d6e867462bb"],["a1eb96fc0265fbad","8889342d532bc722"]]},{"id":"c636d91bf15c92bc","type":"change","z":"a1911aa3.c45be8","name":"LED number to use","rules":[{"t":"set","p":"led","pt":"msg","to":"HEALTH_LED","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":3945,"y":3510,"wires":[["9126c47905fb62d6"]],"l":false},{"id":"01d195b3bb026ad5","type":"subflow:ca5e1e7f2227fa14","z":"a1911aa3.c45be8","name":"Clear","env":[{"name":"delay","value":"800","type":"num"},{"name":"msg1","value":"10,0,10","type":"str"},{"name":"msg2","value":"0,0,6","type":"str"},{"name":"offmsg","value":"0,0,0","type":"str"}],"x":4350,"y":3460,"wires":[["7dd57846c3c570ce"]]},{"id":"5a9373335f02567c","type":"change","z":"a1911aa3.c45be8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4245,"y":3460,"wires":[["bffe8e032a5daccc","8bb0e3cfc93dbcff","a1eb96fc0265fbad"]],"l":false},{"id":"bffe8e032a5daccc","type":"subflow:ca5e1e7f2227fa14","z":"a1911aa3.c45be8","name":"Warn","env":[{"name":"delay","value":"1500","type":"num"},{"name":"msg1","value":"10,0,10","type":"str"},{"name":"msg2","value":"15,2,0","type":"str"},{"name":"offmsg","value":"0,0,0","type":"str"}],"x":4350,"y":3500,"wires":[["7dd57846c3c570ce"]]},{"id":"334c2f69d774311f","type":"change","z":"a1911aa3.c45be8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4245,"y":3500,"wires":[["8bb0e3cfc93dbcff","01d195b3bb026ad5","a1eb96fc0265fbad"]],"l":false},{"id":"8bb0e3cfc93dbcff","type":"subflow:ca5e1e7f2227fa14","z":"a1911aa3.c45be8","name":"Overheat","env":[{"name":"delay","value":"800","type":"num"},{"name":"msg1","value":"15,0,10","type":"str"},{"name":"msg2","value":"15,0,0","type":"str"},{"name":"offmsg","value":"0,0,0","type":"str"}],"x":4360,"y":3540,"wires":[["7dd57846c3c570ce"]]},{"id":"01834d6e867462bb","type":"change","z":"a1911aa3.c45be8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4245,"y":3540,"wires":[["bffe8e032a5daccc","01d195b3bb026ad5","a1eb96fc0265fbad"]],"l":false},{"id":"a1eb96fc0265fbad","type":"subflow:ca5e1e7f2227fa14","z":"a1911aa3.c45be8","name":"Fan Fail","env":[{"name":"delay","value":"1000","type":"num"},{"name":"msg1","value":"6,0,6","type":"str"},{"name":"msg2","value":"6,6,0","type":"str"},{"name":"offmsg","value":"0,0,0","type":"str"}],"x":4360,"y":3580,"wires":[["7dd57846c3c570ce"]]},{"id":"8889342d532bc722","type":"change","z":"a1911aa3.c45be8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"reset","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4245,"y":3580,"wires":[["01d195b3bb026ad5","bffe8e032a5daccc","8bb0e3cfc93dbcff"]],"l":false},{"id":"6094a497e4d8ebaa","type":"switch","z":"a1911aa3.c45be8","name":"Reset?","property":"reset","propertyType":"msg","rules":[{"t":"eq","v":"reset","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":3875,"y":3510,"wires":[["bffe8e032a5daccc","8bb0e3cfc93dbcff","01d195b3bb026ad5","a1eb96fc0265fbad"],["c636d91bf15c92bc"]],"l":false},{"id":"7dd57846c3c570ce","type":"change","z":"a1911aa3.c45be8","name":"","rules":[{"t":"set","p":"wipe","pt":"msg","to":"OVERWRITE","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":4525,"y":3500,"wires":[["416b3856d85d0540"]],"l":false},{"id":"99d39868512bbb84","type":"inject","z":"a1911aa3.c45be8","name":"Reset","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":3740,"y":3660,"wires":[["6094a497e4d8ebaa"]]},{"id":"f906d60e4d4b549b","type":"inject","z":"a1911aa3.c45be8","name":"0","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":3740,"y":3700,"wires":[["6094a497e4d8ebaa"]]},{"id":"c77cc5998ba66449","type":"inject","z":"a1911aa3.c45be8","name":"1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":3740,"y":3740,"wires":[["6094a497e4d8ebaa"]]},{"id":"8505b3f82b02dd86","type":"inject","z":"a1911aa3.c45be8","name":"2","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":3740,"y":3780,"wires":[["6094a497e4d8ebaa"]]},{"id":"3f87036ffe169531","type":"inject","z":"a1911aa3.c45be8","name":"3","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":3740,"y":3820,"wires":[["6094a497e4d8ebaa"]]},{"id":"416b3856d85d0540","type":"function","z":"a1911aa3.c45be8","name":"","func":"var a = flow.get(\"HEALTH_LED\");\nmsg.payload = \"rgb,\"+a+\",\"+msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":4630,"y":3500,"wires":[["7251b8fadcd5a291"]]},{"id":"7251b8fadcd5a291","type":"debug","z":"a1911aa3.c45be8","name":"RGB to LED","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":4740,"y":3670,"wires":[]}]

```

There are 4 input messages:  
0  
1  
2  
3  
and RESET

`reset` is used to turn off any/all indications.  
`0` means all is clear and is an indication a high temperature was detected but since then all is good.  
`1` the temperature is approaching shutdown temperature.  
`2` the temperature has remained high for a time and so isn't about to go down and so the machine will be shutting down.  
`3` is an indicator that the fan hasn't started. Ideally the fan comes on below this temperature and so it wouldn't get here. This is a _prewarning_ that things may be about to get worse.

The _blink_ times are about 2 seconds on all of them.  
But when the fan _stalls_ (and when it it started and gets the temperature back down to where it should be, the blink rate is like `10` seconds.

But if I press the `inject` nodes and simulate the inputs: things work fine.

This bit of code does the conditions `0` and `3`.

```auto
[{"id":"be31bc3135a585f5","type":"switch","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"47 / 40","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"47","vt":"num"},{"t":"lt","v":"40","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":2410,"y":240,"wires":[["b7798f7274bdbc6b"],["0d25769f6d5a0465","fa338c35c48fa03c"]]},{"id":"b7798f7274bdbc6b","type":"gate","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":2565,"y":230,"wires":[["f732d8609971a705"]],"l":false},{"id":"f732d8609971a705","type":"change","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"close","rules":[{"t":"set","p":"payload","pt":"msg","to":"close","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2625,"y":230,"wires":[["b7798f7274bdbc6b","84262946c1391dc1","60e4a1fff9b5f459","fa338c35c48fa03c"]],"l":false},{"id":"60e4a1fff9b5f459","type":"change","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"3","tot":"num"},{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2705,"y":230,"wires":[["2413e5b597dd5888","57d1d918035e5ebb","7cd5cc4e21ddb40f"]],"l":false},{"id":"0d25769f6d5a0465","type":"delay","z":"9b7e7466.a4b698","g":"16280080c89be404","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":2445,"y":290,"wires":[["46028b210df8f537"]],"l":false},{"id":"46028b210df8f537","type":"change","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"open","rules":[{"t":"set","p":"payload","pt":"msg","to":"open","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"control","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2565,"y":290,"wires":[["b7798f7274bdbc6b","fa338c35c48fa03c"]],"l":false},{"id":"fa338c35c48fa03c","type":"gate","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"NOTE commands","controlTopic":"control","defaultState":"closed","openCmd":"close","closeCmd":"open","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","persist":false,"storeName":"memory","x":2625,"y":290,"wires":[["4b0bbbd0ea330f4e","fa1fa4d1c61dd7f4"]],"l":false},{"id":"4b0bbbd0ea330f4e","type":"change","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"},{"t":"delete","p":"topic","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":2705,"y":290,"wires":[["2413e5b597dd5888","7cd5cc4e21ddb40f","57d1d918035e5ebb"]],"l":false},{"id":"57d1d918035e5ebb","type":"debug","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"message","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2850,"y":290,"wires":[]},{"id":"923bf6b12abe8bfc","type":"inject","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"Fan fail","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":2400,"y":360,"wires":[["b7798f7274bdbc6b"]]},{"id":"f883787e2cb1ae29","type":"inject","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"Fan ok","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":2400,"y":410,"wires":[["0d25769f6d5a0465","fa338c35c48fa03c"]]},{"id":"48bc62ec79fd7732","type":"inject","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"Rest","props":[{"p":"reset","v":"reset","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":2400,"y":460,"wires":[["2413e5b597dd5888"]]},{"id":"2413e5b597dd5888","type":"link out","z":"9b7e7466.a4b698","g":"16280080c89be404","name":"Overheat message to LED","mode":"link","links":["64e96bee6cfc04ae","d8bb3a2db5b434ad"],"x":2910,"y":230,"wires":[],"l":true}]

```

The `link out` node goes to the input of that first block of code.

Conditions `1` and `2` are usually averted as I see the _Fan stalled_ indication and go and give it a nudge and it is ok. (That's a whole other problem, I'm getting a new fan but ITMT)

And `reset` is when things have settled down and I don't need any blinking LEDs.

_Normal_ temperaure range (with fan working) is 39 - 44 deg c.

So if it gets above 47, it sets off the _Fan stalled_ warning and maintains it until the temperature gets back below 40.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [19 September 2022 01:11 UTC](https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900/2 "2022-09-19T01:11:57Z")

</div>

Shouldn't the inject nodes have value of temperature as inject payload instead of timestamp ? (except for `msg.reset`), and the inject nodes also should be feeding the `switch` node marked (47/40) to simulate the real inputs from field.

---

<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:** [19 September 2022 04:08 UTC](https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900/3 "2022-09-19T04:08:57Z")

</div>

Ah, ok.

The `0` `1` `2` `3` values are set by the temperatures being at/above (or below) a value.

I know that can be seen as messy, but this way I don't have to look at the temperature's value to know what is happening. But the payload tells me.

In the second part/bit of code the `timestamp` is replaced with the value needed in the `change` node.  
So the `inject` is only injecting a message which is then handled/changed in the next node.

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [19 September 2022 05:07 UTC](https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900/4 "2022-09-19T05:07:02Z")

</div>

Its just my OCD kicked in. so did you find out what is the issue?

---

<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:** [19 September 2022 05:10 UTC](https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900/5 "2022-09-19T05:10:24Z")

</div>

No.

Still confused why when things go really wrong, the times are wrong for the blinking.

No problems with OCD. 😉

---

<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:** [18 November 2022 05:10 UTC](https://discourse.nodered.org/t/this-code-works-differently-when-i-test-it-than-when-it-gets-a-real-message/67900/6 "2022-11-18T05:10:41Z")

</div>

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