Hey. need help in function node to control allow msgs to pass via time

i have being trying many time to set this but get Nothing .
i wanna make file csv rule has time in hour and min and code .
if the code are correct well go to check the time in file is it in range of current time or +15min like 00;00 to 00;15. if it in the range it will allow to send in msg.payload the setpoint value [sp] .this is my flow .

[
    {
        "id": "dd36165422cdf4c9",
        "type": "group",
        "z": "96135b2b.aa4598",
        "style": {
            "stroke": "#999999",
            "stroke-opacity": "1",
            "fill": "none",
            "fill-opacity": "1",
            "label": true,
            "label-position": "nw",
            "color": "#a4a4a4"
        },
        "nodes": [
            "cfdc4009d21a085f",
            "0822496d5b1bc135",
            "597de39234bb9a6d",
            "86c3243e7c93eae4",
            "d5f91815e4edf560",
            "93e2dfba93f99b29",
            "9dfa7cd203bba7a1",
            "d615c723e8b8aa87",
            "08900fc6aebab3a8"
        ],
        "x": 34,
        "y": 259,
        "w": 1292,
        "h": 162
    },
    {
        "id": "cfdc4009d21a085f",
        "type": "csv",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "Data en csv",
        "sep": ";",
        "hdrin": true,
        "hdrout": "none",
        "multi": "one",
        "ret": "\\r\\n",
        "temp": "heure,setpoint,code",
        "skip": "1",
        "strings": true,
        "include_empty_strings": true,
        "include_null_values": "",
        "x": 630,
        "y": 340,
        "wires": [
            [
                "597de39234bb9a6d"
            ]
        ]
    },
    {
        "id": "0822496d5b1bc135",
        "type": "function",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "write values",
        "func": "var sp = msg.payload.sp;\nvar heure = msg.payload.time;\nconst code = msg.payload.code ;\nvar now = new Date();\n\n// now.setTime(now.getTime() - 1000*60*60*24*i);\nlet yyyy = now.getFullYear();\nlet mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nlet dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nlet hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nlet mmm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nlet ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nlet ms = now.getMilliseconds() < 10 ? \"00\" + now.getMilliseconds() : (now.getMilliseconds() < 100 ? \"0\" + now.getMilliseconds() : now.getMilliseconds());\nlet time_jour = yyyy + \"/\" + mm + \"/\" + dd;\nlet time_heure = hh + \":\" + mmm  ;\nmsg.payload = {\n     \"heure\": heure, \"setpoint\": sp, \"code\": code };\nreturn msg;\n",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 430,
        "y": 340,
        "wires": [
            [
                "cfdc4009d21a085f"
            ]
        ]
    },
    {
        "id": "597de39234bb9a6d",
        "type": "file",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "",
        "filename": "C:\\Users\\Actutec PC\\Desktop\\FILES\\pid.control.test.csv",
        "filenameType": "str",
        "appendNewline": false,
        "createDir": true,
        "overwriteFile": "false",
        "encoding": "none",
        "x": 930,
        "y": 340,
        "wires": [
            [
                "d5f91815e4edf560"
            ]
        ]
    },
    {
        "id": "86c3243e7c93eae4",
        "type": "inject",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": " sp 5 . code good .at 00;00",
        "props": [
            {
                "p": "payload.sp",
                "v": "5",
                "vt": "num"
            },
            {
                "p": "payload.code",
                "v": "2302",
                "vt": "str"
            },
            {
                "p": "payload.time",
                "v": "00:00",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 190,
        "y": 340,
        "wires": [
            [
                "0822496d5b1bc135"
            ]
        ]
    },
    {
        "id": "d5f91815e4edf560",
        "type": "debug",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "write",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1230,
        "y": 340,
        "wires": []
    },
    {
        "id": "93e2dfba93f99b29",
        "type": "file",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "",
        "filename": "C:\\Users\\Actutec PC\\Desktop\\FILES\\pid.control.test.csv",
        "filenameType": "str",
        "appendNewline": false,
        "createDir": true,
        "overwriteFile": "delete",
        "encoding": "none",
        "x": 850,
        "y": 380,
        "wires": [
            []
        ]
    },
    {
        "id": "9dfa7cd203bba7a1",
        "type": "inject",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "delet file",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 500,
        "y": 380,
        "wires": [
            [
                "93e2dfba93f99b29"
            ]
        ]
    },
    {
        "id": "d615c723e8b8aa87",
        "type": "inject",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "test if code not correct",
        "props": [
            {
                "p": "payload.sp",
                "v": "6",
                "vt": "num"
            },
            {
                "p": "payload.code",
                "v": "8999",
                "vt": "str"
            },
            {
                "p": "payload.time",
                "v": "23:50",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 180,
        "y": 300,
        "wires": [
            [
                "0822496d5b1bc135"
            ]
        ]
    },
    {
        "id": "08900fc6aebab3a8",
        "type": "inject",
        "z": "96135b2b.aa4598",
        "g": "dd36165422cdf4c9",
        "name": "set sp=4 and time 01:00",
        "props": [
            {
                "p": "payload.sp",
                "v": "4",
                "vt": "num"
            },
            {
                "p": "payload.code",
                "v": "2302",
                "vt": "str"
            },
            {
                "p": "payload.time",
                "v": "01:00",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 200,
        "y": 380,
        "wires": [
            [
                "0822496d5b1bc135"
            ]
        ]
    },
    {
        "id": "bf47cf9025c51c6a",
        "type": "group",
        "z": "96135b2b.aa4598",
        "style": {
            "stroke": "#999999",
            "stroke-opacity": "1",
            "fill": "none",
            "fill-opacity": "1",
            "label": true,
            "label-position": "nw",
            "color": "#a4a4a4"
        },
        "nodes": [
            "f51f135e952fbcf4",
            "5d2ac4ede3774c36",
            "ec75452da754fdcc",
            "8bcb488806ec13a1",
            "69d17dcfcfb15eb8",
            "17ec968dece06acf",
            "11f497f7116785e3",
            "5e6a9e169bfccaee",
            "144af68aed4cd51e",
            "1741ec2f8bc80d28",
            "d689f1e0baaedfec",
            "3ad781df582d2216",
            "c8b37f8b7b678e4c",
            "85dfd4b9af45f9aa"
        ],
        "x": 34,
        "y": 979,
        "w": 1472,
        "h": 302
    },
    {
        "id": "f51f135e952fbcf4",
        "type": "file in",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "",
        "filename": "C:\\Users\\Actutec PC\\Desktop\\FILES\\pid.control.test.csv",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 450,
        "y": 1080,
        "wires": [
            [
                "ec75452da754fdcc"
            ]
        ]
    },
    {
        "id": "5d2ac4ede3774c36",
        "type": "inject",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 1080,
        "wires": [
            [
                "f51f135e952fbcf4"
            ]
        ]
    },
    {
        "id": "ec75452da754fdcc",
        "type": "csv",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "Data en csv",
        "sep": ";",
        "hdrin": false,
        "hdrout": "none",
        "multi": "one",
        "ret": "\\r\\n",
        "temp": "heure,setpoint",
        "skip": "0",
        "strings": false,
        "include_empty_strings": false,
        "include_null_values": false,
        "x": 770,
        "y": 1080,
        "wires": [
            [
                "8bcb488806ec13a1",
                "1741ec2f8bc80d28",
                "3ad781df582d2216"
            ]
        ]
    },
    {
        "id": "8bcb488806ec13a1",
        "type": "function",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "function 0",
        "func": "msg.payload = msg.payload[1];\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 940,
        "y": 1080,
        "wires": [
            [
                "69d17dcfcfb15eb8",
                "5e6a9e169bfccaee",
                "17ec968dece06acf",
                "85dfd4b9af45f9aa"
            ]
        ]
    },
    {
        "id": "69d17dcfcfb15eb8",
        "type": "function",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "function 136",
        "func": "msg.payload = parseFloat(msg.payload.setpoint);\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1110,
        "y": 1200,
        "wires": [
            [
                "11f497f7116785e3"
            ]
        ]
    },
    {
        "id": "17ec968dece06acf",
        "type": "debug",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "debug 55",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload.heure",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1340,
        "y": 1060,
        "wires": []
    },
    {
        "id": "11f497f7116785e3",
        "type": "debug",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "debug 2447",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1290,
        "y": 1220,
        "wires": []
    },
    {
        "id": "5e6a9e169bfccaee",
        "type": "debug",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "debug 24",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1260,
        "y": 1020,
        "wires": []
    },
    {
        "id": "144af68aed4cd51e",
        "type": "debug",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "debug 2450",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 1390,
        "y": 1160,
        "wires": []
    },
    {
        "id": "1741ec2f8bc80d28",
        "type": "debug",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "debug 2451",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 890,
        "y": 1140,
        "wires": []
    },
    {
        "id": "d689f1e0baaedfec",
        "type": "comment",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "read file flow here",
        "info": "",
        "x": 160,
        "y": 1020,
        "wires": []
    },
    {
        "id": "3ad781df582d2216",
        "type": "function",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "function rule to send setpoint ",
        "func": "var sp = msg.payload.sp;\nvar heure = msg.payload.time;\nconst code = msg.payload.code;\nvar now = new Date();\n\n// Assuming you have 'i' declared and initialized here\nlet i = 0;\n\n// Set the current date and time\nnow.setTime(now.getTime() - 1000 * 60 * 60 * 24 * i);\nlet yyyy = now.getFullYear();\nlet mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1);\nlet dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nlet hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nlet mmm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nlet ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nlet ms = now.getMilliseconds() < 10\n    ? \"00\" + now.getMilliseconds()\n    : now.getMilliseconds() < 100\n    ? \"0\" + now.getMilliseconds()\n    : now.getMilliseconds();\nlet time_jour = yyyy + \"/\" + mm + \"/\" + dd;\nlet time_heure = hh + \":\" + mmm;\nlet range = 15;\n\nif (code === \"2302\") {\n  if (heure === time_heure) {\n    msg.payload = sp;\n  } else {\n    msg.payload = \"not in time setting\";\n  }\n} else {\n  msg.payload = \"code not correct, please check the code in the file csv\";\n}\n\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 640,
        "y": 1200,
        "wires": [
            [
                "c8b37f8b7b678e4c"
            ]
        ]
    },
    {
        "id": "c8b37f8b7b678e4c",
        "type": "debug",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "debug Rule data",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 740,
        "y": 1240,
        "wires": []
    },
    {
        "id": "85dfd4b9af45f9aa",
        "type": "function",
        "z": "96135b2b.aa4598",
        "g": "bf47cf9025c51c6a",
        "name": "function 143",
        "func": "msg.payload = parseFloat(msg.payload.heure);\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1170,
        "y": 1120,
        "wires": [
            [
                "144af68aed4cd51e"
            ]
        ]
    }
]

and thanks for help

Please post the function node code separately so that people can more easily read and comment. Contributors often don't want to or don't have the time to load people's flows. Thanks.

1 Like

thanks for replay i fix my flow by this

// This assumes msg.payload contains the array from the CSV
const schedule = msg.payload;

// Function to convert time string to minutes for comparison
function timeToMinutes(time) {
  const [hours, minutes] = time.split(':').map(Number);
  return hours * 60 + minutes;
}

// Function to check the schedule and send the setpoint
function checkScheduleAndSendSetpoint(schedule) {
  const now = new Date();
  const currentTimeInMinutes = now.getHours() * 60 + now.getMinutes();

  for (const item of schedule) {
    const startTime = timeToMinutes(item.time_start);
    const endTime = timeToMinutes(item.time_end);

    // Check if current time is within the start and end time
    if (currentTimeInMinutes >= startTime && currentTimeInMinutes < endTime && item.fonction_stab === "on") {
      // Send the setpoint value
      return { payload: item.setpoint };
    }
  }

  // If no schedule matches the current time, or fonction_stab is not "on", do not send anything
  return null;
}

// Run the function to check the schedule
const result = checkScheduleAndSendSetpoint(schedule);

// Check if result is not null, then send the payload
if (result !== null) {
  node.send(result);
} else {
  // Optionally handle the case where there is no matching schedule
  node.send({ payload: "No matching schedule or fonction_stab is off" });
}

You don't need the node.sends, simply return the result or object from the function node.

Also, you don't really need to mess about with hours and minutes calculations.

Since JS Date objects are actually millisecond representations, you can simply add/subtract them then divide by 60,000 to get minutes elapsed.

1 Like

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