{
    "id": "7bbfbd506fac282d",
    "type": "subflow",
    "name": "Low Pass Filter",
    "info": "Manipulate noise and handle data as stable as you can with low pass filter.",
    "category": "RACKSYNC",
    "in": [
        {
            "x": 160,
            "y": 220,
            "wires": [
                {
                    "id": "ccf930850455e150"
                },
                {
                    "id": "22b25da679a4617a"
                }
            ]
        }
    ],
    "out": [
        {
            "x": 620,
            "y": 140,
            "wires": [
                {
                    "id": "ccf930850455e150",
                    "port": 0
                }
            ]
        },
        {
            "x": 800,
            "y": 320,
            "wires": [
                {
                    "id": "22b25da679a4617a",
                    "port": 0
                }
            ]
        },
        {
            "x": 790,
            "y": 380,
            "wires": [
                {
                    "id": "22b25da679a4617a",
                    "port": 1
                }
            ]
        }
    ],
    "env": [
        {
            "name": "DIFFERENCE",
            "type": "num",
            "value": "100",
            "ui": {
                "icon": "font-awesome/fa-line-chart",
                "label": {
                    "en-US": "Difference"
                },
                "type": "input",
                "opts": {
                    "types": [
                        "num"
                    ]
                }
            }
        },
        {
            "name": "ALPHA",
            "type": "num",
            "value": "0.1",
            "ui": {
                "icon": "font-awesome/fa-remove",
                "label": {
                    "en-US": "Alpha"
                },
                "type": "input",
                "opts": {
                    "types": [
                        "num"
                    ]
                }
            }
        }
    ],
    "meta": {
        "module": "node-red-contrib-low-pass-filter",
        "version": "0.0.5",
        "author": "RACKSYNC CO., LTD. <jack@racksync.com>",
        "desc": "Manipulate noise and handle data as stable as you can with low pass filter.",
        "keywords": "Home Assistant, IoT, Low Pass, Filter, Sensor",
        "license": "MIT"
    },
    "color": "#befc03",
    "inputLabels": [
        "original payload"
    ],
    "outputLabels": [
        "filtered payload",
        "max difference",
        "alpha value"
    ],
    "icon": "font-awesome/fa-random",
    "status": {
        "x": 560,
        "y": 200,
        "wires": [
            {
                "id": "7bbfbd506fac282d",
                "port": 0
            }
        ]
    },

  

"flow":[
    {
        "id": "ccf930850455e150",
        "type": "function",
        "z": "7bbfbd506fac282d",
        "name": "low pass filter",
        "func": "let differenceValue = env.get(\"DIFFERENCE\");\nlet alphaValue = env.get(\"ALPHA\");\n\nconst MAX_DIFFERENCE = parseFloat(differenceValue);\nconst alpha = parseFloat(alphaValue);  // Assuming ALPHA can also be a decimal number.\n\nlet lastProcessedValue = context.get('lastProcessedValue') || msg.payload;  // Fetch the last processed value\n\n// Apply the low-pass filter\nlet filteredValue = lastProcessedValue + alpha * (msg.payload - lastProcessedValue);\n\n// Ensure the change from the last processed value is not more than diff value\nif (filteredValue - lastProcessedValue > MAX_DIFFERENCE) {\n    filteredValue = lastProcessedValue + MAX_DIFFERENCE;\n} else if (lastProcessedValue - filteredValue > MAX_DIFFERENCE) {\n    filteredValue = lastProcessedValue - MAX_DIFFERENCE;\n}\n\n// Store the processed value for next time\ncontext.set('lastProcessedValue', filteredValue);\n\nmsg.payload = filteredValue;\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 340,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "22b25da679a4617a",
        "type": "function",
        "z": "7bbfbd506fac282d",
        "name": "echo default diff & alpha as output 2/3",
        "func": "let differenceValue = env.get(\"DIFFERENCE\");\nlet alphaValue = env.get(\"ALPHA\");\n\nlet msg1 = { payload: differenceValue };\nlet msg2 = { payload: alphaValue };\n\nreturn [msg1, msg2];\n",
        "outputs": 2,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 410,
        "y": 340,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "6cd5f37714a3ca33",
        "type": "subflow:7bbfbd506fac282d",
        "z": "8e07a14948ef83fa",
        "name": "",
        "env": [
            {
                "name": "Difference",
                "value": "100",
                "type": "env"
            },
            {
                "name": "Alpha",
                "value": "0.1",
                "type": "env"
            }
        ],
        "x": 580,
        "y": 560,
        "wires": [
            [
                "a5cfc0e1d7098f8d"
            ],
            [],
            []
        ]
    }
] 
}
