# Start and Stop logging by switch

**URL:** https://discourse.nodered.org/t/start-and-stop-logging-by-switch/58516
**Category:** General
**Created:** [17 February 2022 08:40 UTC](https://discourse.nodered.org/t/start-and-stop-logging-by-switch/58516 "2022-02-17T08:40:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![HollaDieWaldfee](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@HollaDieWaldfee](https://discourse.nodered.org/u/HollaDieWaldfee)
#### Post date: [17 February 2022 08:40 UTC](https://discourse.nodered.org/t/start-and-stop-logging-by-switch/58516/1 "2022-02-17T08:40:40Z")

</div>

Hi!!  
I am currently building a datalogger for my gps.  
I would like to have a switch that u press to start logging and also stop logging.  
Every time I stop logging and start over logging I would like a new file to be generated and saved to my folder.  
I am tinking about using node-red-contrib-simple-gate...

I am not really sure how to do it since I am new to NR.  
Thanks in advance!

```auto
[
    {
        "id": "2cd82789b65e21b3",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "18e64fc4f03f85e0",
        "type": "gate",
        "z": "2cd82789b65e21b3",
        "name": "",
        "controlTopic": "control",
        "defaultState": "closed",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 470,
        "y": 660,
        "wires": [
            [
                "46c5e78853f545fa"
            ]
        ]
    },
    {
        "id": "939995ccfbeb6faa",
        "type": "gpsd",
        "z": "2cd82789b65e21b3",
        "name": "",
        "hostname": "localhost",
        "port": "2947",
        "tpv": true,
        "sky": false,
        "info": false,
        "device": false,
        "gst": false,
        "att": false,
        "x": 170,
        "y": 660,
        "wires": [
            [
                "b9f3d901b1d8eeaa"
            ]
        ]
    },
    {
        "id": "3160293e9028d697",
        "type": "function",
        "z": "2cd82789b65e21b3",
        "name": "Filename",
        "func": "var now = new Date();\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\n\n// Generate out file name pattern\nmsg.fname = \"Log_\"+ yyyy +\"-\" + mm +\"-\"+ dd +\"_\"+hh+\"_\"+mm+ \".txt\";\n// Full filename with path for the file node later\nmsg.filename = \"/home/pi/gps_log_self/\"+ msg.fname;\nmsg.filecontent = msg.payload;\nmsg.payload = {\"pattern\":msg.fname};\n\nnode.status({fill:\"blue\",shape:\"ring\",text:msg.fname});\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 800,
        "wires": [
            [
                "1515c3ab7cf5b0ee"
            ]
        ]
    },
    {
        "id": "1515c3ab7cf5b0ee",
        "type": "fs-file-lister",
        "z": "2cd82789b65e21b3",
        "name": "",
        "start": "/home/pi/gps_log",
        "pattern": "",
        "folders": "*",
        "hidden": true,
        "lstype": "files",
        "path": true,
        "single": true,
        "depth": 0,
        "stat": true,
        "showWarnings": false,
        "x": 840,
        "y": 720,
        "wires": [
            [
                "0fa537dca4636800"
            ]
        ]
    },
    {
        "id": "0fa537dca4636800",
        "type": "change",
        "z": "2cd82789b65e21b3",
        "name": "Get file content",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "filecontent",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 980,
        "y": 780,
        "wires": [
            [
                "3fcbbee264fa11c4"
            ]
        ]
    },
    {
        "id": "3fcbbee264fa11c4",
        "type": "file",
        "z": "2cd82789b65e21b3",
        "name": "",
        "filename": "",
        "appendNewline": true,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 1100,
        "y": 720,
        "wires": [
            []
        ]
    },
    {
        "id": "46c5e78853f545fa",
        "type": "function",
        "z": "2cd82789b65e21b3",
        "name": "",
        "func": "\nfunction getRandomString(length) {\n var randomChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';\n var result = '';\n for ( var i = 0; i < length; i++ ) {\n result += randomChars.charAt(Math.floor(Math.random() * randomChars.length));\n }\n return result;\n}\n\n\nmsg.payload = {\n \"name\" : getRandomString(5),\n \"lat\" : msg.payload.lat,\n \"lon\" : msg.payload.lon,\n \"time\" : msg.payload.time.substr(11,8),\n\n \n}\nreturn msg;\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 760,
        "wires": [
            [
                "3160293e9028d697"
            ]
        ]
    },
    {
        "id": "b9f3d901b1d8eeaa",
        "type": "ui_switch",
        "z": "2cd82789b65e21b3",
        "name": "",
        "label": "switch",
        "tooltip": "",
        "group": "6ce64df2b03eece2",
        "order": 6,
        "width": 0,
        "height": 0,
        "passthru": true,
        "decouple": "false",
        "topic": "control",
        "topicType": "str",
        "style": "",
        "onvalue": "open",
        "onvalueType": "str",
        "onicon": "",
        "oncolor": "",
        "offvalue": "close",
        "offvalueType": "str",
        "officon": "",
        "offcolor": "",
        "animate": false,
        "className": "",
        "x": 320,
        "y": 680,
        "wires": [
            [
                "18e64fc4f03f85e0"
            ]
        ]
    },
    {
        "id": "6ce64df2b03eece2",
        "type": "ui_group",
        "name": "GNSS Tracker",
        "tab": "42f2e0a585dcba03",
        "order": 1,
        "disp": false,
        "width": "20",
        "collapse": false,
        "className": ""
    },
    {
        "id": "42f2e0a585dcba03",
        "type": "ui_tab",
        "name": "GNSS Tracker",
        "icon": "fa-map-o",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

```

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [17 February 2022 08:54 UTC](https://discourse.nodered.org/t/start-and-stop-logging-by-switch/58516/2 "2022-02-17T08:54:29Z")

</div>

Move the switch node to it's own wire that feeds into the gate, then all should be well.

---

<div class="post-metadata">

### Author: ![HollaDieWaldfee](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@HollaDieWaldfee](https://discourse.nodered.org/u/HollaDieWaldfee)
#### Post date: [17 February 2022 08:58 UTC](https://discourse.nodered.org/t/start-and-stop-logging-by-switch/58516/3 "2022-02-17T08:58:18Z")

</div>

Gosh so easy .... thanks for helping!

---

<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: [3 March 2022 08:58 UTC](https://discourse.nodered.org/t/start-and-stop-logging-by-switch/58516/4 "2022-03-03T08:58:47Z")

</div>

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