Set-point (SP) for the tank level node red

I am trying to build a program using the Node-RED client to create a Dashboard that asks the user for the Set-point (SP) for the tank level, utilizes the level measurement , and implement a Proportional Controller using the formula u=K.E , where E is the level error, K is the gain, and u is the opening of the input or discharge valve (whichever is applicable), on the scale of 0-10. Also, the dashboard will also show level measurement and valves status. currently, the set point is not working and I need some help

Welcome to the forum @Abdulmoshen

You might like to look at node-red-contrib-pid which should be able to do what you want.

thanks for the swift response. I tried to use the PID function and I did not managed to apply successfully. could you please help me out

Post the flow containing just the pid node and the surrounding logic so I can see what you have done.
To post a flow here, select the nodes then Ctrl-E (or menu Export), copy to clipboard, and paste it here. In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

See this post for more details - How to share code or flow json

[
    {
        "id": "0e1e633305730d14",
        "type": "tab",
        "label": "Flow 2",
        "disabled": true,
        "info": "",
        "env": []
    },
    {
        "id": "70aa43458caaa78e",
        "type": "modbus-read",
        "z": "0e1e633305730d14",
        "name": "Level Measurement ",
        "topic": "level",
        "showStatusActivities": false,
        "logIOActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "InputRegister",
        "adr": "20",
        "quantity": "1",
        "rate": "1",
        "rateUnit": "ms",
        "delayOnStart": false,
        "startDelayTime": "",
        "server": "7a2cb10c45c1ec4b",
        "useIOFile": false,
        "ioFile": "",
        "useIOForPayload": false,
        "emptyMsgOnFail": false,
        "x": 130,
        "y": 140,
        "wires": [
            [
                "6670d94aec713f87",
                "b2a63c0c58423b5e",
                "8423ae1792729db3",
                "65483643eba12b96",
                "97276e3a0182edb5"
            ],
            []
        ]
    },
    {
        "id": "6670d94aec713f87",
        "type": "function",
        "z": "0e1e633305730d14",
        "name": "function 3",
        "func": "msg.payload=msg.payload*30/100;\nmsg.payload=msg.payload.toFixed(0);\nif (msg.payload >= 270)\n    msg.payload = true;\nelse\n    msg.payload = false;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 200,
        "wires": [
            [
                "97276e3a0182edb5",
                "ee87a3a1d0e426f8"
            ]
        ]
    },
    {
        "id": "97276e3a0182edb5",
        "type": "ui_gauge",
        "z": "0e1e633305730d14",
        "name": "",
        "group": "de260a87fe267d0d",
        "order": 0,
        "width": 0,
        "height": 0,
        "gtype": "wave",
        "title": "Tank Level",
        "label": "cm",
        "format": "{{value}}",
        "min": 0,
        "max": "300",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "diff": false,
        "className": "",
        "x": 850,
        "y": 120,
        "wires": []
    },
    {
        "id": "ee87a3a1d0e426f8",
        "type": "ui_led",
        "z": "0e1e633305730d14",
        "order": 2,
        "group": "bbbaa47e36050a98",
        "width": 0,
        "height": 0,
        "label": "Overflow Indication",
        "labelPlacement": "left",
        "labelAlignment": "left",
        "colorForValue": [
            {
                "color": "#ff0000",
                "value": "true",
                "valueType": "bool"
            },
            {
                "color": "#008000",
                "value": "false",
                "valueType": "bool"
            }
        ],
        "allowColorForValueInMessage": false,
        "shape": "circle",
        "showGlow": true,
        "name": "",
        "x": 890,
        "y": 280,
        "wires": []
    },
    {
        "id": "cfce8d702dcbfdf1",
        "type": "modbus-write",
        "z": "0e1e633305730d14",
        "name": "Input Valve",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "HoldingRegister",
        "adr": "30",
        "quantity": "1",
        "server": "7a2cb10c45c1ec4b",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 530,
        "y": 440,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "625b67a1b7bfb0b4",
        "type": "ui_slider",
        "z": "0e1e633305730d14",
        "name": "",
        "label": "Input Valve",
        "tooltip": "",
        "group": "bbbaa47e36050a98",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "1000",
        "step": "1",
        "className": "",
        "x": 150,
        "y": 440,
        "wires": [
            [
                "cfce8d702dcbfdf1"
            ]
        ]
    },
    {
        "id": "8ae735bce7407677",
        "type": "modbus-write",
        "z": "0e1e633305730d14",
        "name": "DischargeValve",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "",
        "dataType": "HoldingRegister",
        "adr": "31",
        "quantity": "1",
        "server": "7a2cb10c45c1ec4b",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 540,
        "y": 520,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "0afad6d5ef9def50",
        "type": "ui_slider",
        "z": "0e1e633305730d14",
        "name": "",
        "label": "Discharge Valve",
        "tooltip": "",
        "group": "bbbaa47e36050a98",
        "order": 1,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "topic",
        "topicType": "msg",
        "min": 0,
        "max": "1000",
        "step": "100",
        "className": "",
        "x": 140,
        "y": 520,
        "wires": [
            [
                "8ae735bce7407677"
            ]
        ]
    },
    {
        "id": "b2a63c0c58423b5e",
        "type": "function",
        "z": "0e1e633305730d14",
        "name": "function 5",
        "func": "var K = 1; // Set the gain value\nvar E = msg.payload[0] - msg.payload.SP\n\n// Calculate the valve opening for 10 iterations\nfor (var i = 0; i < 10; i++) {\n    var u = K * E; // Calculate the valve opening\n\n    // Limit the valve opening between 0 and 10\n    if (u < 0) {\n        u = 0;\n    } else if (u > 10) {\n        u = 10;\n    }\n\n    // Save the valve opening for later use\n    flow.set(\"ValveStatus\", u);\n\n    // Calculate the new error using the updated valve opening\n    msg.payload[0] = msg.payload[0] + u;\n    E = msg.payload.SP - msg.payload[0];\n}\n\nreturn { payload: u };\n\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 60,
        "wires": [
            [
                "21c46fa298eac81f",
                "dcf276172203636f"
            ]
        ]
    },
    {
        "id": "8423ae1792729db3",
        "type": "ui_gauge",
        "z": "0e1e633305730d14",
        "name": "",
        "group": "de260a87fe267d0d",
        "order": 10,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "Level Measurement",
        "label": "units",
        "format": "{{value}}",
        "min": 0,
        "max": 10,
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "diff": false,
        "className": "",
        "x": 390,
        "y": 360,
        "wires": []
    },
    {
        "id": "cd3e76d6ec05e4d2",
        "type": "ui_numeric",
        "z": "0e1e633305730d14",
        "name": "",
        "label": "Set-Point (SP)",
        "tooltip": "",
        "group": "bbbaa47e36050a98",
        "order": 8,
        "width": 0,
        "height": 0,
        "wrap": false,
        "passthru": true,
        "topic": "topic",
        "topicType": "msg",
        "format": "{{value}}",
        "min": 0,
        "max": "1000",
        "step": 1,
        "className": "",
        "x": 140,
        "y": 360,
        "wires": [
            [
                "b2a63c0c58423b5e"
            ]
        ]
    },
    {
        "id": "21c46fa298eac81f",
        "type": "ui_text",
        "z": "0e1e633305730d14",
        "group": "bbbaa47e36050a98",
        "order": 9,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Valve Status",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 610,
        "y": 360,
        "wires": []
    },
    {
        "id": "ccc831f8fd35b623",
        "type": "inject",
        "z": "0e1e633305730d14",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 110,
        "y": 260,
        "wires": [
            [
                "cd3e76d6ec05e4d2"
            ]
        ]
    },
    {
        "id": "65483643eba12b96",
        "type": "function",
        "z": "0e1e633305730d14",
        "name": "arrayToVal",
        "func": "var level = {topic: 'Level',payload : msg.payload[0]};\nreturn level;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 430,
        "y": 260,
        "wires": [
            [
                "b2a63c0c58423b5e"
            ]
        ]
    },
    {
        "id": "dcf276172203636f",
        "type": "debug",
        "z": "0e1e633305730d14",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 800,
        "y": 60,
        "wires": []
    },
    {
        "id": "300735acaf68ee2b",
        "type": "PID",
        "z": "0e1e633305730d14",
        "name": "",
        "setpoint": 21,
        "pb": 1,
        "ti": 9999,
        "td": 0,
        "integral_default": 0.5,
        "smooth_factor": 3,
        "max_interval": 600,
        "enable": 1,
        "disabled_op": 0,
        "x": 430,
        "y": 200,
        "wires": [
            []
        ]
    },
    {
        "id": "7a2cb10c45c1ec4b",
        "type": "modbus-client",
        "name": "Fill Valve ",
        "clienttype": "tcp",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "166.87.236.106",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttyUSB",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "9600",
        "serialDatabits": "8",
        "serialStopbits": "1",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": true,
        "showWarnings": true,
        "showLogs": true
    },
    {
        "id": "de260a87fe267d0d",
        "type": "ui_group",
        "name": "Level Measurement",
        "tab": "7328d5bda7059b23",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "bbbaa47e36050a98",
        "type": "ui_group",
        "name": "Set-point (SP) of the tank level",
        "tab": "7328d5bda7059b23",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "7328d5bda7059b23",
        "type": "ui_tab",
        "name": "Set-point (SP) of the tank level",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

You have not connected the PID node to anything. You said you had tried to make it work and failed.
Did you see the tuning guide and example flow linked from the help text of the node?

Also, if posting again then post just the nodes around the PID node, not the whole thing with display nodes and so on.

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