Periodical reading of Modbus data from devices based on DB stored IP, and ASDU

Hi all.

So I am really tired, or stupid. Please, help, if you have any idea.
Here is the code of flow, that read DB with IP and ID(ASDU) and after that use this data in cycle put to Modbus to read the data about temp, voltage and current from modbus devices.

So the increment in cycle does not running.

[
    {
        "id": "2e031c0d462c62b7",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "358df04e372457f9",
        "type": "debug",
        "z": "2e031c0d462c62b7",
        "name": "debug 201",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 740,
        "y": 360,
        "wires": []
    },
    {
        "id": "8d2b804eec4f5ac3",
        "type": "function",
        "z": "2e031c0d462c62b7",
        "name": "file_read_name",
        "func": "var msg1, msg2, msg3;\nvar arr = [];\nvar arr2 = [];\nvar j = 1, msg_sum = [];\nvar mpl = msg.payload.length;\n\nfor (var i = 0; i < mpl; i++) {\n    //arr.push({ [msg.payload[i].location]: msg.payload[i].id });\n    arr2.push(msg.payload[i].id + \";\" +msg.payload[i].location + \";\" + msg.payload[i].ris_ip + \";\" + msg.payload[i].ris_id);  \n}\n            \nmsg2 = { payload: arr2};\nmsg3 = { payload: mpl };\n\n    return [msg1, msg2, msg3];\n",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 160,
        "y": 140,
        "wires": [
            [],
            [
                "f2d5d1a5d34011ec",
                "9ff5c4420360c041"
            ],
            [
                "62e130a6f418f15f",
                "e4c7889d5cdf3499"
            ]
        ]
    },
    {
        "id": "62e130a6f418f15f",
        "type": "debug",
        "z": "2e031c0d462c62b7",
        "name": "debug 202",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 460,
        "y": 130,
        "wires": []
    },
    {
        "id": "be63f7ba6361208a",
        "type": "inject",
        "z": "2e031c0d462c62b7",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "3",
        "crontab": "",
        "once": true,
        "onceDelay": "2",
        "topic": "",
        "payload": "1",
        "payloadType": "num",
        "x": 130,
        "y": 190,
        "wires": [
            [
                "362add31d809355f"
            ]
        ]
    },
    {
        "id": "e301e35a75e47053",
        "type": "debug",
        "z": "2e031c0d462c62b7",
        "name": "debug 203",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 500,
        "y": 490,
        "wires": []
    },
    {
        "id": "95fbc3f6c0414b9e",
        "type": "debug",
        "z": "2e031c0d462c62b7",
        "name": "debug 204",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 720,
        "y": 420,
        "wires": []
    },
    {
        "id": "79f70ee7fd767751",
        "type": "inject",
        "z": "2e031c0d462c62b7",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "600",
        "crontab": "",
        "once": true,
        "onceDelay": "1",
        "topic": "SELECT * FROM ups_data ORDER BY id",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 20,
        "wires": [
            [
                "d87824dfc8fc26fb"
            ]
        ]
    },
    {
        "id": "f2d5d1a5d34011ec",
        "type": "debug",
        "z": "2e031c0d462c62b7",
        "name": "debug 205",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 460,
        "y": 70,
        "wires": []
    },
    {
        "id": "4700f489a8ea3b5b",
        "type": "debug",
        "z": "2e031c0d462c62b7",
        "name": "debug 206",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 710,
        "y": 500,
        "wires": []
    },
    {
        "id": "46d2ac062aede649",
        "type": "modbus-flex-connector",
        "z": "2e031c0d462c62b7",
        "name": "",
        "maxReconnectsPerMinute": 4,
        "emptyQueue": false,
        "showStatusActivities": true,
        "showErrors": false,
        "server": "8de5dabcee0a5816",
        "x": 700,
        "y": 240,
        "wires": [
            [
                "9073bb7bff87b854"
            ]
        ]
    },
    {
        "id": "9073bb7bff87b854",
        "type": "debug",
        "z": "2e031c0d462c62b7",
        "name": "debug 207",
        "active": true,
        "tosidebar": true,
        "console": true,
        "tostatus": true,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "payload",
        "statusType": "auto",
        "x": 740,
        "y": 300,
        "wires": []
    },
    {
        "id": "d87824dfc8fc26fb",
        "type": "sqlite",
        "z": "2e031c0d462c62b7",
        "mydb": "77663dac4f64af8e",
        "sqlquery": "msg.topic",
        "sql": "",
        "name": "sqlitedb",
        "x": 140,
        "y": 80,
        "wires": [
            [
                "8d2b804eec4f5ac3"
            ]
        ]
    },
    {
        "id": "8bcf7aa79f19d4a4",
        "type": "function",
        "z": "2e031c0d462c62b7",
        "name": "function 21",
        "func": "var msg1, msg2, msg3;\nvar sada_loc, sada_ip, sada_id, sada_rid;\nvar arr = [];\nvar arr2 = [], obsah = [] ;\nvar s = 0;\nobsah = flow.get(\"whole_string\");\nvar index = flow.get(\"records_sum\");\nvar incr = flow.get(\"increm\");\nvar select = flow.get(\"trigger_select\");\n\nif (select == 1) {\n  \n    var sada_def = obsah[incr].split(\";\");\n    sada_rid = sada_def[0];\n    sada_loc = sada_def[1];\n    sada_ip = sada_def[2];\n    sada_id = sada_def[3];  \n    //msg3 = { payload: incr };    \n\nmsg1 = {\n    payload: {\n        'connectorType': 'TCP',\n        'tcpHost': sada_ip,\n        'tcpPort': '502',\n        'unitId': sada_id\n    }\n}  \n    if (incr >= 0) {\n        s = incr;\n    }\n    if (s == index) {\n        s = 0;;\n    } else {\n        s = s + 1;\n    }   \n      msg2 = {payload:  s}; \n    return [msg1, msg2, msg3];\n}",
        "outputs": 3,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 450,
        "y": 320,
        "wires": [
            [
                "46d2ac062aede649"
            ],
            [
                "95fbc3f6c0414b9e"
            ],
            [
                "4700f489a8ea3b5b"
            ]
        ]
    },
    {
        "id": "9ff5c4420360c041",
        "type": "change",
        "z": "2e031c0d462c62b7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "whole_string",
                "pt": "flow",
                "to": "payload",
                "tot": "msg",
                "dc": true
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 190,
        "wires": [
            [
                "8bcf7aa79f19d4a4",
                "358df04e372457f9"
            ]
        ]
    },
    {
        "id": "e4c7889d5cdf3499",
        "type": "change",
        "z": "2e031c0d462c62b7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "records_sum",
                "pt": "flow",
                "to": "payload",
                "tot": "msg",
                "dc": true
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 430,
        "y": 240,
        "wires": [
            [
                "8bcf7aa79f19d4a4"
            ]
        ]
    },
    {
        "id": "e77f3f4d0bf9862f",
        "type": "change",
        "z": "2e031c0d462c62b7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "trigger_select",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 190,
        "y": 320,
        "wires": [
            [
                "8bcf7aa79f19d4a4"
            ]
        ]
    },
    {
        "id": "362add31d809355f",
        "type": "trigger",
        "z": "2e031c0d462c62b7",
        "name": "",
        "op1": "1",
        "op2": "0",
        "op1type": "num",
        "op2type": "num",
        "duration": "500",
        "extend": false,
        "overrideDelay": false,
        "units": "ms",
        "reset": "",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 180,
        "y": 250,
        "wires": [
            [
                "e77f3f4d0bf9862f"
            ]
        ]
    },
    {
        "id": "e9295fec37bfdce6",
        "type": "change",
        "z": "2e031c0d462c62b7",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "increm",
                "pt": "flow",
                "to": "payload",
                "tot": "msg",
                "dc": true
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 290,
        "y": 430,
        "wires": [
            [
                "36937936b8946297"
            ]
        ]
    },
    {
        "id": "36937936b8946297",
        "type": "delay",
        "z": "2e031c0d462c62b7",
        "name": "",
        "pauseType": "delay",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 470,
        "y": 410,
        "wires": [
            [
                "8bcf7aa79f19d4a4",
                "e301e35a75e47053"
            ]
        ]
    },
    {
        "id": "8de5dabcee0a5816",
        "type": "modbus-client",
        "name": "",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "192.168.1.240",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": "",
        "commandDelay": 1,
        "clientTimeout": 1000,
        "reconnectOnTimeout": true,
        "reconnectTimeout": 500,
        "parallelUnitIdsAllowed": true,
        "showWarnings": true,
        "showLogs": true
    },
    {
        "id": "77663dac4f64af8e",
        "type": "sqlitedb",
        "db": "/home/pi/SmartUPS/db_data_ups.sqlite",
        "mode": "RW"
    }
]

