# Calculate Average from Global Variables

**URL:** <https://discourse.nodered.org/t/calculate-average-from-global-variables/80537>\
**Category:** General\
**Created:** [13 August 2023 16:34 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537 "2023-08-13T16:34:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![richiep](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@richiep](https://discourse.nodered.org/u/richiep)\
**Post date:** [13 August 2023 16:34 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/1 "2023-08-13T16:34:43Z")

</div>

Hey All,  
I'm trying to calculate the average temp from 2 temperature sensors which are stored in global variables. Here's my flow, how can I set it up to calculate the average?  
Thanks!

```auto
[
    {
        "id": "5e73126742dd6de3",
        "type": "inject",
        "z": "bd5d8f0934d852df",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 720,
        "wires": [
            [
                "92eef5368866f343",
                "1e02e4439dfe79f2"
            ]
        ]
    },
    {
        "id": "92eef5368866f343",
        "type": "change",
        "z": "bd5d8f0934d852df",
        "name": "Hanging Lamp Temp",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "currentTempHangingLamp",
                "tot": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 500,
        "y": 680,
        "wires": [
            [
                "5b2168d7ab676831"
            ]
        ]
    },
    {
        "id": "1e02e4439dfe79f2",
        "type": "change",
        "z": "bd5d8f0934d852df",
        "name": "Bookshelf Temp",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "currentTempBookshelfTemp",
                "tot": "global",
                "dc": true
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 400,
        "y": 740,
        "wires": [
            [
                "5b2168d7ab676831"
            ]
        ]
    },
    {
        "id": "ba4501feab4a54cd",
        "type": "function",
        "z": "bd5d8f0934d852df",
        "name": "calculate average",
        "func": "var lamp = msg.payload[1]\nvar bookshelf = msg.payload[2]\n\nmsg.payload = ((lamp + bookshelf)/2);\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 910,
        "y": 720,
        "wires": [
            []
        ]
    },
    {
        "id": "8462518c70518a74",
        "type": "debug",
        "z": "bd5d8f0934d852df",
        "name": "debug 351",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1070,
        "y": 640,
        "wires": []
    },
    {
        "id": "5b2168d7ab676831",
        "type": "join",
        "z": "bd5d8f0934d852df",
        "name": "",
        "mode": "custom",
        "build": "array",
        "property": "payload",
        "propertyType": "msg",
        "key": "payload",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 710,
        "y": 720,
        "wires": [
            [
                "ba4501feab4a54cd",
                "8462518c70518a74"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 August 2023 16:48 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/2 "2023-08-13T16:48:17Z")

</div>

If you are using a `function` node, you don't need the two `change` nodes of the `join` node. Just get the global variables in the function node.

```auto
let lamp = global.get('currentTempHangingLamp');
let bookshelf = global.get('currentTempBookshelfTemp');

msg.payload = ((lamp + bookshelf)/2);
return msg;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 August 2023 16:49 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/3 "2023-08-13T16:49:10Z")

</div>

Arrays atart at 0 not 1.  
You do not need a join.  
Are the stored globals numbers?  
If not you would need to parse them.  
here is an example

```auto
[{"id":"5e73126742dd6de3","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"json","x":330,"y":120,"wires":[["1e02e4439dfe79f2"]]},{"id":"1e02e4439dfe79f2","type":"change","z":"b9860b4b9de8c8da","name":"global array","rules":[{"t":"set","p":"payload[0]","pt":"msg","to":"currentTempBookshelfTemp","tot":"global","dc":true},{"t":"set","p":"payload[1]","pt":"msg","to":"currentTempHangingLamp","tot":"global","dc":true}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":200,"wires":[["ba4501feab4a54cd","8462518c70518a74"]]},{"id":"ba4501feab4a54cd","type":"function","z":"b9860b4b9de8c8da","name":"calculate average","func":"let lamp = parseFloat(msg.payload[0]);\nlet bookshelf = parseFloat(msg.payload[1]);\n\nmsg.payload = ((lamp + bookshelf)/2);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":260,"wires":[["8462518c70518a74"]]},{"id":"8462518c70518a74","type":"debug","z":"b9860b4b9de8c8da","name":"debug 351","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":180,"wires":[]}]

```

Or all in a change node

```auto
[{"id":"5e73126742dd6de3","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[]","payloadType":"json","x":330,"y":140,"wires":[["1e02e4439dfe79f2"]]},{"id":"1e02e4439dfe79f2","type":"change","z":"b9860b4b9de8c8da","name":"global array","rules":[{"t":"set","p":"payload[0]","pt":"msg","to":"currentTempBookshelfTemp","tot":"global","dc":true},{"t":"set","p":"payload[1]","pt":"msg","to":"currentTempHangingLamp","tot":"global","dc":true},{"t":"set","p":"payload","pt":"msg","to":"$average($$.payload.$number($))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":220,"wires":[["8462518c70518a74"]]},{"id":"8462518c70518a74","type":"debug","z":"b9860b4b9de8c8da","name":"debug 351","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":200,"wires":[]}]

```

[edit]  
If you stored the globals in on object rather than separate variables you could do it even easier.  
save as  
`global.` `current.TempHangingLamp`  
`global.` `current.TempBokkshelfLamp`  
Then in a change node  
set `msg.` `payload`  
to `:J` `$average($globalContext("current").*)`  
This way if you add more temps to current object, you would not have to edit any code to still get an average.

---

<div class="post-metadata">

