Append a timestamp to filename with switch trigger?

Hi, I use a dashboard switch to trigger the recording of data to a txt file. This part work.
Now I need help to append the timestamp to the filename.
I had a couple of problem that I partly solved.

First one. This is not similar to timestamping separetad pictures. In my case, once triggered, 1hz data are recorded in the file as long as the switch state is true. I need to append the timestamp to the filename only when I first trigger the switch. The flow below is a beginning to this. Every time I toggle the switch, the Change node not only output a true/false flow that control data recording but also output once the timestamp corresponding to the switch toggling time.
Then the convert time node format the timestamp to YYYY-MM-DD HH:MM:SS.
I would prefer YYYYMMDDHHMMSS but I don't understand how to do it. This is my first question.

The second aspect is to integrate this format timestamp into the associated filename. I actually rely on Path to store the data name DataB.txt in a folder. Instead I would like to obtain DataB_YYYYMMDDHHMMSS.txt
Can you help?

[
    {
        "id": "765d84d40cd1a292",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "ad97e887ff5f0bc4",
        "type": "ui_switch",
        "z": "765d84d40cd1a292",
        "name": "",
        "label": "Record switch",
        "tooltip": "",
        "group": "2444546f087153a2",
        "order": 11,
        "width": 0,
        "height": 0,
        "passthru": true,
        "decouple": "false",
        "topic": "topic",
        "topicType": "msg",
        "style": "",
        "onvalue": "true",
        "onvalueType": "bool",
        "onicon": "",
        "oncolor": "green",
        "offvalue": "false",
        "offvalueType": "bool",
        "officon": "",
        "offcolor": "red",
        "animate": false,
        "className": "",
        "x": 100,
        "y": 60,
        "wires": [
            [
                "ea6da3ad4c30ff74"
            ]
        ]
    },
    {
        "id": "ea6da3ad4c30ff74",
        "type": "change",
        "z": "765d84d40cd1a292",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "record",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "",
                "tot": "date"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 290,
        "y": 60,
        "wires": [
            [
                "519172c2362d78e7"
            ]
        ]
    },
    {
        "id": "519172c2362d78e7",
        "type": "function",
        "z": "765d84d40cd1a292",
        "name": "Convert timestamp",
        "func": "msg.payload = new Date().toISOString();\nmsg.payload = msg.payload.replace(/^(.*)T(.*)Z$/, \"$1 $2\");  // with the 'T' and 'Z' removed\nmsg.payload = msg.payload.substring(0, 19)\nmsg.topic = \"File_Time\"\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 60,
        "wires": [
            [
                "dc8f4120f9eb9cbc"
            ]
        ]
    },
    {
        "id": "dc8f4120f9eb9cbc",
        "type": "debug",
        "z": "765d84d40cd1a292",
        "name": "debug 2505",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 690,
        "y": 60,
        "wires": []
    },
    {
        "id": "982be7311e73e46e",
        "type": "switch",
        "z": "765d84d40cd1a292",
        "name": "True/false",
        "property": "record",
        "propertyType": "flow",
        "rules": [
            {
                "t": "true"
            },
            {
                "t": "false"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 280,
        "y": 180,
        "wires": [
            [
                "6902b885793e5ec2"
            ],
            []
        ]
    },
    {
        "id": "6902b885793e5ec2",
        "type": "file",
        "z": "765d84d40cd1a292",
        "name": "",
        "filename": "/data/dataB.txt",
        "filenameType": "str",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "utf8",
        "x": 500,
        "y": 160,
        "wires": [
            [
                "da05ce0f2f25af33"
            ]
        ]
    },
    {
        "id": "da05ce0f2f25af33",
        "type": "debug",
        "z": "765d84d40cd1a292",
        "name": "debug 2506",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 700,
        "y": 160,
        "wires": []
    },
    {
        "id": "e1e3bcdcf7dd2717",
        "type": "inject",
        "z": "765d84d40cd1a292",
        "name": "Data In",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "this is my data to be saved in the file",
        "payloadType": "str",
        "x": 100,
        "y": 180,
        "wires": [
            [
                "982be7311e73e46e"
            ]
        ]
    },
    {
        "id": "2444546f087153a2",
        "type": "ui_group",
        "name": "Default",
        "tab": "8e29298fdb5d2d76",
        "order": 1,
        "disp": true,
        "width": "12",
        "collapse": false,
        "className": ""
    },
    {
        "id": "8e29298fdb5d2d76",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

Thank you

If I understand correctly. Something like this may work for you.

[{"id":"ad97e887ff5f0bc4","type":"ui_switch","z":"765d84d40cd1a292","name":"","label":"Record switch","tooltip":"","group":"2444546f087153a2","order":11,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"green","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"red","animate":false,"className":"","x":100,"y":60,"wires":[["ea6da3ad4c30ff74"]]},{"id":"ea6da3ad4c30ff74","type":"change","z":"765d84d40cd1a292","name":"","rules":[{"t":"set","p":"record","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"filename","pt":"flow","to":"$moment().format(\"[my_file_name_]YYYYMMDDHHMMSS\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":60,"wires":[["dc8f4120f9eb9cbc"]]},{"id":"dc8f4120f9eb9cbc","type":"debug","z":"765d84d40cd1a292","name":"debug 2505","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":590,"y":60,"wires":[]},{"id":"e1e3bcdcf7dd2717","type":"inject","z":"765d84d40cd1a292","name":"Data In","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"this is my data to be saved in the file","payloadType":"str","x":90,"y":180,"wires":[["982be7311e73e46e"]]},{"id":"982be7311e73e46e","type":"switch","z":"765d84d40cd1a292","name":"True/false","property":"record","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":230,"y":180,"wires":[["f9418b4c552135f1"],[]]},{"id":"f9418b4c552135f1","type":"change","z":"765d84d40cd1a292","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"filename","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":180,"wires":[["6902b885793e5ec2"]]},{"id":"6902b885793e5ec2","type":"file","z":"765d84d40cd1a292","name":"","filename":"filename","filenameType":"msg","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"utf8","x":580,"y":160,"wires":[["da05ce0f2f25af33"]]},{"id":"da05ce0f2f25af33","type":"debug","z":"765d84d40cd1a292","name":"debug 2506","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":740,"y":160,"wires":[]},{"id":"2444546f087153a2","type":"ui_group","name":"Default","tab":"8e29298fdb5d2d76","order":1,"disp":true,"width":"12","collapse":false,"className":""},{"id":"8e29298fdb5d2d76","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

[edit] fixed typo

I am not sure how to combine my actual working directory : path: /data/my_file_name.txt
with the function you use (below).
Where should my working directory should be included?

$moment().format("[my_file_name_]YYYYMMDDHHMMSS")
$moment().format("[path/filename]YYYYMMDDHHMMSS[.txt]")

Ok this work. You are the best! Thank you very much.

[{"id":"765d84d40cd1a292","type":"tab","label":"Flow 2","disabled":false,"info":"","env":[]},{"id":"55dc6d787f2dc295","type":"ui_switch","z":"765d84d40cd1a292","name":"","label":"Record switch","tooltip":"","group":"2444546f087153a2","order":11,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"green","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"red","animate":false,"className":"","x":150,"y":60,"wires":[["2468c6583ae8dc9f"]]},{"id":"2468c6583ae8dc9f","type":"change","z":"765d84d40cd1a292","name":"","rules":[{"t":"set","p":"record","pt":"flow","to":"payload","tot":"msg"},{"t":"set","p":"filename","pt":"flow","to":"$moment().format(\"[/data/dataB_]YYYYMMDD_HHMMss[.txt]\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":60,"wires":[["5969ad585571a614"]]},{"id":"5969ad585571a614","type":"debug","z":"765d84d40cd1a292","name":"debug 2505","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":570,"y":60,"wires":[]},{"id":"19f30938bb62a75f","type":"inject","z":"765d84d40cd1a292","name":"Data In","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"sensor data to record","payloadType":"str","x":110,"y":180,"wires":[["c7820edc0b8bdc3f"]]},{"id":"c7820edc0b8bdc3f","type":"switch","z":"765d84d40cd1a292","name":"True/false","property":"record","propertyType":"flow","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":280,"y":180,"wires":[["eb779a21bcaa0d5c"],[]]},{"id":"eb779a21bcaa0d5c","type":"change","z":"765d84d40cd1a292","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"filename","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":160,"wires":[["7b1d59e7447010b3","2c3d8021f6a5db29"]]},{"id":"7b1d59e7447010b3","type":"file","z":"765d84d40cd1a292","name":"","filename":"filename","filenameType":"msg","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"utf8","x":670,"y":160,"wires":[["aa58de0d76a59a1c"]]},{"id":"aa58de0d76a59a1c","type":"debug","z":"765d84d40cd1a292","name":"debug 2506","active":true,"tosidebar":true,"console":true,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":880,"y":120,"wires":[]},{"id":"2c3d8021f6a5db29","type":"debug","z":"765d84d40cd1a292","name":"debug 2507","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":220,"wires":[]},{"id":"2444546f087153a2","type":"ui_group","name":"Default","tab":"8e29298fdb5d2d76","order":1,"disp":true,"width":"12","collapse":false,"className":""},{"id":"8e29298fdb5d2d76","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

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