Many thanks to your ideas.

The best plan is to add debug nodes and find exactly which node is not doing what you expect. Then if you cannot see why that node is not working for you can ask for more help.

I understand it, but, debug nodes I have everytime added. So this is little bit special. Because this flow sometimes working, sometimes no. If I have changed small thing and next time I put it back, is not running.
I have deleted also file flows.json in .nodered directory(RPi), but without result.
I thought that I have something wrong, but …. I am not sure. May be, I will reinstall node-red on RPi. So before it, I need to be sure about my code. May be, I have anything wrong in code, what goes Node Red wrong. May be not.

There is no need to re-install, it will not make any difference.

You said that sometimes it is not running - exactly what in which part of the flow is not running?

F.e. at this moment this :

var msg1, msg2, msg3, msg4;
var sada_def, sada_loc, sada_ip, sada_id, sada_rid;
var s = 0;
var d_length = msg.payload.length;
var data = msg.payload;

var incr = parseInt(flow.get("increment"));
var select = parseInt(flow.get("trigger_select"));

if (select == 1) {
    
msg1 = { payload: {// modbus connector data
        'connectorType': 'TCP',
    'tcpHost': data[incr].ris_ip,
        'tcpPort': '502',
    'unitId': data[incr].ris_id
    }
}  
    if (incr >= 0) { // if increment value is more than 0, s value goes with incr.
        s = incr;
    }
    
    if (s == d_length) { // if s is equal data length, than set to 0
        s = 0;
    } else { // if not, increment it and send to msg2 out
        s = s + 1;
    }   
    msg2 = { payload: s }; // value for increment
   
}
msg4 = { payload: data}; // for check
return [msg1, msg2, msg3, msg4];

This is in function node function 20.

So picture of nodes are here :

What do you mean by not running?
When it fails, is it being sent a message? If so then what is in the message and what output is it outputting and what should it be outputting?