Closing file after use of node-red : file

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

[
    {
        "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": [
            []
        ]
    }
]

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.

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

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

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...):

[{"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":[[]]}]

How exactly are you checking whether it has been overwritten?

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...

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.

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...

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

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