**Author:** ![richiep](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@richiep](https://discourse.nodered.org/u/richiep)\
**Post date:** [13 August 2023 18:10 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/4 "2023-08-13T18:10:55Z")

</div>

> [@E1cid](#):
>
> If you stored the globals in on object rather than separate variables you could do it even easier.  
> save as  
> `global.` `current.TempHangingLamp`  
> `global.` `current.TempBokkshelfLamp`  
> Then in a change node  
> set `msg.` `payload`  
> to `:J` `$average($globalContext("current").*)`  
> This way if you add more temps to current object, you would not have to edit any code to still get an average.

Took me a minute to set it up but this works! Thank you. Here it is for future readers of this thread.

```auto
[
    {
        "id": "7f2c0335c9b36ed4",
        "type": "mqtt in",
        "z": "f01c1351c0c2d98d",
        "name": "",
        "topic": "/apartment/bookshelf/temp",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "e8de652f3f0d5685",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 390,
        "y": 160,
        "wires": [
            [
                "523daf1b5c53cad6",
                "6b741b4f0f6d7037",
                "c8c17943408c22fa",
                "0b8499eda245d82d",
                "0ea4d60aafbfbf66"
            ]
        ]
    },
    {
        "id": "c8c17943408c22fa",
        "type": "change",
        "z": "f01c1351c0c2d98d",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "current.tempBookshelf",
                "pt": "global",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 760,
        "y": 60,
        "wires": [
            []
        ]
    },
    {
        "id": "e8de652f3f0d5685",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.154",
        "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": ""
    }
]

```

and the second variable

```auto
[
    {
        "id": "3dbb97be82245dad",
        "type": "mqtt in",
        "z": "7955d6419dd0e9b3",
        "name": "",
        "topic": "/apartment/ceiling/temp",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "68dfc99953924658",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 280,
        "y": 300,
        "wires": [
            [
                "e27cce5922bd1a7f",
                "6dec5b5f133b1d1f",
                "b16a0e9fcca20514"
            ]
        ]
    },
    {
        "id": "6dec5b5f133b1d1f",
        "type": "change",
        "z": "7955d6419dd0e9b3",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "current.tempApartmentCeiling",
                "pt": "global",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 640,
        "y": 260,
        "wires": [
            []
        ]
    },
    {
        "id": "68dfc99953924658",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.217",
        "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": ""
    }
]

```

Followed by bringing them together and calculating the average

```auto
[
    {
        "id": "5e73126742dd6de3",
        "type": "inject",
        "z": "bd5d8f0934d852df",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 220,
        "y": 680,
        "wires": [
            [
                "92eef5368866f343"
            ]
        ]
    },
    {
        "id": "92eef5368866f343",
        "type": "change",
        "z": "bd5d8f0934d852df",
        "name": "Calculate apartment temp average",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$average($globalContext(\"current\").*)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 460,
        "y": 680,
        "wires": [
            [
                "8462518c70518a74"
            ]
        ]
    },
    {
        "id": "8462518c70518a74",
        "type": "debug",
        "z": "bd5d8f0934d852df",
        "name": "debug 351",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 790,
        "y": 680,
        "wires": []
    }
]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 August 2023 18:27 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/5 "2023-08-13T18:27:57Z")

</div>

You could also break the object in to further sections for easily adding more info later, i.e store temp and say humidity or other readings.  
`global.current.temp.Hanginglamp`  
or  
`global.current.humidity.Hanginglamp`

you could then call  
`$globalContext("current").temp.*`  
to retrieve all temps.

---

<div class="post-metadata">

**Author:** ![richiep](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@richiep](https://discourse.nodered.org/u/richiep)\
**Post date:** [13 August 2023 18:30 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/6 "2023-08-13T18:30:37Z")

</div>

Ah! I'll use this for my next variable which is humidity.

Thx!

---

<div class="post-metadata">

**Author:** ![richiep](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@richiep](https://discourse.nodered.org/u/richiep)\
**Post date:** [13 August 2023 22:26 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/7 "2023-08-13T22:26:42Z")

</div>

ACK! this was working... I added another sensor to the mix and now I'm getting the following error

```auto
"Invalid JSONata expression: Argument 1 of function "average" must be an array of "numbers""

```

I deleted the new stuff and I'm still getting the error... Anyway here are my current nodes. I'm having trouble make sense of it.

```auto
[
    {
        "id": "df543ac168cbb6e1",
        "type": "change",
        "z": "bd5d8f0934d852df",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "current.tempBookshelf",
                "pt": "global",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 680,
        "wires": [
            []
        ]
    },
    {
        "id": "ea09359d10b0fc36",
        "type": "change",
        "z": "bd5d8f0934d852df",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "current.tempHangingLamp",
                "pt": "global",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 490,
        "y": 720,
        "wires": [
            []
        ]
    },
    {
        "id": "17de199bf8f942aa",
        "type": "change",
        "z": "bd5d8f0934d852df",
        "name": "Calculate apartment temp average",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "$average($globalContext(\"current\").*)",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 480,
        "y": 760,
        "wires": [
            [
                "ef931523cdca7dae"
            ]
        ]
    }
]

```

Edit I added a JSON node to create an object. When I look at the result in the terminal it shows as a number. Still the same error

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 August 2023 06:16 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/8 "2023-08-14T06:16:38Z")

</div>

What does the global current look like when you add the new temp?

---

<div class="post-metadata">

**Author:** ![richiep](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@richiep](https://discourse.nodered.org/u/richiep)\
**Post date:** [14 August 2023 16:40 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/9 "2023-08-14T16:40:07Z")

</div>

I kept messing around and wound up using the solution from zenofmud.

Thanks E1cid!

---

<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:** [28 August 2023 16:40 UTC](https://discourse.nodered.org/t/calculate-average-from-global-variables/80537/10 "2023-08-28T16:40:10Z")

</div>

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