Python3-function calulation fail

Hi,

I'm seeking help with this code I have.

Point 1:
When I provide a number input via ui.text.input to the Python 3 function, it only remembers it until I provide input from another ui.text.input.

Point 2:
When my MeasuredPPM is above PPMSetpoint, it should gradually open, but this is not happening.

JSON Code:

[
    {
        "id": "0a4ed0543769fad8",
        "type": "function",
        "z": "0c6431dbee54e2e2",
        "name": "PPMSetpoint",
        "func": "if (typeof msg.payload !== 'object') {\n    // Hvis msg.payload ikke er et objekt, opret et objekt med PPMSetpoint\n    msg.payload = { PPMSetpoint: Number(msg.payload) };\n} else {\n    // Hvis msg.payload allerede er et objekt, opdater PPMSetpoint, hvis det er defineret\n    if (msg.payload.PPMSetpoint !== undefined && msg.payload.PPMSetpoint !== null) {\n        msg.payload.PPMSetpoint = Number(msg.payload.PPMSetpoint);\n    }\n}\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1230,
        "y": 620,
        "wires": [
            [
                "474db4a060feea65"
            ]
        ]
    },
    {
        "id": "13e50a507f788fcd",
        "type": "function",
        "z": "0c6431dbee54e2e2",
        "name": "MeasuredPPM",
        "func": "// Håndtering af målt PPM\nif (typeof msg.payload !== 'object') {\n    // Hvis msg.payload ikke er et objekt, opret et objekt med MeasuredPPM\n    msg.payload = { MeasuredPPM: Number(msg.payload) };\n} else {\n    // Hvis msg.payload allerede er et objekt, opdater MeasuredPPM, hvis det er defineret\n    if (msg.payload.MeasuredPPM !== undefined && msg.payload.MeasuredPPM !== null) {\n        msg.payload.MeasuredPPM = Number(msg.payload.MeasuredPPM);\n    }\n}\n\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1220,
        "y": 660,
        "wires": [
            [
                "474db4a060feea65"
            ]
        ]
    },
    {
        "id": "51e2ca50e7a657cf",
        "type": "modbus-write",
        "z": "0c6431dbee54e2e2",
        "name": "Setpoint for flow 0-10000",
        "showStatusActivities": false,
        "showErrors": false,
        "showWarnings": true,
        "unitid": "1",
        "dataType": "HoldingRegister",
        "adr": "0",
        "quantity": "1",
        "server": "1c23fca020e29bfc",
        "emptyMsgOnFail": false,
        "keepMsgProperties": false,
        "delayOnStart": false,
        "startDelayTime": "",
        "x": 1670,
        "y": 640,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "474db4a060feea65",
        "type": "python3-function",
        "z": "0c6431dbee54e2e2",
        "name": "",
        "func": "# If msg.payload is not an object, assume it's a numerical value\nif isinstance(msg, (int, float)):\n    setpoint_ppm = float(msg)\nelse:\n    # If msg.payload is an object, update PPMSetpoint if defined\n    payload = msg.get(\"payload\", {})\n    setpoint_ppm = float(payload.get(\"PPMSetpoint\", 0))\n\n# Save incoming values from the UI\nmeasured_ppm = float(payload.get(\"MeasuredPPM\", 0))\n\n# Calculate the difference between the setpoint and the measured PPM\ndifference = measured_ppm - setpoint_ppm\n\n# Assume a simple proportional control with a proportional band of ±100 around the setpoint\np_band = 700\nscale_factor = 30  # Adjust as needed\nspj_opening = 0  # Set default value to 0\n\n# Logic to adjust for the damper opening based on the difference\nif difference > p_band:\n    # If PPM is above setpoint and outside the proportional band, calculate opening\n    spj_opening = max(0, min(10000, (difference - p_band) * scale_factor))\n\n# Create a new dictionary for output\noutput_msg = {\n    \"payload\": spj_opening\n}\n\nreturn output_msg\n",
        "outputs": 1,
        "x": 1430,
        "y": 640,
        "wires": [
            [
                "51e2ca50e7a657cf",
                "27d33c68489a6d19",
                "8079a78fac3fb693"
            ]
        ]
    },
    {
        "id": "1c23fca020e29bfc",
        "type": "modbus-client",
        "name": "",
        "clienttype": "serial",
        "bufferCommands": true,
        "stateLogEnabled": false,
        "queueLogEnabled": false,
        "failureLogEnabled": true,
        "tcpHost": "127.0.0.1",
        "tcpPort": "502",
        "tcpType": "DEFAULT",
        "serialPort": "/dev/ttySC0",
        "serialType": "RTU-BUFFERD",
        "serialBaudrate": "38400",
        "serialDatabits": "8",
        "serialStopbits": "2",
        "serialParity": "none",
        "serialConnectionDelay": "100",
        "serialAsciiResponseStartDelimiter": "0x3A",
        "unit_id": "1",
        "commandDelay": "1",
        "clientTimeout": "1000",
        "reconnectOnTimeout": true,
        "reconnectTimeout": "2000",
        "parallelUnitIdsAllowed": true,
        "showErrors": true,
        "showWarnings": true,
        "showLogs": true
    }
]
  

Hi. There is no need to post links to code. Post them directly in the forum thread

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 
```

You can edit and correct your post by clicking the pencil :pencil2: icon.

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

Okay, thanks. I didn't know about that.

Regarding no. 2: Your difference is measured - setpoint, but to get the output signal, you make a test that the difference signal must be greater than 700 before the output signal begins. Is that what you want?

The spj_opening should open gradually if the measure is above the set point.

If I remake the code and make the set point to 700 in the code it working… so I forgetting something when I have to input🥴