Comparing flow variables

Hey All,
I have a motion sensor that turns on lights, in addition to that I have a fan that I want to turn on and off based on the temperature. I set the "temperature threshold" and write the value to a text file. I also write the currentTemp to a text file. I then compare the values to determine if the fan turns on or off.

To do this I'm setting the threshold and currentTemp values to flow variables and using function nodes to do the comparison. For some reason both functions are triggering simultaniously and not working properly. How can I write these functions so the work correctly?

Thanks!
Rich

[
    {
        "id": "9e5e69a5e191851d",
        "type": "switch",
        "z": "787c361b51b9d832",
        "name": "ON",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "ON",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 622,
        "y": 435,
        "wires": [
            [
                "0271de3c43e83b63",
                "15df05b0c1c9bc03",
                "47799609d55e4892",
                "3c181d2edef0aa42"
            ]
        ]
    },
    {
        "id": "8fb38073d051089f",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "Set currentTemp FLOW Value",
        "rules": [
            {
                "t": "set",
                "p": "currentTemp",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 370,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "15df05b0c1c9bc03",
        "type": "file in",
        "z": "787c361b51b9d832",
        "name": "READ tempNow.txt",
        "filename": "/home/pi/scripts/temperature/currentTemps/insideFromApt.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 130,
        "y": 240,
        "wires": [
            [
                "8fb38073d051089f"
            ]
        ]
    },
    {
        "id": "47799609d55e4892",
        "type": "file in",
        "z": "787c361b51b9d832",
        "name": "READ fanThreshold.txt",
        "filename": "/home/pi/scripts/temperature/currentTemps/fanThreshold.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 140,
        "y": 180,
        "wires": [
            [
                "6283761a5021dc1f"
            ]
        ]
    },
    {
        "id": "6283761a5021dc1f",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "Set threshold FLOW Value",
        "rules": [
            {
                "t": "set",
                "p": "threshold",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 180,
        "wires": [
            [
                "a5e0f4f975982884"
            ]
        ]
    },
    {
        "id": "3c181d2edef0aa42",
        "type": "delay",
        "z": "787c361b51b9d832",
        "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": 600,
        "y": 220,
        "wires": [
            [
                "fe6085756568d779",
                "0ae75004c433dcc1"
            ]
        ]
    },
    {
        "id": "fe6085756568d779",
        "type": "function",
        "z": "787c361b51b9d832",
        "name": "is currentTemp greater then ON",
        "func": "if (flow.currentTemp >= flow.threshold){\nmsg.payload = 'ON';}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 710,
        "y": 160,
        "wires": [
            [
                "ca07b1e317370f3a"
            ]
        ]
    },
    {
        "id": "0ae75004c433dcc1",
        "type": "function",
        "z": "787c361b51b9d832",
        "name": "is currentTemp greater then OFF",
        "func": "if (flow.currentTemp < flow.threshold){\nmsg.payload = \"OFF\";}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 710,
        "y": 260,
        "wires": [
            [
                "d3f0d0e1559e86df"
            ]
        ]
    },
    {
        "id": "d3f0d0e1559e86df",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "OFF",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 960,
        "y": 260,
        "wires": [
            [
                "adc79e3075a38133",
                "7430bc2f365004ac"
            ]
        ]
    },
    {
        "id": "ca07b1e317370f3a",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "ON",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 940,
        "y": 160,
        "wires": [
            [
                "9ccddd93d34e7fc1",
                "7430bc2f365004ac"
            ]
        ]
    },
    {
        "id": "7430bc2f365004ac",
        "type": "debug",
        "z": "787c361b51b9d832",
        "name": "debug 299",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1090,
        "y": 100,
        "wires": []
    }
]

The values of flow.currentTemp and flow.threshold are not available in a function this way.

if (flow.currentTemp >= flow.threshold){
msg.payload = 'ON';}
return msg;

You have to do this

const now = flow.get("currentTemp") ?? 0  // default 0 if var does not exist
const threshold = flow.get("threshold") ?? 0
if (now >= threshold){
msg.payload = 'ON'
}
return msg;
1 Like

Hmm,
I tried that. But it's still firing both functions and I get "ON" and "OFF" at the same time. Not sure how it's validating through both functions. Maybe it needs an else return null?

Here's the above threshold

const now = flow.get("currentTemp") ?? 0  // default 0 if var does not exist
const threshold = flow.get("threshold") ?? 0
if (now >= threshold) {
    msg.payload = 'ON'
}
return msg;

Here's the below threshold

const now = flow.get("currentTemp") ?? 0  // default 0 if var does not exist
const threshold = flow.get("threshold") ?? 0
if (now < threshold) {
    msg.payload = 'OFF'
}
return msg;

Here's the relevant flow

[
    {
        "id": "9e5e69a5e191851d",
        "type": "switch",
        "z": "787c361b51b9d832",
        "name": "ON",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "ON",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 630,
        "y": 440,
        "wires": [
            [
                "0271de3c43e83b63",
                "15df05b0c1c9bc03",
                "47799609d55e4892",
                "3c181d2edef0aa42"
            ]
        ]
    },
    {
        "id": "8fb38073d051089f",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "Set currentTemp FLOW Value",
        "rules": [
            {
                "t": "set",
                "p": "currentTemp",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 370,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "6283761a5021dc1f",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "Set threshold FLOW Value",
        "rules": [
            {
                "t": "set",
                "p": "threshold",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 380,
        "y": 180,
        "wires": [
            [
                "a5e0f4f975982884"
            ]
        ]
    },
    {
        "id": "47799609d55e4892",
        "type": "file in",
        "z": "787c361b51b9d832",
        "name": "READ fanThreshold.txt",
        "filename": "/home/pi/scripts/temperature/currentTemps/fanThreshold.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 140,
        "y": 180,
        "wires": [
            [
                "6283761a5021dc1f"
            ]
        ]
    },
    {
        "id": "15df05b0c1c9bc03",
        "type": "file in",
        "z": "787c361b51b9d832",
        "name": "READ tempNow.txt",
        "filename": "/home/pi/scripts/temperature/currentTemps/insideFromApt.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 130,
        "y": 240,
        "wires": [
            [
                "8fb38073d051089f"
            ]
        ]
    },
    {
        "id": "3c181d2edef0aa42",
        "type": "delay",
        "z": "787c361b51b9d832",
        "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": 600,
        "y": 220,
        "wires": [
            [
                "fe6085756568d779",
                "0ae75004c433dcc1"
            ]
        ]
    },
    {
        "id": "fe6085756568d779",
        "type": "function",
        "z": "787c361b51b9d832",
        "name": "is currentTemp greater than threshold ON",
        "func": "const now = flow.get(\"currentTemp\") ?? 0  // default 0 if var does not exist\nconst threshold = flow.get(\"threshold\") ?? 0\nif (now >= threshold) {\n    msg.payload = 'ON'\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 720,
        "y": 140,
        "wires": [
            [
                "ca07b1e317370f3a"
            ]
        ]
    },
    {
        "id": "0ae75004c433dcc1",
        "type": "function",
        "z": "787c361b51b9d832",
        "name": "is currentTemp less than threshold? OFF",
        "func": "const now = flow.get(\"currentTemp\") ?? 0  // default 0 if var does not exist\nconst threshold = flow.get(\"threshold\") ?? 0\nif (now < threshold) {\n    msg.payload = 'OFF'\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 740,
        "y": 260,
        "wires": [
            [
                "d3f0d0e1559e86df"
            ]
        ]
    },
    {
        "id": "d3f0d0e1559e86df",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "OFF",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 960,
        "y": 260,
        "wires": [
            [
                "adc79e3075a38133",
                "7430bc2f365004ac"
            ]
        ]
    },
    {
        "id": "ca07b1e317370f3a",
        "type": "change",
        "z": "787c361b51b9d832",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "ON",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 940,
        "y": 160,
        "wires": [
            [
                "9ccddd93d34e7fc1",
                "7430bc2f365004ac"
            ]
        ]
    },
    {
        "id": "7430bc2f365004ac",
        "type": "debug",
        "z": "787c361b51b9d832",
        "name": "debug 299",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1090,
        "y": 100,
        "wires": []
    }
]

To be honest I find your flow rather confusing with it's temporary files and context variables, but I assume there are good reasons for that.

I do not see any need for the two function nodes though.
Can you test if this tweeked fow works?

[{"id":"f14e4f2a567f3bb1","type":"tab","label":"Flow 5","disabled":false,"info":"","env":[]},{"id":"d4604b09a0e981e6","type":"group","z":"f14e4f2a567f3bb1","name":"Your flow, rearranged for compactness","style":{"label":true},"nodes":["9e5e69a5e191851d","8fb38073d051089f","15df05b0c1c9bc03","47799609d55e4892","6283761a5021dc1f","3c181d2edef0aa42","fe6085756568d779","0ae75004c433dcc1","d3f0d0e1559e86df","ca07b1e317370f3a","7430bc2f365004ac"],"x":14,"y":19,"w":952,"h":202},{"id":"e91b8b463f6e7e5d","type":"group","z":"f14e4f2a567f3bb1","name":"Two functions replaced by a switch node","style":{"label":true},"nodes":["63a023ac278b6eb5","2dbdf0742126029a","2edff2d360fc5191","7302de5e906c805c","4b09dcc50b174c56","0dbd7f8da6fe7250","c6b30a78f6b122ec","9a6be2080614715e","8311b68406384bda","8cb998c121247b9c"],"x":14,"y":239,"w":952,"h":202},{"id":"9e5e69a5e191851d","type":"switch","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"ON","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":90,"y":120,"wires":[["15df05b0c1c9bc03","47799609d55e4892","3c181d2edef0aa42"]]},{"id":"8fb38073d051089f","type":"change","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"Set currentTemp FLOW Value","rules":[{"t":"set","p":"currentTemp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":180,"wires":[[]]},{"id":"15df05b0c1c9bc03","type":"file in","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"READ tempNow.txt","filename":"/home/pi/scripts/temperature/currentTemps/insideFromApt.txt","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":210,"y":180,"wires":[["8fb38073d051089f"]]},{"id":"47799609d55e4892","type":"file in","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"READ fanThreshold.txt","filename":"/home/pi/scripts/temperature/currentTemps/fanThreshold.txt","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":200,"y":60,"wires":[["6283761a5021dc1f"]]},{"id":"6283761a5021dc1f","type":"change","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"Set threshold FLOW Value","rules":[{"t":"set","p":"threshold","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":60,"wires":[[]]},{"id":"3c181d2edef0aa42","type":"delay","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","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":240,"y":120,"wires":[["fe6085756568d779","0ae75004c433dcc1"]]},{"id":"fe6085756568d779","type":"function","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"is currentTemp greater then ON","func":"if (flow.currentTemp >= flow.threshold){\nmsg.payload = 'ON';}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":100,"wires":[["ca07b1e317370f3a"]]},{"id":"0ae75004c433dcc1","type":"function","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"is currentTemp greater then OFF","func":"if (flow.currentTemp < flow.threshold){\nmsg.payload = \"OFF\";}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":140,"wires":[["d3f0d0e1559e86df"]]},{"id":"d3f0d0e1559e86df","type":"change","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":140,"wires":[["7430bc2f365004ac"]]},{"id":"ca07b1e317370f3a","type":"change","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":100,"wires":[["7430bc2f365004ac"]]},{"id":"7430bc2f365004ac","type":"debug","z":"f14e4f2a567f3bb1","g":"d4604b09a0e981e6","name":"debug 299","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":120,"wires":[]},{"id":"8cb998c121247b9c","type":"switch","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"","property":"currentTemp","propertyType":"flow","rules":[{"t":"gte","v":"threshold","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":530,"y":340,"wires":[["9a6be2080614715e"],["c6b30a78f6b122ec"]]},{"id":"63a023ac278b6eb5","type":"switch","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"ON","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"ON","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":90,"y":340,"wires":[["2edff2d360fc5191","7302de5e906c805c","0dbd7f8da6fe7250"]]},{"id":"2dbdf0742126029a","type":"change","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"Set currentTemp FLOW Value","rules":[{"t":"set","p":"currentTemp","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":400,"wires":[[]]},{"id":"2edff2d360fc5191","type":"file in","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"READ tempNow.txt","filename":"/home/pi/scripts/temperature/currentTemps/insideFromApt.txt","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":210,"y":400,"wires":[["2dbdf0742126029a"]]},{"id":"7302de5e906c805c","type":"file in","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"READ fanThreshold.txt","filename":"/home/pi/scripts/temperature/currentTemps/fanThreshold.txt","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":200,"y":280,"wires":[["4b09dcc50b174c56"]]},{"id":"4b09dcc50b174c56","type":"change","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"Set threshold FLOW Value","rules":[{"t":"set","p":"threshold","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":280,"wires":[[]]},{"id":"0dbd7f8da6fe7250","type":"delay","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","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":240,"y":340,"wires":[["8cb998c121247b9c"]]},{"id":"c6b30a78f6b122ec","type":"change","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"OFF","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":360,"wires":[["8311b68406384bda"]]},{"id":"9a6be2080614715e","type":"change","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"ON","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":320,"wires":[["8311b68406384bda"]]},{"id":"8311b68406384bda","type":"debug","z":"f14e4f2a567f3bb1","g":"e91b8b463f6e7e5d","name":"debug 321","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":850,"y":340,"wires":[]}]

And note that if the current temp is very close to the threshold and the fan turns on, the temperature drops a tiny bit and the fan turns off, temperature rises a tiny bit ...
To avoid such behaviour you need to introduce a bit of hysteresis (think that's the right word) The temperature where the fan turns on should be somewhat higher than the temperature where it turns off again.

Awesome! It works!

Thanks again jbudd! You've helped me a bunch of times and I really appreciate it!

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