Beagle Bone Black Thermostat problems with the "read file" node

6/13/2024, 10:45:56 AMnode: Temperature Handlerfunction : (error)

"TypeError: Cannot read properties of undefined (reading 'readFile')"

[
    {
        "id": "2ffefe74a7d3908d",
        "type": "file in",
        "z": "f2116ba899830da1",
        "name": "temp read",
        "filename": "/home/debian/temp.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 380,
        "y": 720,
        "wires": [
            [
                "5def5a4cb6ba7bf6"
            ]
        ]
    }
] 

the file exists, permissions are 777 (for testing)
Here's the entire flow just in case.

    {
        "id": "f2116ba899830da1",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "9a72c14afcfe7cde",
        "type": "mqtt in",
        "z": "f2116ba899830da1",
        "name": "Heatpump/heat",
        "topic": "heatpump/heat",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "38bf5da89401e7ba",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 140,
        "y": 220,
        "wires": [
            [
                "b9f5d8c69ebecb0b"
            ]
        ]
    },
    {
        "id": "cdaf3563d4dbe443",
        "type": "mqtt in",
        "z": "f2116ba899830da1",
        "name": "Auto MQTT",
        "topic": "heatpump/auto",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "38bf5da89401e7ba",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 130,
        "y": 340,
        "wires": [
            [
                "06732bcc45d44c8f"
            ]
        ]
    },
    {
        "id": "232b922219c09889",
        "type": "mqtt in",
        "z": "f2116ba899830da1",
        "name": "Fan MQTT",
        "topic": "heatpump/fan",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "38bf5da89401e7ba",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 120,
        "y": 40,
        "wires": [
            [
                "6d0162ef5ac15ca1"
            ]
        ]
    },
    {
        "id": "8434a9bf5dc59c66",
        "type": "mqtt in",
        "z": "f2116ba899830da1",
        "name": "Cool MQTT",
        "topic": "heatpump/cool",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "38bf5da89401e7ba",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 130,
        "y": 120,
        "wires": [
            [
                "4117b0a8545ff458"
            ]
        ]
    },
    {
        "id": "2c567478d696b027",
        "type": "mqtt in",
        "z": "f2116ba899830da1",
        "name": "Desired MQTT",
        "topic": "heatpump/desired",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "38bf5da89401e7ba",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 140,
        "y": 440,
        "wires": [
            [
                "25eacfa4065e1ac2"
            ]
        ]
    },
    {
        "id": "6c71cf659898f4f6",
        "type": "mqtt in",
        "z": "f2116ba899830da1",
        "name": "Temp MQTT",
        "topic": "heatpump/temp",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "38bf5da89401e7ba",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 130,
        "y": 540,
        "wires": [
            [
                "5b4243b766ac3bd6"
            ]
        ]
    },
    {
        "id": "6d0162ef5ac15ca1",
        "type": "switch",
        "z": "f2116ba899830da1",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 330,
        "y": 40,
        "wires": [
            [
                "3d4a86e60cd25ae3"
            ],
            [
                "d4a4f76557c8de3d"
            ]
        ]
    },
    {
        "id": "4117b0a8545ff458",
        "type": "switch",
        "z": "f2116ba899830da1",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 330,
        "y": 120,
        "wires": [
            [
                "97835733171620c4",
                "d17414d88237089c",
                "3d4a86e60cd25ae3"
            ],
            [
                "7dcf49e09582cbac",
                "d4a4f76557c8de3d"
            ]
        ]
    },
    {
        "id": "b9f5d8c69ebecb0b",
        "type": "switch",
        "z": "f2116ba899830da1",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 330,
        "y": 220,
        "wires": [
            [
                "d8063f57c29aca44",
                "7dcf49e09582cbac",
                "3d4a86e60cd25ae3"
            ],
            [
                "d17414d88237089c",
                "d4a4f76557c8de3d"
            ]
        ]
    },
    {
        "id": "3d4a86e60cd25ae3",
        "type": "exec",
        "z": "f2116ba899830da1",
        "command": "home/debian/fan_on.sh",
        "addpay": "",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "Fan ON",
        "x": 680,
        "y": 20,
        "wires": [
            [
                "ddef8c95892a987f"
            ],
            [],
            []
        ]
    },
    {
        "id": "7dcf49e09582cbac",
        "type": "exec",
        "z": "f2116ba899830da1",
        "command": "home/debian/cool_off.sh",
        "addpay": "",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "Cool OFF",
        "x": 880,
        "y": 140,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "feef1ff0a4219b77",
        "type": "exec",
        "z": "f2116ba899830da1",
        "command": "home/debian/cool_on.sh",
        "addpay": "",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "Cool ON",
        "x": 680,
        "y": 100,
        "wires": [
            [
                "4452067148b87859"
            ],
            [],
            []
        ]
    },
    {
        "id": "d17414d88237089c",
        "type": "exec",
        "z": "f2116ba899830da1",
        "command": "home/debian/heat_off.sh",
        "addpay": "",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "Heat OFF",
        "x": 880,
        "y": 240,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "9f316e51d609cefd",
        "type": "exec",
        "z": "f2116ba899830da1",
        "command": "home/debian/heat_on.sh",
        "addpay": "",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "Heat ON",
        "x": 700,
        "y": 200,
        "wires": [
            [
                "7082cffb63cc10ac"
            ],
            [],
            []
        ]
    },
    {
        "id": "97835733171620c4",
        "type": "delay",
        "z": "f2116ba899830da1",
        "name": "",
        "pauseType": "delay",
        "timeout": "30",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 500,
        "y": 120,
        "wires": [
            [
                "feef1ff0a4219b77"
            ]
        ]
    },
    {
        "id": "d8063f57c29aca44",
        "type": "delay",
        "z": "f2116ba899830da1",
        "name": "",
        "pauseType": "delay",
        "timeout": "30",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 500,
        "y": 220,
        "wires": [
            [
                "9f316e51d609cefd"
            ]
        ]
    },
    {
        "id": "06732bcc45d44c8f",
        "type": "switch",
        "z": "f2116ba899830da1",
        "name": "",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "ON",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "OFF",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 330,
        "y": 340,
        "wires": [
            [
                "5def5a4cb6ba7bf6"
            ],
            []
        ]
    },
    {
        "id": "25eacfa4065e1ac2",
        "type": "file",
        "z": "f2116ba899830da1",
        "name": "desired.txt",
        "filename": "/home/debian/desired.txt",
        "filenameType": "str",
        "appendNewline": false,
        "createDir": false,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 370,
        "y": 440,
        "wires": [
            []
        ]
    },
    {
        "id": "5b4243b766ac3bd6",
        "type": "file",
        "z": "f2116ba899830da1",
        "name": "temp.txt",
        "filename": "/home/debian/temp.txt",
        "filenameType": "str",
        "appendNewline": false,
        "createDir": false,
        "overwriteFile": "true",
        "encoding": "none",
        "x": 360,
        "y": 540,
        "wires": [
            []
        ]
    },
    {
        "id": "36ca8d85981c0d22",
        "type": "file in",
        "z": "f2116ba899830da1",
        "name": "desired read",
        "filename": "/home/debian/desired.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 370,
        "y": 660,
        "wires": [
            [
                "5def5a4cb6ba7bf6"
            ]
        ]
    },
    {
        "id": "2ffefe74a7d3908d",
        "type": "file in",
        "z": "f2116ba899830da1",
        "name": "temp read",
        "filename": "/home/debian/temp.txt",
        "filenameType": "str",
        "format": "utf8",
        "chunk": false,
        "sendError": false,
        "encoding": "none",
        "allProps": false,
        "x": 380,
        "y": 720,
        "wires": [
            [
                "5def5a4cb6ba7bf6"
            ]
        ]
    },
    {
        "id": "743f7d650d452752",
        "type": "watch",
        "z": "f2116ba899830da1",
        "name": "desired watch",
        "files": "/home/debian/desired.txt",
        "recursive": "",
        "x": 170,
        "y": 660,
        "wires": [
            [
                "36ca8d85981c0d22"
            ]
        ]
    },
    {
        "id": "00c584c303162185",
        "type": "watch",
        "z": "f2116ba899830da1",
        "name": "temp watch",
        "files": "/home/debian/temp.txt",
        "recursive": "",
        "x": 170,
        "y": 720,
        "wires": [
            [
                "2ffefe74a7d3908d"
            ]
        ]
    },
    {
        "id": "ddef8c95892a987f",
        "type": "mqtt out",
        "z": "f2116ba899830da1",
        "name": "Fan Condition",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "38bf5da89401e7ba",
        "x": 1280,
        "y": 40,
        "wires": []
    },
    {
        "id": "4452067148b87859",
        "type": "mqtt out",
        "z": "f2116ba899830da1",
        "name": "Cooling",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "38bf5da89401e7ba",
        "x": 1260,
        "y": 120,
        "wires": []
    },
    {
        "id": "7082cffb63cc10ac",
        "type": "mqtt out",
        "z": "f2116ba899830da1",
        "name": "Heating",
        "topic": "",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "38bf5da89401e7ba",
        "x": 1260,
        "y": 200,
        "wires": []
    },
    {
        "id": "d4a4f76557c8de3d",
        "type": "exec",
        "z": "f2116ba899830da1",
        "command": "/home/debian/fan_off.sh",
        "addpay": "",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "Fan OFF",
        "x": 880,
        "y": 60,
        "wires": [
            [
                "ddef8c95892a987f"
            ],
            [],
            []
        ]
    },
    {
        "id": "5def5a4cb6ba7bf6",
        "type": "function",
        "z": "f2116ba899830da1",
        "name": "Temperature Handler",
        "func": "const fs = global.get('fs');\n\nfs.readFile('/home/debian/desired.txt', 'utf8', (err, desiredData) => {\n    if (err) {\n        node.error('Error reading desired temperature file', err);\n        return;\n    }\n    const desiredTemp = parseFloat(desiredData.trim());\n\n    fs.readFile('/home/debian/temp.txt', 'utf8', (err, actualData) => {\n        if (err) {\n            node.error('Error reading actual temperature file', err);\n            return;\n        }\n        const actualTemp = parseFloat(actualData.trim());\n\n        let msg = null;\n\n        if (actualTemp >= desiredTemp + 2) {\n            msg = { topic: 'heatpump/cool', payload: 'ON' };\n        } else if (actualTemp <= desiredTemp - 2) {\n            msg = { topic: 'heatpump/heat', payload: 'ON' };\n        } else if (actualTemp <= desiredTemp + 1) {\n            msg = { topic: 'heatpump/cool', payload: 'OFF' };\n        } else if (actualTemp >= desiredTemp - 1) {\n            msg = { topic: 'heatpump/heat', payload: 'OFF' };\n        }\n\n        if (msg) {\n            node.send([msg, null]);\n        }\n    });\n});\nreturn [null, null];",
        "outputs": 2,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 660,
        "wires": [
            [
                "4117b0a8545ff458",
                "c04c50d381c27c80"
            ],
            [
                "b9f5d8c69ebecb0b",
                "bc6ff2b69b0a7c28"
            ]
        ]
    },
    {
        "id": "c04c50d381c27c80",
        "type": "debug",
        "z": "f2116ba899830da1",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 880,
        "y": 600,
        "wires": []
    },
    {
        "id": "bc6ff2b69b0a7c28",
        "type": "debug",
        "z": "f2116ba899830da1",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 880,
        "y": 720,
        "wires": []
    },
    {
        "id": "38bf5da89401e7ba",
        "type": "mqtt-broker",
        "name": "Test",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "autoUnsubscribe": true,
        "birthTopic": "home/test",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closeRetain": "false",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    }
]

