Skip to content

Instantly share code, notes, and snippets.

@juggledad
Created July 28, 2018 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juggledad/2ec32b279faddcb3c4754df7bd10a4d1 to your computer and use it in GitHub Desktop.
Save juggledad/2ec32b279faddcb3c4754df7bd10a4d1 to your computer and use it in GitHub Desktop.
EXAMPLE - turning a node's status 'on' and 'off'

The following example will show how to turn a node's status on and off. This example uses an inject node to start the process connected to a function node that has a deliberate error (a variable is undefined) connected to a debug node (which never activates because of the error.)

A 'catch' node catches the error and pass it to a function node that sets the function node's status to a red dot with the text of 'ERROR'. The function node is attached to a debug node which displays the error message.

The 'catch' node is also connected to a delay node (a 3 second delay) which is connected to a change node that sets msg.topic to 'clear'. The change node is connected to the function node that has the status displaying.

The key is in the function node. It checks node.topic to see if it is "clear" - if it is, it clears node.status and returns without sending a msg otherwise it sets the node status and msg.topic anad goes on it's merry way.

// check to see if status should be cleared
if (msg.topic == "clear") {
    // YES so clear status and exit without sending a msg.
    node.status({});
    return;
} else {
    // NO, set status, msg.topis and return msg
    node.status({fill:"red",shape:"dot",text:"ERROR"});
    msg.topic = "Logging flow";
}
return msg;
[{"id":"40e605db.1646c4","type":"tab","label":"DEMO: set and clear status","disabled":false,"info":""},{"id":"13ca9dda.22db62","type":"inject","z":"40e605db.1646c4","name":"","topic":"","payload":"Cause an error","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":260,"y":180,"wires":[["878e49af.7d025"]]},{"id":"977f6cf6.9a1b5","type":"function","z":"40e605db.1646c4","name":"Display status","func":"// check to see if status should be cleared\nif (msg.topic == \"clear\") {\n // YES so clear status and exit without sending a msg.\n node.status({});\n return;\n} else {\n // NO, set status, msg.topis and return msg\n node.status({fill:\"red\",shape:\"dot\",text:\"ERROR\"});\n msg.topic = \"Logging flow\";\n}\nreturn msg;","outputs":1,"noerr":0,"x":720,"y":300,"wires":[["74767bb3.5f098c"]]},{"id":"878e49af.7d025","type":"function","z":"40e605db.1646c4","name":"This will throw an error","func":"var n = 23 + 56 / a;\n//var n = 23 + 56 ;\nmsg.payload = n;\nreturn msg;","outputs":1,"noerr":0,"x":500,"y":180,"wires":[["42449687.d4d5c8"]]},{"id":"42449687.d4d5c8","type":"debug","z":"40e605db.1646c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":710,"y":180,"wires":[]},{"id":"acf57cfc.609288","type":"delay","z":"40e605db.1646c4","name":"","pauseType":"delay","timeout":"3","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":360,"y":240,"wires":[["44577add.6bf58c"]]},{"id":"44577add.6bf58c","type":"change","z":"40e605db.1646c4","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"clear","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":240,"wires":[["977f6cf6.9a1b5"]]},{"id":"e426ad6.a61c05","type":"catch","z":"40e605db.1646c4","name":"","scope":null,"x":200,"y":300,"wires":[["acf57cfc.609288","977f6cf6.9a1b5"]]},{"id":"74767bb3.5f098c","type":"debug","z":"40e605db.1646c4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":910,"y":300,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment