# Assistance getting correct data sent to influxdb

**URL:** <https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011>\
**Category:** General\
**Tags:** database, function-node\
**Created:** [19 November 2023 15:41 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011 "2023-11-19T15:41:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Automata](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@Automata](https://discourse.nodered.org/u/Automata)\
**Post date:** [19 November 2023 15:41 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011/1 "2023-11-19T15:41:58Z")

</div>

Good morning all, looking for some assistance. ive managed to set my global variables, though incorrectly. i dont want the thermostat itself to send info to influx, but rather the data BEFORE it get to the stat, so ive essentially doubled my global variables unintentionallly. Im only needing the ones that start with Caps to get to influx, Mode and HvacState are still missing from global and no data populates in InfluxDB although i can see my variables i set . i have more i would like to send in the future, so this is a good starting point. FLow attached and any advice or help appreciated.  
[flows (1).json](https://discourse.nodered.org/uploads/short-url/anXCyqawvMx68Ue6wdSsIHe0MZG.json) (51.1 KB)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2023 15:59 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011/2 "2023-11-19T15:59:42Z")

</div>

In future please post flows inline. In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

Also please just post enough of the flow that we can see the problem.

I have looked briefly at your flow, however. I don't understand why you are putting the data into global variables, if you look at the help text for the influxdb node you will see that it expects the data to be in msg.payload.

---

<div class="post-metadata">

**Author:** ![Automata](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@Automata](https://discourse.nodered.org/u/Automata)\
**Post date:** [19 November 2023 16:14 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011/3 "2023-11-19T16:14:08Z")

</div>

Hello, im setting them as global variable because they will be used later in the flow for logic.

```auto
[
    {
        "id": "5da4dce8f26e8141",
        "type": "influxdb out",
        "z": "1c014c4fab0f2d54",
        "influxdb": "9adf24b17054de54",
        "name": "Cloud",
        "measurement": "status",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "s",
        "retentionPolicyV18Flux": "",
        "org": "Automata",
        "bucket": "Remote Monitoring",
        "x": 2130,
        "y": 560,
        "wires": []
    },
    {
        "id": "7abab74d481c6426",
        "type": "function",
        "z": "1c014c4fab0f2d54",
        "name": "Transfer Data",
        "func": "msg.topic = \"InfluxData\";\nvar ambientTemp = global.get('ghostThermostat.ambient_temperature');\nvar targetTemp = global.get('ghostThermostat.target_temperature');\nvar hvacState = global.get('ghostThermostat.switch_state');\nvar occupied = global.get('ghostThermostat.away');\nvar mode = global.get('ghostThermostat.mode');\nvar outdoorAirTemp = global.get('outdoorAirTemp');\nmsg.payload = {\n \"Occupied\": occupied,\n \"HvacState\": hvacState,\n \"TargetTemp\": targetTemp,\n \"AmbientTemp\": ambientTemp,\n \"Mode\": mode,\n \"OutdoorAirTemp\": outdoorAirTemp\n}\n\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1680,
        "y": 560,
        "wires": [
            [
                "95a4190f7f283a65",
                "b19f98e69aa4d88b"
            ]
        ]
    },
    {
        "id": "95a4190f7f283a65",
        "type": "debug",
        "z": "1c014c4fab0f2d54",
        "name": "debug 55",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 2080,
        "y": 600,
        "wires": []
    },
    {
        "id": "e70c6cb9ee35668a",
        "type": "function",
        "z": "1c014c4fab0f2d54",
        "name": "Data 2",
        "func": "if (msg.topic == \"outdoorAirTemp\") {\n global.set(\"outdoorAirTemp\", msg.payload);\nreturn msg;\n}\nmsg.topic = \"outdoorAirTemp\";\nvar data = {\n 'outdoorAirtemp': global.get('outdoorAirTemp') \n}\nmsg.payload = data;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1150,
        "y": 680,
        "wires": [
            [
                "7abab74d481c6426"
            ]
        ]
    },
    {
        "id": "b19f98e69aa4d88b",
        "type": "change",
        "z": "1c014c4fab0f2d54",
        "name": "",
        "rules": [
            {
                "t": "move",
                "p": "payload.AmbientTemp",
                "pt": "msg",
                "to": "AmbientTemp",
                "tot": "global"
            },
            {
                "t": "move",
                "p": "payload.Occupied",
                "pt": "msg",
                "to": "Occupied",
                "tot": "global"
            },
            {
                "t": "move",
                "p": "payload.TargetTemp",
                "pt": "msg",
                "to": "TargetTemp",
                "tot": "global"
            },
            {
                "t": "move",
                "p": "payload.switch_state",
                "pt": "msg",
                "to": "HvacState",
                "tot": "global"
            },
            {
                "t": "move",
                "p": "payload.mode",
                "pt": "msg",
                "to": "Mode",
                "tot": "global"
            },
            {
                "t": "move",
                "p": "payload.outdoorAIrTemp",
                "pt": "msg",
                "to": "OutdoorAirTemp",
                "tot": "global"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1920,
        "y": 560,
        "wires": [
            [
                "5da4dce8f26e8141"
            ]
        ]
    },
    {
        "id": "9adf24b17054de54",
        "type": "influxdb",
        "hostname": "127.0.0.1",
        "port": "8086",
        "protocol": "http",
        "database": "database",
        "name": "Cloud",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "2.0",
        "url": "https://us-east-1-1.aws.cloud2.influxdata.com/",
        "rejectUnauthorized": true
    }
]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 November 2023 20:24 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011/4 "2023-11-19T20:24:24Z")

</div>

By using Move in the Change node you are removing the data from msg.payload, so the data is not going to the Influx node. If you use Set global.xxx to msg.payload.xxx then it will leave the values in the payload.

Note though, that wherever practical it is best to avoid global data. Instead, feed the message containing the data to the nodes that need it.

---

<div class="post-metadata">

**Author:** ![Automata](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@Automata](https://discourse.nodered.org/u/Automata)\
**Post date:** [19 November 2023 22:19 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011/5 "2023-11-19T22:19:14Z")

</div>

I have since changed, and still having the issue. attached is both of the data functions, then the influx formatting function, finally the influxdb. my error is :TypeError: msg.payload.forEach is not a function

```auto
[
    {
        "id": "b0656f55c8f92ee4",
        "type": "function",
        "z": "bbe2372dbf2ff77c",
        "name": "Data",
        "func": "\nif (msg.topic == \"ambient_temperature\") {\n global.set(\"ghostThermostat.ambient_temperature\", msg.payload);\n}\nif (msg.topic == \"target_temperature\") {\n global.set(\"ghostThermostat.target_temperature\", msg.payload);\n}\nif (msg.topic == \"hvac_state\") {\n global.set(\"ghostThermostat.hvac_state\", msg.payload);\n}\nif (msg.topic == \"away\") {\n global.set(\"ghostThermostat.away\", msg.payload);\n}\nmsg.topic = \"update\";\nvar data = {\n 'ambient_temperature': global.get('ghostThermostat.ambient_temperature') || 72,\n 'target_temperature': global.get('ghostThermostat.target_temperature') || 72,\n 'hvac_state': global.get('ghostThermostat.hvac,state') || 'off',\n 'away': global.get('ghostThermostat.away') || 'false'\n}\nmsg.payload = data;\nreturn msg;",
        "outputs": 1,
        "timeout": "",
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1290,
        "y": 420,
        "wires": [
            [
                "80722bc65ec10c88",
                "1a36d393d6c0ecad",
                "b1f23cdb62e5ec7b"
            ]
        ]
    },
    {
        "id": "b1f23cdb62e5ec7b",
        "type": "function",
        "z": "bbe2372dbf2ff77c",
        "name": "Transfer Data",
        "func": "msg.topic = \"InfluxData\";\nvar ambientTemp = global.get('ghostThermostat.ambient_temperature');\nvar targetTemp = global.get('ghostThermostat.target_temperature');\nvar hvacState = global.get('ghostThermostat.switch_state');\nvar occupied = global.get('ghostThermostat.away');\nvar mode = global.get('ghostThermostat.mode');\nvar outdoorAirTemp = global.get('OutdoorAirTemp');\nmsg.payload = {\n \"Occupied\": occupied,\n \"HvacState\": hvacState,\n \"TargetTemp\": targetTemp,\n \"AmbientTemp\": ambientTemp,\n \"Mode\": mode,\n \"OutdoorAirTemp\": outdoorAirTemp\n}\n\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1500,
        "y": 420,
        "wires": [
            [
                "c82fb99e35817d92",
                "d020d6b12981f2b1"
            ]
        ]
    },
    {
        "id": "d020d6b12981f2b1",
        "type": "function",
        "z": "bbe2372dbf2ff77c",
        "name": "function 2",
        "func": "msg.topic = \"InfluxData\";\n\nvar ambientTemp = global.get('ghostThermostat.ambient_temperature');\nvar targetTemp = global.get('ghostThermostat.target_temperature');\nvar hvacState = global.get('ghostThermostat.switch_state');\nvar occupied = global.get('ghostThermostat.away');\nvar mode = global.get('ghostThermostat.mode');\nvar outdoorAirTemp = global.get('outdoorAirTemp');\n\n// Format data in InfluxDB line protocol\nvar influxData = `thermostat_data,location=your_location Occupied=${occupied},HvacState=\"${hvacState}\",TargetTemp=${targetTemp},AmbientTemp=${ambientTemp},Mode=\"${mode}\",OutdoorAirTemp=${outdoorAirTemp}`;\n\n// Set the formatted data as payload\nmsg.payload = influxData;\n\nreturn msg;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1700,
        "y": 420,
        "wires": [
            [
                "251c8387fd125c72"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [20 November 2023 07:12 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011/6 "2023-11-20T07:12:23Z")

</div>

That's what I get when pushing a random value (as `ambient_temperature`) into your functions:

`thermostat_data,location=your_location Occupied=undefined,HvacState="undefined",TargetTemp=undefined,AmbientTemp=1700462871586,Mode="undefined",OutdoorAirTemp=undefined`

Might not be the cause of the error you get, but I'm convinced, `"undefined"` is not what you intend to send into the db...

> [@Automata](#):
>
> :TypeError: msg.payload.forEach

The nodes you provided do not generate this error - sth else does. Looks like this "other node" expects an array as `msg.payload`...

---

<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:** [19 January 2024 07:12 UTC](https://discourse.nodered.org/t/assistance-getting-correct-data-sent-to-influxdb/83011/7 "2024-01-19T07:12:31Z")

</div>

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