Function node stopping

#1

Im having issues with having my node to continually run. It seems to run only one time and until i redeploy it wont run again.

[{"id":"8346c397.c91db","type":"function","z":"6c09d71.3976f28","name":"Boiler Relay 1","func":"var upperThreshold = 140;\nvar lowerThreshold = 130;\n\n\nvar previousTemp = context.previousTemp || \n        (upperThreshold - lowerThreshold)/2;\n        \nvar temp = msg.payload;\n\ncontext.previousTemp = temp;\n\n\n// Check if the new temp is different from the previous\nif (previousTemp != temp){\n\tif ((previousTemp < upperThreshold) \n\t    && (temp >= upperThreshold)){\n\t\tmsg.payload = \"2\";\n\t\treturn msg;\n\t}\n\telse if ((previousTemp > lowerThreshold) \n\t && (temp <= lowerThreshold)){\n\t\tmsg.payload = \"\";\n\t\treturn msg;\n\t}\n}","outputs":1,"noerr":0,"x":1547.142951965332,"y":1361.4286365509033,"wires":[["2f7037af.2dda38"]]}]
0 Likes

#2

Check that the node is receiving message by using a debug node.

If it is then use logging events with your code to check what is happening

https://nodered.org/docs/writing-functions#logging-events

0 Likes

#3

using the debug before it keeps sending info to it. i already checked that. i also tried using a message as a string and as a number.

0 Likes

#4

You node does not return anything if previousTemp == temp or if neither of the two if statements is true. Therefore I deduce that one of those conditions is happening. You can use node.warn() statements to debug your code to find out what is happening. For example after the first section you can insert lines like

node.warn("temp is " + temp);

and that information will be displayed in the debug pane. Insert such statements at various points to see what is happening.
You should change to the current way of accessing the context, using context.get and context.set. At some point the old method may be removed.
Also I note that there are yellow triangles with exclamation marks against two lines suggesting syntax errors. I don't know what the code that you have will do as you have it, you should have the && on the line before.

0 Likes

#5

so theres nothing i can add that would just tell the code to keep running?

0 Likes

#6

What do you mean by 'keep running'? The code is run each time the Function node receives a message. Your code will send on a message under a couple of conditions you have in the code, other wise it doesn't return anything and the flow will stop.

0 Likes

#7

about once a minute the Arduino refreshes the sensor data. so 1 time a min it re sends the updated temperature. The issue seems is that the function im using only shows a output on the first time of seeing data. After that i can change whats being sent to the node and nothing new or different comes out. Almost as if the code inside it runs 1 time and stops. I even tried using just a inject to test and had no change.

0 Likes

#8

The reason nothing comes out is because it is getting to the end (where there is no return statement so it does not send a message) instead of one of the return msg statements.
As I said, put some warn statements in and see where the flow is going.
Have you corrected the errors in the code I pointed out that are highlighted with the yellow triangles?

0 Likes

#9

I have just looked at the code and it will only send a message when the temperature crosses upwards over the upper threshold or downwards across the lower threshold. It looks as if this is the intention but perhaps not.

0 Likes