Assistance getting correct data sent to influxdb

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 (51.1 KB)

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 ```)

``` 
   code goes here 
```

See this post for more details - How to share code or flow json

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.

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

[
    {
        "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
    }
]

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.

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

[
    {
        "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"
            ]
        ]
    }
]

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...

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

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