I am an old electrician and am a little lost in software.

@willgoon12337 Welcome to the forum!

What does the file (/home/debian/temp.txt) contain?
Can you copy the contents and paste it to a response?

Also what platform are you running NR on?
What version of NR and node.js are you using (see node-red startup log to get that info)

75 at the moment it's supposed to be written by the output of an MQTT topic heatpump/temp

debian bookworm

     Loaded: loaded (/lib/systemd/system/nodered.service; disabled; preset: enabled)
     Active: active (running) since Thu 2024-06-13 15:02:19 UTC; 13s ago
       Docs: https://nodered.org/docs/
   Main PID: 4490 (node)
      Tasks: 18 (limit: 1024)
     Memory: 27.9M
        CPU: 12.953s
     CGroup: /system.slice/nodered.service
             ├─4490 /opt/node-red/bin/node --max_old_space_size=256 /opt/node-red/lib/node_modules/node-red/red.js
             └─4501 npm

Jun 13 15:02:19 BeagleBone systemd[1]: Started nodered.service - Node-RED graphical event wiring tool.
Jun 13 15:02:27 BeagleBone Node-RED[4490]: 13 Jun 15:02:27 - [info]
Jun 13 15:02:27 BeagleBone Node-RED[4490]: Welcome to Node-RED
Jun 13 15:02:27 BeagleBone Node-RED[4490]: ===================
Jun 13 15:02:27 BeagleBone Node-RED[4490]: 13 Jun 15:02:27 - [info] Node-RED version: v3.1.2
Jun 13 15:02:27 BeagleBone Node-RED[4490]: 13 Jun 15:02:27 - [info] Node.js  version: v16.20.2
Jun 13 15:02:27 BeagleBone Node-RED[4490]: 13 Jun 15:02:27 - [info] Linux 5.10.168-ti-r77 arm LE

