Advice needed: Using a timestamp to send a filename result to an email flow

Folks, need some advice on logic please.

I am working on a flow to email a filename once daily.

A inject with a time recurrence is my trigger: msg.payload
The source is the filename being written to (appending data into the file): msg.filename : string[35]

The intent is for when the timestamp comes through and it is bigger than 0, it will send the filename through to the next node which will kick-off the sendmail function and pickup the file.

Code:
'var A = msg.filename;

if (msg.payload > 0) {
A = msg.payload;

}
return msg;'

If I pass the msg.filename to the email flow without this control, the email sends every time the file is written too (which is every few seconds).

Welcome to the forum @phpurdon

Please attach a debug node, set to show complete message, so that it shows what is going into your function, and paste a screenshot of the debug output here so we can see what are getting. Screenshot just the debug pane please so that it is readable.

Hi @phpurdon
Welcome to this forum. According to what I understood, I'd solve your topic with the following flow:

Be aware, that the switch node sends (in this configuration) a msg with timestamp !== 0 to both outputs ... as I anticipated, you'd like to process each message anyway...

As you can see, there seems to be no need to use a hand written function...

flow.json
[
    {
        "id": "9af0897702ca56cf",
        "type": "tab",
        "label": "Flow 22",
        "disabled": false,
        "info": "",
        "env": [],
        "_mcu": {
            "mcu": false
        }
    },
    {
        "id": "4d911f3e29410894",
        "type": "inject",
        "z": "9af0897702ca56cf",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "_mcu": {
            "mcu": false
        },
        "x": 360,
        "y": 240,
        "wires": [
            [
                "0b1699479b979032"
            ]
        ]
    },
    {
        "id": "044b642be061fd2b",
        "type": "inject",
        "z": "9af0897702ca56cf",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "_mcu": {
            "mcu": false
        },
        "x": 370,
        "y": 280,
        "wires": [
            [
                "0b1699479b979032"
            ]
        ]
    },
    {
        "id": "0b1699479b979032",
        "type": "switch",
        "z": "9af0897702ca56cf",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "neq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "nnull"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "_mcu": {
            "mcu": false
        },
        "x": 510,
        "y": 260,
        "wires": [
            [
                "b18f3abed4f017fd"
            ],
            [
                "a61e10eb774de94b"
            ]
        ]
    },
    {
        "id": "b18f3abed4f017fd",
        "type": "debug",
        "z": "9af0897702ca56cf",
        "name": "send mail",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "\"send mail\"",
        "targetType": "jsonata",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": {
            "mcu": false
        },
        "x": 660,
        "y": 240,
        "wires": []
    },
    {
        "id": "a61e10eb774de94b",
        "type": "debug",
        "z": "9af0897702ca56cf",
        "name": "do whatever",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "msg",
        "_mcu": {
            "mcu": false
        },
        "x": 670,
        "y": 280,
        "wires": []
    }
]

I guess this does what you mean (but more info would help).

Flow stores filenames sent to it, and if timestamp (number in payload >0) is received, the last filename is sent out.

function node:

if (msg.filename) {
    context.set('filename', msg.filename);
}

if (msg.payload > 0) {
    let filename = context.get('filename') || "";
    msg.payload = filename;
    return msg;
}


