# Closing file after use of node-red : file

**URL:** <https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763>\
**Category:** General\
**Created:** [6 April 2021 12:57 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763 "2021-04-06T12:57:26Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dirk\_L](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@Dirk\_L](https://discourse.nodered.org/u/Dirk_L)\
**Post date:** [6 April 2021 12:57 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/1 "2021-04-06T12:57:27Z")

</div>

Hi @all out there!

I am nearly new on red-node. Or it seems to me as if...

I am using node-red : file to write data in a file. Fine and it works.

But (There will allways be a but..).

The file seems not to be updated in my file system (RP4). After waiting, maybe after the flows has ended or started again, the file updates. I also tried to send the filename via msg.filename, same effect.

Question: Can I force close file? Or where is my fault? Only a slow raspberry?  
(See the flow for testing below...)

Thanks f reading and any hint!

Dirk

```auto
[
    {
        "id": "3b49d206.22100e",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "63d0071f.d1f498",
        "type": "inject",
        "z": "3b49d206.22100e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 80,
        "wires": [
            [
                "e3b88583.644ca8"
            ]
        ]
    },
    {
        "id": "cecce8d5.6464d8",
        "type": "file",
        "z": "3b49d206.22100e",
        "name": "",
        "filename": "//etc/openhab2/html/nr_message_001.html",
        "appendNewline": false,
        "createDir": false,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 570,
        "y": 220,
        "wires": [
            [
                "e76365a3.a55b28",
                "783926c.f311ed8"
            ]
        ]
    },
    {
        "id": "3ee8c328.aa0bdc",
        "type": "function",
        "z": "3b49d206.22100e",
        "name": "set Text in iFrame",
        "func": "let textValue = \"\"\ntextValue=\"<!doctype html>\"\ntextValue+=\"<html>\"\ntextValue+=\"<head>\"\ntextValue+=\"<meta charset='utf-8'>\"\ntextValue+=\"<meta name='viewport# content='width=device-width, initial-scale=1.0'>\"\ntextValue+=\"<title>Beschreibung der Seite (erscheint in der Titelzeile des Browsers)</title>\"\ntextValue+=\"</head>\"\ntextValue+=\"<body>\"\ntextValue+=\"<p>\"\n\nif (global.get('timerTime') >= 0)\n{\n textValue+=\"Timer: \" + global.get('timerTime');\n global.set('timerTime', global.get('timerTime')-1);\n}\nelse\n textValue+=\"Timer ist abgelaufen!\" + global.get('timerTime');\n\ntextValue+=\"</p>\"\ntextValue+=\"</body>\"\ntextValue+=\"</html>\";\nlet msg1 = {payload: textValue};\n//msg1 = {filename: \"//etc/openhab2/html/nr_message_001.html\"};\nlet msg2 = {payload: null};\nif (global.get('timerTime') > 0)\n msg2 = {payload: global.get('timerTime')};\nreturn [msg1,msg2];",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 210,
        "y": 220,
        "wires": [
            [
                "cecce8d5.6464d8",
                "103ccd57.3e4593"
            ],
            [
                "32205b50.ad7bc4",
                "d51da409.3506b8"
            ]
        ]
    },
    {
        "id": "85f312ec.288ef",
        "type": "delay",
        "z": "3b49d206.22100e",
        "name": "",
        "pauseType": "delay",
        "timeout": "6",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "minute",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "x": 600,
        "y": 140,
        "wires": [
            [
                "3ee8c328.aa0bdc"
            ]
        ]
    },
    {
        "id": "103ccd57.3e4593",
        "type": "debug",
        "z": "3b49d206.22100e",
        "name": "m_01",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 310,
        "y": 300,
        "wires": []
    },
    {
        "id": "32205b50.ad7bc4",
        "type": "function",
        "z": "3b49d206.22100e",
        "name": "Trigger delay if needed again",
        "func": "if (msg.payload != null)\n return msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 240,
        "y": 140,
        "wires": [
            [
                "85f312ec.288ef"
            ]
        ]
    },
    {
        "id": "e3b88583.644ca8",
        "type": "function",
        "z": "3b49d206.22100e",
        "name": "set delay for test to 15 ",
        "func": "global.set('timerTime', 15);\nmsg.payload = 15;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 360,
        "y": 80,
        "wires": [
            [
                "85f312ec.288ef"
            ]
        ]
    },
    {
        "id": "d51da409.3506b8",
        "type": "debug",
        "z": "3b49d206.22100e",
        "name": "m_02",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 310,
        "y": 340,
        "wires": []
    },
    {
        "id": "e76365a3.a55b28",
        "type": "debug",
        "z": "3b49d206.22100e",
        "name": "m_03",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 770,
        "y": 300,
        "wires": []
    },
    {
        "id": "783926c.f311ed8",
        "type": "function",
        "z": "3b49d206.22100e",
        "name": "",
        "func": "\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 540,
        "y": 320,
        "wires": [
            []
        ]
    }
]

```

---

<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:** [6 April 2021 13:16 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/2 "2021-04-06T13:16:27Z")

</div>

Why have you got two forward slashes at the start of the filename. I don't expect that to make a difference but you should correct it anyway.

In the help text for the node it says  
" If `msg.filename` is used the file will be closed after every write. For best performance use a fixed filename."  
So I suggest emptying the filename field in the node and adding `msg.filename` to the message you are passing in and see if that sorts it.

---

<div class="post-metadata">

**Author:** ![Dirk\_L](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@Dirk\_L](https://discourse.nodered.org/u/Dirk_L)\
**Post date:** [6 April 2021 13:22 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/3 "2021-04-06T13:22:38Z")

</div>

Hi Colin,

thanks' for your reply. Did both things, no change. Any other idea?

May be of inrestet: I am using Version 1.2.6

Kind regards

Dirk

---

<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:** [6 April 2021 13:47 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/4 "2021-04-06T13:47:10Z")

</div>

Can you post your current flow, using msg.filename?  
When you export the flow select Compact at the bottom of the window and it takes up less room in the post.

Edit: Also, exactly how are you checking that it has not been overwritten?  
More Edit: Also probably worth upgrading node-red just in case.

Go to your .node-red folder and run  
`sudo npm install -g --unsafe-perm node-red`

---

<div class="post-metadata">

**Author:** ![Dirk\_L](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@Dirk\_L](https://discourse.nodered.org/u/Dirk_L)\
**Post date:** [6 April 2021 14:00 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/5 "2021-04-06T14:00:19Z")

</div>

I am careful with updates/upgrades since my openHab broke... I will have to do a backup upfornt...

Edit: I check the file via file explorer. Loking at Date/time and content.

And here is the changed flow (reduced it to two tries...):

```auto
[{"id":"3b49d206.22100e","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"63d0071f.d1f498","type":"inject","z":"3b49d206.22100e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":80,"wires":[["e3b88583.644ca8"]]},{"id":"cecce8d5.6464d8","type":"file","z":"3b49d206.22100e","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":470,"y":220,"wires":[["e76365a3.a55b28","783926c.f311ed8"]]},{"id":"3ee8c328.aa0bdc","type":"function","z":"3b49d206.22100e","name":"set Text in iFrame","func":"let textValue = \"\"\ntextValue=\"<!doctype html>\"\ntextValue+=\"<html>\"\ntextValue+=\"<head>\"\ntextValue+=\"<meta charset='utf-8'>\"\ntextValue+=\"<meta name='viewport# content='width=device-width, initial-scale=1.0'>\"\ntextValue+=\"<title>Beschreibung der Seite (erscheint in der Titelzeile des Browsers)</title>\"\ntextValue+=\"</head>\"\ntextValue+=\"<body>\"\ntextValue+=\"<p>\"\n\nif (global.get('timerTime') >= 0)\n{\n textValue+=\"Timer: \" + global.get('timerTime');\n global.set('timerTime', global.get('timerTime')-1);\n}\nelse\n textValue+=\"Timer ist abgelaufen!\" + global.get('timerTime');\n\ntextValue+=\"</p>\"\ntextValue+=\"</body>\"\ntextValue+=\"</html>\";\nlet msg1 = {payload: textValue, \n filename: \"//etc/openhab2/html/nr_message_001.html\"};\nlet msg2 = {payload: null};\nif (global.get('timerTime') > 0)\n msg2 = {payload: global.get('timerTime')};\nreturn [msg1,msg2];","outputs":2,"noerr":0,"initialize":"","finalize":"","x":210,"y":220,"wires":[["cecce8d5.6464d8","103ccd57.3e4593"],["32205b50.ad7bc4","d51da409.3506b8"]]},{"id":"85f312ec.288ef","type":"delay","z":"3b49d206.22100e","name":"","pauseType":"delay","timeout":"8","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":600,"y":140,"wires":[["3ee8c328.aa0bdc"]]},{"id":"103ccd57.3e4593","type":"debug","z":"3b49d206.22100e","name":"m_01","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":350,"y":340,"wires":[]},{"id":"32205b50.ad7bc4","type":"function","z":"3b49d206.22100e","name":"Trigger delay if needed again","func":"if (msg.payload != null)\n return msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":240,"y":140,"wires":[["85f312ec.288ef"]]},{"id":"e3b88583.644ca8","type":"function","z":"3b49d206.22100e","name":"set delay for test to 15 ","func":"global.set('timerTime', 2);\nmsg.payload = 2;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":80,"wires":[["85f312ec.288ef"]]},{"id":"d51da409.3506b8","type":"debug","z":"3b49d206.22100e","name":"m_02","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":170,"y":360,"wires":[]},{"id":"e76365a3.a55b28","type":"debug","z":"3b49d206.22100e","name":"m_03","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":770,"y":300,"wires":[]},{"id":"783926c.f311ed8","type":"function","z":"3b49d206.22100e","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":320,"wires":[[]]}]

```

---

<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:** [6 April 2021 14:05 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/6 "2021-04-06T14:05:49Z")

</div>

How exactly are you checking whether it has been overwritten?

---

<div class="post-metadata">

**Author:** ![Dirk\_L](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@Dirk\_L](https://discourse.nodered.org/u/Dirk_L)\
**Post date:** [6 April 2021 14:07 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/7 "2021-04-06T14:07:31Z")

</div>

Srry forgt this in my last post:

I check the file via file explorer. Looking at Date/time and content.

I am using therefore a share done via samba on my rasperry.

And my o is hampering...

---

<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:** [6 April 2021 14:14 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/8 "2021-04-06T14:14:18Z")

</div>

Sometimes shares like that can be slow to update. Instead try ssh into the pi and run  
`ls -l /etc/openhab2/html/nr*`  
to check.  
You still have two slashes on the front.  
It works fine for me, though I am not using that version of node-red.

---

<div class="post-metadata">

**Author:** ![Dirk\_L](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@Dirk\_L](https://discourse.nodered.org/u/Dirk_L)\
**Post date:** [6 April 2021 14:16 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/9 "2021-04-06T14:16:41Z")

</div>

I will try ssh.

The two slashes mean that the path starts at root of filesystem. And the file comes up. Starting with one slash would be a relative path in relativ to node-red. I will try to find one test I did with one slash.

Did a test: One / does the same... I thought that I used this in my former MS-DOS time...

---

<div class="post-metadata">

**Author:** ![Dirk\_L](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@Dirk\_L](https://discourse.nodered.org/u/Dirk_L)\
**Post date:** [6 April 2021 14:26 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/10 "2021-04-06T14:26:41Z")

</div>

Yes, via SSH all files are done and the content ist fine. Looked via nano and saw that the content was changed. As I will later use a timer with one minute for each circle, this will not occur anymore I think...

Thanks for your help and your time!  
(I will mark your last post as solution...)

Dirk

---

<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:** [20 April 2021 14:27 UTC](https://discourse.nodered.org/t/closing-file-after-use-of-node-red-file/43763/11 "2021-04-20T14:27:19Z")

</div>

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