Enabling / disabling writing data to a file using a dashboard switch node

Dear Experts,

I have created a flow where I can store data into a file and it already works as expected.

But now I want to enable / disable the logging using a simple switch (may be by blocking the entire message?) on the dashboard and I've been looking for a solution for hours now again and now I can no longer!

Can you please help me with a few concrete hints?
I am sure, that in the end it is really easy. But I can't figure it out ...

Here is a screenshot of my flow...

Thank you!!!!

Didnt your other thread solve this?

No! Or do you see a 'switch' and a 'write file' node on this screenshot?

I do not want to disable the flow or the injection, I want to disable the logging only...

No, but that is completely irrelevant.

For example several of the solutions offered in that thread show you how to block the flow of a msg.

So using the same process, you can block these log messages reaching the file node!

If you cant see that then post your flow and I'll show you.

Hi Stephen,
first thank you very much for your quick response!
I think I understand what you mean. In German you say "Viele Wege führen nach Rom!"...
But currently I can not see it.

But I will check this again and come back

  • with a solution or
  • a new question or
  • with a copy of my code or
  • I don't know...

But it will take some time.
Playing around with Node Red is (unfortunately?) not my main activity...


We in UK also use "Many roads lead to Rome"

NOTE: I recommend the 2nd option in this thread: Cancel inputs for a period of time - #6 by Steve-Mcl - because I can help you with it & it doesnt use contrib nodes.

If you were to use that ↑ demo then you would place the gate in series between the function and file node to block / allow the flow of messages. And instead of injects to control the gate, use your dashboard switch to send the necessary values to "flip" the gate open / closed.

I tried... no success!

Your code in gate is using msg.topic == "_block_toggle".
But in dashboard switch I do not have a msg.topic.


You can change the comparison to anything that suits you.

I do not see the problem. Set a flow variable with the dashboard switch node. Then put a switch node as filter before the write file node. Where is the problem??