Where do you set the global you access in your function node?
const fs = global.get('fs');
What if it doesn't exist?

debian@BeagleBone:~$ nano .node-red/settings.js

Why are you trying to read the file in the function node? You are already reading it in the read file node.

From the .node-red/settings.js

     * This allows extra node modules to be made available with in Function node.
     * For example, the following:
     *    functionGlobalContext: { os:require('os') }
     * will allow the `os` module to be accessed in a Function node using:
     *    global.get("os")
     */
    functionGlobalContext: {
        fs: require('fs'),
    },

I hate to bother you fellas with this but I don't have any hair left to pull out.

to compare it to the desired.txt which is also a number set by MQtt from Home Assistant.

Might I suggest another way. Take a look at this flow:

[{"id":"36ca8d85981c0d22","type":"file in","z":"f2116ba899830da1","name":"desired read","filename":"/home/debian/desired.txt","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":310,"y":640,"wires":[["01813f9fb5bd06d2"]]},{"id":"2ffefe74a7d3908d","type":"file in","z":"f2116ba899830da1","name":"temp read","filename":"/home/debian/temp.txt","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":300,"y":720,"wires":[["47a02785e718657f"]]},{"id":"743f7d650d452752","type":"watch","z":"f2116ba899830da1","name":"desired watch","files":"/home/debian/desired.txt","recursive":"","x":90,"y":640,"wires":[["36ca8d85981c0d22"]]},{"id":"00c584c303162185","type":"watch","z":"f2116ba899830da1","name":"temp watch","files":"/home/debian/temp.txt","recursive":"","x":90,"y":680,"wires":[["36ca8d85981c0d22"]]},{"id":"5def5a4cb6ba7bf6","type":"function","z":"f2116ba899830da1","name":"Temperature Handler","func":"const fs = global.get('fs')||0;\n\nfs.readFile('/home/debian/desired.txt', 'utf8', (err, desiredData) => {\n    if (err) {\n        node.error('Error reading desired temperature file', err);\n        return;\n    }\n    const desiredTemp = parseFloat(desiredData.trim());\n\n    fs.readFile('/home/debian/temp.txt', 'utf8', (err, actualData) => {\n        if (err) {\n            node.error('Error reading actual temperature file', err);\n            return;\n        }\n        const actualTemp = parseFloat(actualData.trim());\n\n        let msg = null;\n\n        if (actualTemp >= desiredTemp + 2) {\n            msg = { topic: 'heatpump/cool', payload: 'ON' };\n        } else if (actualTemp <= desiredTemp - 2) {\n            msg = { topic: 'heatpump/heat', payload: 'ON' };\n        } else if (actualTemp <= desiredTemp + 1) {\n            msg = { topic: 'heatpump/cool', payload: 'OFF' };\n        } else if (actualTemp >= desiredTemp - 1) {\n            msg = { topic: 'heatpump/heat', payload: 'OFF' };\n        }\n\n        if (msg) {\n            node.send([msg, null]);\n        }\n    });\n});\nreturn [null, null];","outputs":2,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":680,"wires":[["4117b0a8545ff458","c04c50d381c27c80"],["b9f5d8c69ebecb0b","bc6ff2b69b0a7c28"]]},{"id":"c04c50d381c27c80","type":"debug","z":"f2116ba899830da1","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":600,"wires":[]},{"id":"bc6ff2b69b0a7c28","type":"debug","z":"f2116ba899830da1","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":720,"wires":[]},{"id":"47a02785e718657f","type":"change","z":"f2116ba899830da1","name":"","rules":[{"t":"set","p":"temp","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":760,"wires":[["5def5a4cb6ba7bf6","d322563860498e6e"]]},{"id":"01813f9fb5bd06d2","type":"change","z":"f2116ba899830da1","name":"","rules":[{"t":"set","p":"desired","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":680,"wires":[["2ffefe74a7d3908d"]]},{"id":"d322563860498e6e","type":"debug","z":"f2116ba899830da1","name":"debug 3450","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":760,"wires":[]},{"id":"2684223e9304092f","type":"inject","z":"f2116ba899830da1","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":600,"wires":[["36ca8d85981c0d22"]]}]

So what it does is if either of the watch nodes (or the inject) trigger, the code will
1 - read the desired file
2 - move msg.payload to msg.desired
3 - read the temp file
4 - read move msg.payload to msg.temp
5 - sent the msg to the function node (which you will have to change.) in the msg passed to the function node, you will have msg.desired and msg.temp that you can access and use.

[UPDATE] you could also add this flow

[{"id":"313f9d07fbf73007","type":"debug","z":"f2116ba899830da1","name":"debug 3452","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":920,"wires":[]},{"id":"482a1b3c765f42a6","type":"catch","z":"f2116ba899830da1","name":"","scope":null,"uncaught":false,"x":560,"y":880,"wires":[["1aff7a090c887cd3"]]},{"id":"a4e24a115ad067a1","type":"debug","z":"f2116ba899830da1","name":"if file doesn't exist","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"error.message","targetType":"msg","statusVal":"error.message","statusType":"auto","x":890,"y":840,"wires":[]},{"id":"1aff7a090c887cd3","type":"switch","z":"f2116ba899830da1","name":"","property":"error.message","propertyType":"msg","rules":[{"t":"cont","v":"Error: ENOENT:","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":710,"y":880,"wires":[["a4e24a115ad067a1"],["313f9d07fbf73007"]]}]

which will send an error message to the debut log if either file is missing.

[Update 2]
you could change your function node to this:

let desiredData = msg.desired
let actualData  = mst.temp
    
const desiredTemp = parseFloat(desiredData.trim());
const actualTemp = parseFloat(actualData.trim());

let msg = null;

if (actualTemp >= desiredTemp + 2) {
    msg = { topic: 'heatpump/cool', payload: 'ON' };
} else if (actualTemp <= desiredTemp - 2) {
    msg = { topic: 'heatpump/heat', payload: 'ON' };
} else if (actualTemp <= desiredTemp + 1) {
    msg = { topic: 'heatpump/cool', payload: 'OFF' };
} else if (actualTemp >= desiredTemp - 1) {
    msg = { topic: 'heatpump/heat', payload: 'OFF' };
}

if (msg) {
    node.send([msg, null]);
}
return [null, null];

thank you very much, I am leaning quite a lot playing with this. I wish that I had known about node-red years ago.

Error Msg

function : (error)
"SyntaxError: Identifier 'msg' has already been declared (body:line 7)" ```

Can you show me your function node?

I deleted line 7 and that got rid of the error

let actualData = msg.temp

const desiredTemp = parseFloat(desiredData.trim());
const actualTemp = parseFloat(actualData.trim());

if (actualTemp >= desiredTemp + 2) {
    msg = { topic: 'heatpump/cool', payload: 'ON' };
} else if (actualTemp <= desiredTemp - 2) {
    msg = { topic: 'heatpump/heat', payload: 'ON' };
} else if (actualTemp <= desiredTemp + 1) {
    msg = { topic: 'heatpump/cool', payload: 'OFF' };
} else if (actualTemp >= desiredTemp - 1) {
    msg = { topic: 'heatpump/heat', payload: 'OFF' };
}

if (msg) {
    node.send([msg, null]);
}
return [null, null]; ```

Glad to have been helpful! Here is some other things that might help you out.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

Thanks for all the help, and I will watch the videos because I still have a lot I want to do with the BBB and HA. It's working perfectly now BTW.

1 Like

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