[{"id":"eba83855da952a33","type":"function","z":"1ca0617cfc39c447","name":"","func":"if (msg.filename) {\n    context.set('filename', msg.filename);\n}\n\nif (msg.payload > 0) {\n    let filename = context.get('filename') || \"\";\n    msg.payload = filename;\n    return msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":1580,"wires":[["05af7840011df897"]]},{"id":"c65ab5a1ff7eea6c","type":"inject","z":"1ca0617cfc39c447","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":1580,"wires":[["eba83855da952a33"]]},{"id":"e68c6e63794480a1","type":"inject","z":"1ca0617cfc39c447","name":"filename","props":[{"p":"payload"},{"p":"filename","v":"/my_log_file.log","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":340,"y":1640,"wires":[["eba83855da952a33"]]},{"id":"05af7840011df897","type":"debug","z":"1ca0617cfc39c447","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":750,"y":1580,"wires":[]}]

Thank you for your replies. I am working through your suggestions and will revert on my results over the next few weeks (traveling in New Zealand ATM).

Hello Cameo69,

I've used your proposal, as it (to me) seems a lot simpler. But still no luck.

I can send an attachment (created by the "Auto Logger") if I specify it manually.

As soon as I try to automate the sending of an attachment from the location generated by the "Auto Logger", this falls apart as the debug on the email node shows the msg.filename does not carry through, it returns "undefined" and the email does not have an attachment.

I did read up about msg.attachment being used by the email node, however, from what I can see this is "translated" to msg.filename.

Can you or any members offer me any advice on where to look next?

[
    {
        "id": "f2c6efab076989ba",
        "type": "comment",
        "z": "516ef0a12415588a",
        "name": "Auto Logger",
        "info": "https://www.youtube.com/watch?v=E2aBIqssQLM",
        "x": 1390,
        "y": 80,
        "wires": []
    },
    {
        "id": "bedf609f70c6d011",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 2290,
        "y": 200,
        "wires": []
    },
    {
        "id": "2c97982bef6b85e5",
        "type": "switch",
        "z": "516ef0a12415588a",
        "name": "",
        "property": "$count(msg.payload)",
        "propertyType": "jsonata",
        "rules": [
            {
                "t": "eq",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 2269,
        "y": 291,
        "wires": [
            [
                "52b1ca19298874cd"
            ],
            [
                "427b5eae31792d01"
            ]
        ]
    },
    {
        "id": "c174a28ada387d00",
        "type": "csv",
        "z": "516ef0a12415588a",
        "name": "",
        "sep": ";",
        "hdrin": "",
        "hdrout": "once",
        "multi": "one",
        "ret": "\\n",
        "temp": "TimeStamp",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 2609,
        "y": 271,
        "wires": [
            [
                "467a7deec3b7c73c"
            ]
        ]
    },
    {
        "id": "467a7deec3b7c73c",
        "type": "file",
        "z": "516ef0a12415588a",
        "name": "",
        "filename": "filename",
        "filenameType": "msg",
        "appendNewline": false,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 2779,
        "y": 271,
        "wires": [
            []
        ]
    },
    {
        "id": "06c5c57b19f66235",
        "type": "csv",
        "z": "516ef0a12415588a",
        "name": "",
        "sep": ";",
        "hdrin": "",
        "hdrout": "none",
        "multi": "one",
        "ret": "\\n",
        "temp": "TimeStamp",
        "skip": "0",
        "strings": true,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 2609,
        "y": 311,
        "wires": [
            [
                "467a7deec3b7c73c"
            ]
        ]
    },
    {
        "id": "52b1ca19298874cd",
        "type": "change",
        "z": "516ef0a12415588a",
        "name": "Get file content",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "filecontent",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 2469,
        "y": 271,
        "wires": [
            [
                "c174a28ada387d00"
            ]
        ]
    },
    {
        "id": "427b5eae31792d01",
        "type": "change",
        "z": "516ef0a12415588a",
        "name": "Get file content",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "filecontent",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 2469,
        "y": 311,
        "wires": [
            [
                "06c5c57b19f66235"
            ]
        ]
    },
    {
        "id": "f67ced8bc8f39445",
        "type": "fs-file-lister",
        "z": "516ef0a12415588a",
        "name": "",
        "start": "/home/pi/datalog/",
        "pattern": "",
        "folders": "*",
        "hidden": true,
        "lstype": "files",
        "path": true,
        "single": true,
        "depth": 0,
        "stat": true,
        "showWarnings": false,
        "x": 2020,
        "y": 200,
        "wires": [
            [
                "bedf609f70c6d011",
                "2c97982bef6b85e5"
            ]
        ]
    },
    {
        "id": "1073ba61d10f141d",
        "type": "function",
        "z": "516ef0a12415588a",
        "name": "data",
        "func": "//if (mesg.topic === \"temp_sp_500_3\") {\nvar now = new Date();\nmsg.payload = {\n    \"TimeStamp\" : flow.get(\"temp_sp_500_3\")\n}\n//}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1550,
        "y": 140,
        "wires": [
            [
                "0ab9950b7eddc2fa",
                "ae784c99b23d6277"
            ]
        ]
    },
    {
        "id": "0ab9950b7eddc2fa",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1710,
        "y": 140,
        "wires": []
    },
    {
        "id": "ae784c99b23d6277",
        "type": "function",
        "z": "516ef0a12415588a",
        "name": "Filename generator",
        "func": "// Get the current time and convert it to text\nvar 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 = \"Peter_\"+ yyyy + mm + dd + \".csv\";\n// Full filename with path for the file node later\nmsg.filename = \"/home/pi/datalog/\"+ msg.fname;\n\n// We save the current payload into a different place on the msg object\nmsg.filecontent = msg.payload;\n\n// We are passing the file name search pattern to fs node to tell us if the file exists or not\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": 1750,
        "y": 200,
        "wires": [
            [
                "f67ced8bc8f39445",
                "b23c17ba7a16c626",
                "eba83855da952a33"
            ]
        ]
    },
    {
        "id": "34f4337b338d4461",
        "type": "inject",
        "z": "516ef0a12415588a",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "5",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1390,
        "y": 140,
        "wires": [
            [
                "1073ba61d10f141d"
            ]
        ]
    },
    {
        "id": "90f8ecd7dacd7f43",
        "type": "inject",
        "z": "516ef0a12415588a",
        "name": "Scheduled Trigger",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "36000",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "Subjectline Datalog",
        "payload": "",
        "payloadType": "date",
        "x": 1420,
        "y": 480,
        "wires": [
            [
                "dddd6ec2987b97d0",
                "eba83855da952a33",
                "b9603c2529d42790"
            ]
        ]
    },
    {
        "id": "b9603c2529d42790",
        "type": "file in",
        "z": "516ef0a12415588a",
        "name": "",
        "filename": "/home/pi/datalog/Peter_20230226.csv",
        "filenameType": "str",
        "format": "",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 1810,
        "y": 780,
        "wires": [
            [
                "a80c5ec5f294e8e5",
                "0ce04207c238e1ba"
            ]
        ]
    },
    {
        "id": "0ce04207c238e1ba",
        "type": "e-mail",
        "z": "516ef0a12415588a",
        "d": true,
        "server": "ahosting.com",
        "port": "465",
        "secure": true,
        "tls": true,
        "name": "mail@gmail.com",
        "dname": "SendMail Node",
        "x": 2300,
        "y": 780,
        "wires": []
    },
    {
        "id": "a80c5ec5f294e8e5",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "filename",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2100,
        "y": 860,
        "wires": []
    },
    {
        "id": "b23c17ba7a16c626",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "debug 3",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "filename",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2020,
        "y": 280,
        "wires": []
    },
    {
        "id": "eba83855da952a33",
        "type": "function",
        "z": "516ef0a12415588a",
        "name": "",
        "func": "if (msg.filename) {\n    context.set('filename', msg.filename);\n}\n\nif (msg.payload > 0) {\n    let filename = context.get('filename') || \"\";\n    msg.payload = filename;\n    return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2080,
        "y": 480,
        "wires": [
            [
                "05af7840011df897",
                "2eeeeec2d102eecf",
                "bfc54ccad06088ba",
                "c39c8092cd03705c"
            ]
        ]
    },
    {
        "id": "c65ab5a1ff7eea6c",
        "type": "inject",
        "z": "516ef0a12415588a",
        "d": true,
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1820,
        "y": 440,
        "wires": [
            [
                "eba83855da952a33"
            ]
        ]
    },
    {
        "id": "e68c6e63794480a1",
        "type": "inject",
        "z": "516ef0a12415588a",
        "d": true,
        "name": "filename",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "filename",
                "v": "/my_log_file.log",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 1820,
        "y": 540,
        "wires": [
            [
                "eba83855da952a33"
            ]
        ]
    },
    {
        "id": "05af7840011df897",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2310,
        "y": 540,
        "wires": []
    },
    {
        "id": "dddd6ec2987b97d0",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "debug 4",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "filename",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1640,
        "y": 400,
        "wires": []
    },
    {
        "id": "2eeeeec2d102eecf",
        "type": "e-mail",
        "z": "516ef0a12415588a",
        "d": true,
        "server": "hosting.com",
        "port": "465",
        "secure": true,
        "tls": false,
        "name": "mail@gmail.com",
        "dname": "SendMail Node",
        "credentials": {},
        "x": 2300,
        "y": 480,
        "wires": []
    },
    {
        "id": "bfc54ccad06088ba",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "msg.filename",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "filename",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2310,
        "y": 600,
        "wires": []
    },
    {
        "id": "c39c8092cd03705c",
        "type": "debug",
        "z": "516ef0a12415588a",
        "name": "msg.topic",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "topic",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 2300,
        "y": 660,
        "wires": []
    },
    {
        "id": "8a9bd5d150854882",
        "type": "comment",
        "z": "516ef0a12415588a",
        "name": "Send Attachment",
        "info": "",
        "x": 1400,
        "y": 360,
        "wires": []
    },
    {
        "id": "ae34cf2f677e0369",
        "type": "comment",
        "z": "516ef0a12415588a",
        "name": "Send Attachment - manual filename",
        "info": "",
        "x": 1460,
        "y": 780,
        "wires": []
    }
]

Let me see if I understand what you are doing.

  1. you have a folder on the pi where a file you need to monitor
  2. you look for a particular file with a name and the current date.
  3. If it shows up, you want to email the file somewhere when it shows up but only send it once a day

Is that about right?
is your data file transferred to the folder all at once or is a program writing to the file?

Hello Zenofmud,

Correct. The file is written to every 30 seconds.

The ideal time to send it would be at the end of each day at 23:59:55 before the new file is created with the new date stamped filename.

Many thanks,

Peter

In that case you should use node-red-contrib-cron-plus to start a flow at the time you want.

The built in inject node can also fire at a simple preset time every day

Hi All.

Right, problem solved.

The "function" picks up the timestamp from "Scheduled Trigger" and if greater than 0 sends the filename location to the "read file" node which sends the actual file to the "SendMail Node"

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