[{"id":"6f9ecff51b3542f4","type":"ui_switch","z":"88a88855e07a9bcf","name":"","label":"switch","tooltip":"","group":"8439522ea5fad93c","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":930,"y":180,"wires":[["8868a7d0e6140a88"]]},{"id":"8868a7d0e6140a88","type":"change","z":"88a88855e07a9bcf","name":"","rules":[{"t":"set","p":"logging","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":180,"wires":[[]]},{"id":"51139ba4f58d1264","type":"switch","z":"88a88855e07a9bcf","name":"allow logging","property":"logging","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":300,"wires":[["2388402aadeea722"]]},{"id":"2388402aadeea722","type":"file","z":"88a88855e07a9bcf","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":1090,"y":300,"wires":[[]]},{"id":"da8487fee9b7db7c","type":"function","z":"88a88855e07a9bcf","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":730,"y":300,"wires":[["51139ba4f58d1264"]]},{"id":"8439522ea5fad93c","type":"ui_group","name":"Test","tab":"49e9f4d45b15375e","order":1,"disp":true,"width":"6","collapse":false},{"id":"49e9f4d45b15375e","type":"ui_tab","name":"Test","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

@mickym2 : The main problem with all forums is:

  • If you are somehow an "expert" then your comment / description might be helpful
  • If you are an absolute beginner it can happen that the comment / description is simply not understood

But I am already on a good way using the solution from @Steve-Mcl .
Still struggling to implement the correct initial state: "writing disabled"...

But your solution sounds easier.... I will have a look on it...


As you can see in the screenshot I have now somehow manged the issue by using your solution.
But the logic is still wrong (unblocked when "logging is switched off" and blocked when "logging is switched on"). And I can't figure out how to change it.

Here is the code:

[{"id":"f8a5b7e1c332a045","type":"tab","label":"Spielwiese","disabled":false,"info":"","env":[]},{"id":"68cf734df0af2b1b","type":"inject","z":"f8a5b7e1c332a045","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"0.5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":230,"y":200,"wires":[["2b5a4837b7a3778a"]]},{"id":"e9af5b33d982125b","type":"debug","z":"f8a5b7e1c332a045","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":610,"y":160,"wires":[]},{"id":"2b5a4837b7a3778a","type":"function","z":"f8a5b7e1c332a045","name":"gate","func":"var block = context.get(\"block\") || false;\n\n//if (msg.topic == \"_block_toggle\") {\nif (msg.payload == true) {\n\n    block = !block;\n    context.set(\"block\", block);\n    var stat = {\n        fill: block ? \"red\" : \"green\",\n        shape: block ? \"dot\": \"ring\",\n        text: block ? \"Blocked\" : \"Unblocked\"\n    }\n    node.status(stat)\n}\n\nif(block) return null; //halt flow\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":160,"wires":[["e9af5b33d982125b"]]},{"id":"dc568e29ed2459b2","type":"ui_switch","z":"f8a5b7e1c332a045","name":"Switch Logging","label":"<font color=#FFFFFF size=\"4\"><strong>Logging</strong></font>","tooltip":"","group":"d15916e202a9929f","order":8,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"true","offvalueType":"bool","officon":"","offcolor":"","animate":false,"className":"","x":220,"y":140,"wires":[["2b5a4837b7a3778a"]]},{"id":"d15916e202a9929f","type":"ui_group","name":"Spielwiese","tab":"f045990709d4a630","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"f045990709d4a630","type":"ui_tab","name":"Spielwiese","icon":"dashboard","order":9,"disabled":false,"hidden":false}]

I will now try to test the solution from the Bavarian expert @mickym2 . Perhaps his code is in German

You may be disappointed. :wink: - The function node is only dummy to demonstrate where the switch node has to be placed. So my function node is a dummy and should be replaced with your function node.

The logic is still simple:
With the switch - I set a flow variable logging to true or false - dependend on the switch.
The switch node in front of your file node blocks all messages if the flow variable logging isn't true. :wink:

If you need to initialize your flow variable to a specific value then use an inject node which act as input as well and sends only one message after node-red is started. In this case you can use pass through of the dashboard switch.

[{"id":"b4c0206a2b9d36ce","type":"ui_switch","z":"c451086a0228abe0","name":"","label":"switch","tooltip":"","group":"8439522ea5fad93c","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":630,"y":180,"wires":[["7bf1ce8f689ece25"]]},{"id":"7bf1ce8f689ece25","type":"change","z":"c451086a0228abe0","name":"","rules":[{"t":"set","p":"logging","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":820,"y":180,"wires":[[]]},{"id":"99bd263a70cdef9d","type":"switch","z":"c451086a0228abe0","name":"allow logging","property":"logging","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":610,"y":300,"wires":[["268359bb89df44fe"]]},{"id":"109c875874c35d74","type":"file","z":"c451086a0228abe0","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":790,"y":300,"wires":[[]]},{"id":"a8b46906379c4b31","type":"inject","z":"c451086a0228abe0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":420,"y":320,"wires":[["99bd263a70cdef9d"]]},{"id":"268359bb89df44fe","type":"debug","z":"c451086a0228abe0","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":360,"wires":[]},{"id":"d29dcbc8a62e4387","type":"inject","z":"c451086a0228abe0","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":490,"y":180,"wires":[["b4c0206a2b9d36ce"]]},{"id":"8439522ea5fad93c","type":"ui_group","name":"Test","tab":"49e9f4d45b15375e","order":1,"disp":true,"width":"6","collapse":false},{"id":"49e9f4d45b15375e","type":"ui_tab","name":"Test","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

The difference beetween my solution and @mickym2 is that mine is portable (can be re-used) & has visual feedback (i.e. status in the editor). Like in mickym2s solution, when using a flow variable, if you want a 2nd or 3rd gate, you need to change all the flow variable names - however it is simpler to understand..

Anyhow, i imagine this is what you want...




        "id": "68cf734df0af2b1b",
        "type": "inject",
        "z": "f8a5b7e1c332a045",
        "name": "",
        "props": [
                "p": "payload"
                "p": "topic",
                "vt": "str"
        "repeat": "0.5",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 278,
        "y": 192,
        "wires": [
        "id": "e9af5b33d982125b",
        "type": "debug",
        "z": "f8a5b7e1c332a045",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 630,
        "y": 192,
        "wires": []
        "id": "2b5a4837b7a3778a",
        "type": "function",
        "z": "f8a5b7e1c332a045",
        "name": "gate",
        "func": "var block = context.get(\"block\") || false;\n\n//if (msg.topic == \"_block_toggle\") {\nif (msg.topic == \"block\") {\n    block = !!msg.payload;\n    context.set(\"block\", block);\n    var stat = {\n        fill: block ? \"red\" : \"green\",\n        shape: block ? \"dot\": \"ring\",\n        text: block ? \"Blocked\" : \"Unblocked\"\n    }\n    node.status(stat)\n    return null; //halt flow\n}\n\nif(block) return null; //halt flow\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 450,
        "y": 192,
        "wires": [
        "id": "dc568e29ed2459b2",
        "type": "ui_switch",
        "z": "f8a5b7e1c332a045",
        "name": "Add to log",
        "label": "<font color=\"#000\" size=\"4\"><strong>Add to log</strong></font>",
        "tooltip": "",
        "group": "d15916e202a9929f",
        "order": 8,
        "width": 0,
        "height": 0,
        "passthru": true,
        "decouple": "false",
        "topic": "block",
        "topicType": "str",
        "style": "",
        "onvalue": "false",
        "onvalueType": "bool",
        "onicon": "",
        "oncolor": "",
        "offvalue": "true",
        "offvalueType": "bool",
        "officon": "",
        "offcolor": "",
        "animate": false,
        "className": "",
        "x": 252,
        "y": 288,
        "wires": [
        "id": "8ce9b9d926ad212d",
        "type": "ui_text",
        "z": "f8a5b7e1c332a045",
        "group": "d15916e202a9929f",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "text",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 610,
        "y": 128,
        "wires": []
        "id": "d15916e202a9929f",
        "type": "ui_group",
        "name": "Spielwiese",
        "tab": "f045990709d4a630",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
        "id": "f045990709d4a630",
        "type": "ui_tab",
        "name": "Spielwiese",
        "icon": "dashboard",
        "order": 9,
        "disabled": false,
        "hidden": false

My visual feedback - is the context menu - just a joke. :wink:

Es ist zum ... ich krieg's nicht hin.
Deine Lösung sieht so einfach aus, aber das Loggen wird nicht gestopp:

Was mache ich falsch?

Zeig mal Dein Kontext Menü -


Ja das ist leer... da muss wohl noch was rein. Oder? Keine Ahnung leider...

Na dann hast Du nicht initialisiert oder den switch noch nicht betätigt. Nutz den Flow zum Schluss da initialisiere ich den switch mit einer inject Node. Und es ist solange leer - Du musst refreshen:


OK, I will try ......

The switch node - is very simple - just looking if flow variable logging is true. If this is not the you must initialize the flow variable with the dashboard switch or Inject Node - and it used boolean true for on and boolean false for off.


The flow varibale is set by the next change node: