# Why cant i call Globalvariable in another function?

**URL:** <https://discourse.nodered.org/t/why-cant-i-call-globalvariable-in-another-function/74018>\
**Category:** General\
**Tags:** function-node\
**Created:** [21 January 2023 20:03 UTC](https://discourse.nodered.org/t/why-cant-i-call-globalvariable-in-another-function/74018 "2023-01-21T20:03:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rizkiaswangga](https://avatars.discourse-cdn.com/v4/letter/r/4af34b/32.png) [@rizkiaswangga](https://discourse.nodered.org/u/rizkiaswangga)\
**Post date:** [21 January 2023 20:03 UTC](https://discourse.nodered.org/t/why-cant-i-call-globalvariable-in-another-function/74018/1 "2023-01-21T20:03:11Z")

</div>

Hello everybody.....

I'm new to Node-Red and new to the forum, so this is the first time posting on this forum 😉

so, i'm trying to make a Global Variable on "function 4", which the later can be accessed on the "function 5" without connecting directly, i've been changing the syntax on the code but still i can get function 5 to display the globalvariable set by function 4? what's wrong on the code? ☹

to the left is a bit messy, i'll tidy it up after i've successfully able to call the globalvariable 😃

```auto
[
    {
        "id": "3d794c16765e7902",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "2ea135a572225763",
        "type": "mqtt in",
        "z": "3d794c16765e7902",
        "name": "",
        "topic": "sensors",
        "qos": "0",
        "datatype": "auto-detect",
        "broker": "f58d6e8545746097",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 90,
        "y": 220,
        "wires": [
            [
                "c16314662bd62116"
            ]
        ]
    },
    {
        "id": "c16314662bd62116",
        "type": "json",
        "z": "3d794c16765e7902",
        "name": "",
        "property": "payload",
        "action": "str",
        "pretty": false,
        "x": 210,
        "y": 220,
        "wires": [
            [
                "4c7f21c7d7244827",
                "9d95ce3a7b9191a3"
            ]
        ]
    },
    {
        "id": "d2bb71c71a19eee6",
        "type": "debug",
        "z": "3d794c16765e7902",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 500,
        "y": 360,
        "wires": []
    },
    {
        "id": "0ef756c805ee027e",
        "type": "mysql",
        "z": "3d794c16765e7902",
        "mydb": "633ea3fdbad1a6f4",
        "name": "",
        "x": 530,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "4c7f21c7d7244827",
        "type": "function",
        "z": "3d794c16765e7902",
        "name": "function 1",
        "func": "msg.topic = \"INSERT INTO tbl_sensors (water_level_sensor, soil_condition, weather_info) VALUES (\" + parseFloat(msg.payload.split(',')[0]) + \",\" + \"'\" + msg.payload.split(',')[1] + \"'\" + \",\" + \"'\" + msg.payload.split(',')[2] + \"'\" + \");\"\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 360,
        "y": 280,
        "wires": [
            [
                "d2bb71c71a19eee6",
                "0ef756c805ee027e"
            ]
        ]
    },
    {
        "id": "60eceb19406fe5da",
        "type": "ui_gauge",
        "z": "3d794c16765e7902",
        "name": "Tank Level",
        "group": "54f06fbf65a33cdb",
        "order": 1,
        "width": 0,
        "height": 0,
        "gtype": "wave",
        "title": "Tank Level",
        "label": "%",
        "format": "{{msg.water_level}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "diff": false,
        "className": "",
        "x": 530,
        "y": 160,
        "wires": []
    },
    {
        "id": "028c0629f9b5bf8c",
        "type": "ui_text",
        "z": "3d794c16765e7902",
        "group": "54f06fbf65a33cdb",
        "order": 1,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "The current Soil Condition is :",
        "format": "{{msg.soil_level}}",
        "layout": "row-center",
        "className": "",
        "x": 590,
        "y": 100,
        "wires": []
    },
    {
        "id": "9d95ce3a7b9191a3",
        "type": "function",
        "z": "3d794c16765e7902",
        "name": "function 2",
        "func": "var soil_level = msg.payload.split(\",\")[1];\nmsg.soil_level = soil_level;\n\nvar water_level = msg.payload.split(\",\")[0];\nmsg.water_level = water_level;\n\nvar weather_condition = msg.payload.split(\",\")[2];\nmsg.weather_condition = weather_condition;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 360,
        "y": 140,
        "wires": [
            [
                "028c0629f9b5bf8c",
                "60eceb19406fe5da",
                "a706fe101af18a12",
                "aafb99f462199583"
            ]
        ]
    },
    {
        "id": "5ac8bffefd45a2c9",
        "type": "mysql",
        "z": "3d794c16765e7902",
        "mydb": "633ea3fdbad1a6f4",
        "name": "",
        "x": 870,
        "y": 220,
        "wires": [
            [
                "a52fdada52bcaab6"
            ]
        ]
    },
    {
        "id": "a52fdada52bcaab6",
        "type": "function",
        "z": "3d794c16765e7902",
        "name": "function 3",
        "func": "var query = \"SELECT * FROM tbl_sensors\";\n\nvar result = global.get('mysql').query(query, function (err, rows, fields) {\n if (!err) {\n msg.payload = rows;\n }\n else {\n msg.payload = \"Error: \" + err;\n }\n return msg;\n});",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1080,
        "y": 220,
        "wires": [
            [
                "a4c3dd1ebd1d71ba"
            ]
        ]
    },
    {
        "id": "a7b503eb96c1bc04",
        "type": "ui_chart",
        "z": "3d794c16765e7902",
        "name": "",
        "order": 0,
        "width": 0,
        "height": 0,
        "label": "chart",
        "chartType": "line",
        "legend": "false",
        "xformat": "HH:mm:ss",
        "interpolate": "linear",
        "nodata": "",
        "dot": false,
        "ymin": "",
        "ymax": "",
        "removeOlder": 1,
        "removeOlderPoints": "",
        "removeOlderUnit": "3600",
        "cutout": 0,
        "useOneColor": false,
        "useUTC": false,
        "colors": [
            "#1F77B4",
            "#AEC7E8",
            "#FF7F0E",
            "#2CA02C",
            "#98DF8A",
            "#D62728",
            "#FF9896",
            "#9467BD",
            "#C5B0D5"
        ],
        "outputs": 1,
        "useDifferentColor": false,
        "className": "",
        "x": 1270,
        "y": 220,
        "wires": [
            []
        ]
    },
    {
        "id": "a4c3dd1ebd1d71ba",
        "type": "debug",
        "z": "3d794c16765e7902",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1280,
        "y": 160,
        "wires": []
    },
    {
        "id": "a706fe101af18a12",
        "type": "ui_text",
        "z": "3d794c16765e7902",
        "group": "54f06fbf65a33cdb",
        "order": 0,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "The Weather is : ",
        "format": "{{msg.weather_condition}}",
        "layout": "row-center",
        "className": "",
        "x": 560,
        "y": 40,
        "wires": []
    },
    {
        "id": "25b8a1835a49ca6e",
        "type": "ui_button",
        "z": "3d794c16765e7902",
        "name": "",
        "group": "27631c4f948d2939",
        "order": 3,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Restart System",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "true",
        "payloadType": "bool",
        "topic": "topic",
        "topicType": "msg",
        "x": 940,
        "y": 80,
        "wires": [
            [
                "0b9bf4f26910e495"
            ]
        ]
    },
    {
        "id": "0b9bf4f26910e495",
        "type": "mqtt out",
        "z": "3d794c16765e7902",
        "name": "",
        "topic": "Control",
        "qos": "",
        "retain": "",
        "respTopic": "",
        "contentType": "",
        "userProps": "",
        "correl": "",
        "expiry": "",
        "broker": "f58d6e8545746097",
        "x": 1220,
        "y": 80,
        "wires": []
    },
    {
        "id": "aafb99f462199583",
        "type": "function",
        "z": "3d794c16765e7902",
        "name": "function 4",
        "func": "function setGlobalVariables(msg) {\n context.set(\"water_level\", msg.water_level);\n context.set(\"soil_level\", msg.soil_level);\n context.set(\"weather_condition\", msg.weather_condition);\n return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 200,
        "wires": [
            []
        ]
    },
    {
        "id": "a11869ee09f9c414",
        "type": "http in",
        "z": "3d794c16765e7902",
        "name": "",
        "url": "/plantwatering",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 250,
        "y": 560,
        "wires": [
            [
                "752c6c28c7ae92ef"
            ]
        ]
    },
    {
        "id": "127958d67fe94006",
        "type": "http response",
        "z": "3d794c16765e7902",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 750,
        "y": 560,
        "wires": []
    },
    {
        "id": "752c6c28c7ae92ef",
        "type": "function",
        "z": "3d794c16765e7902",
        "name": "function 5",
        "func": "msg.payload = { \"tank\":global.get(\"water_level\")}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 520,
        "y": 560,
        "wires": [
            [
                "127958d67fe94006"
            ]
        ]
    },
    {
        "id": "f58d6e8545746097",
        "type": "mqtt-broker",
        "name": "",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "633ea3fdbad1a6f4",
        "type": "MySQLdatabase",
        "name": "",
        "host": "127.0.0.1",
        "port": "3306",
        "db": "db_project",
        "tz": "",
        "charset": "UTF8"
    },
    {
        "id": "54f06fbf65a33cdb",
        "type": "ui_group",
        "name": "Realtime Data",
        "tab": "0647979cdf08dedf",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "27631c4f948d2939",
        "type": "ui_group",
        "name": "System Control",
        "tab": "0647979cdf08dedf",
        "order": 2,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "0647979cdf08dedf",
        "type": "ui_tab",
        "name": "Automatic Plant Watering",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

```

those are the json expoorted from node-red, please tell me if i did something wrong exporting

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [21 January 2023 20:46 UTC](https://discourse.nodered.org/t/why-cant-i-call-globalvariable-in-another-function/74018/2 "2023-01-21T20:46:29Z")

</div>

Hi @rizkiaswangga, welcome to the forums.

Here is your problem from looking quickly :

`function4` is declaring a function

```javascript
function setGlobalVariables(msg) {
    context.set("water_level", msg.water_level);
    context.set("soil_level", msg.soil_level);
    context.set("weather_condition", msg.weather_condition);
    return msg;
}

```

But your not calling it, also I think it's slightly wrong.

it should be:

```javascript
function setGlobalVariables(msg) {
    global.set("water_level", msg.water_level);
    global.set("soil_level", msg.soil_level);
    global.set("weather_condition", msg.weather_condition);
}

setGlobalVariables(msg);
return msg

```

**EDIT**  
You should also attach your properties to `msg.payload` as opposed to the root of `msg`  
most nodes will expect `payload` to contain the juicy bits (including the http request node)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 March 2023 20:46 UTC](https://discourse.nodered.org/t/why-cant-i-call-globalvariable-in-another-function/74018/3 "2023-03-22T20:46:37Z")

</div>

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