Drift simulation


My Node-red has a very strange behavior, and I would like to know if it's the case only for me or if someone else encounter this.

I use a slider to simulate a drift on a given value. For the purpose of the exemple, I fixed the value to 80 and reduced the problem to a very simple code.

If the slider is moved to a value inferior of 80, the function increase the entrant value of 5%, then reinject it to the slider. It goes trough it and the process loops until it reach 80.

However, when I move this value at 45 for instance, the correction does not apply. But with 50, it works.
I don't understand why...

Here is the code :

        "id": "45e21b720a5859ad",
        "type": "tab",
        "label": "Flow 3",
        "disabled": false,
        "info": "",
        "env": []
        "id": "0005d4001c69b0ed",
        "type": "function",
        "z": "45e21b720a5859ad",
        "name": "drift compensation",
        "func": "var newDrift = { payload: msg.payload };\n\nif (msg.payload < 80){\n    newDrift.payload *= 1.05;\n}\n\nreturn newDrift;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1350,
        "y": 1100,
        "wires": [
        "id": "cc8a7360ddef5c2f",
        "type": "ui_slider",
        "z": "45e21b720a5859ad",
        "name": "",
        "label": "driftSimulation",
        "tooltip": "",
        "group": "7f6d97ae5791cb88",
        "order": 4,
        "width": 0,
        "height": 0,
        "passthru": true,
        "outs": "end",
        "topic": "topic",
        "topicType": "msg",
        "min": "0",
        "max": "100",
        "step": "5",
        "className": "",
        "x": 1020,
        "y": 1100,
        "wires": [
        "id": "7f6d97ae5791cb88",
        "type": "ui_group",
        "name": "test",
        "tab": "cc75e59fad33e7d0",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
        "id": "cc75e59fad33e7d0",
        "type": "ui_tab",
        "name": "Test",
        "icon": "dashboard",
        "order": 5,
        "disabled": false,
        "hidden": false

Thank you in advance

If you add debug nodes on the input and output of your function you will see what is going on.
Your function moves it by 5% each time.
You have configured the slider to only move in steps of 5, so each time you try to move it, it can only move in steps of 5, so if a 5% change does not move it to the next 5 position it does not move, and does not pass on the message.
If you change the step size to 1 it will work until you get down to very low value, but even then it will eventually stick. You need a different algorithm.

Thank you for the quick and clear response. I'll probably use a PID node in the future, but now I get the problem.

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