MQTT to InfluxDB/Grafana -string value

Hi.

I have a flow that connects to broker and brings in values via MQTT. All the values except 1 are numbers (parsed JSON object). One is a state (string) that says, stand-by, discharged, chargind. For some reason, InfluxDB is not liking this. Do I need to confgure the influx node in NodeRed before sending it to the influxDB node? It correct value (word) is displayed in the debug pane after the function node.

The error im getting in influxDB is " unsupported input type for mean aggregate: string"

Here are two flows: one is showing RenogyBattTemp (a number), the other is RenogyBattState ( a word). Its the latter that I'm having trouble bringing into InfluxDB.

[
    {
        "id": "a2dfcb43bcf6b83c",
        "type": "mqtt in",
        "z": "51a740fcee7d2bf2",
        "name": "Renogy Battery Temperature",
        "topic": "solar/renogy_battery_1/temperature/state",
        "qos": "2",
        "datatype": "json",
        "broker": "607f4bb0a0ae6958",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 1320,
        "y": 280,
        "wires": [
            [
                "81966104e50ed507"
            ]
        ]
    },
    {
        "id": "81966104e50ed507",
        "type": "function",
        "z": "51a740fcee7d2bf2",
        "name": "",
        "func": "msg.payload = {\n    RenogyBattTemp: msg.payload\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1520,
        "y": 280,
        "wires": [
            [
                "69d1bb968ae31db3",
                "b9dabd5ea9e5b78f"
            ]
        ]
    },
    {
        "id": "69d1bb968ae31db3",
        "type": "influxdb out",
        "z": "51a740fcee7d2bf2",
        "influxdb": "8906a2bc97d7619b",
        "name": "Influx",
        "measurement": "SolarRegulator",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "Home",
        "bucket": "AntonioBucket",
        "x": 1690,
        "y": 280,
        "wires": []
    },
    {
        "id": "faec16862dd6a6a2",
        "type": "mqtt in",
        "z": "51a740fcee7d2bf2",
        "name": "Renogy Battery State",
        "topic": "solar/renogy_battery_1/state/state",
        "qos": "2",
        "datatype": "utf8",
        "broker": "607f4bb0a0ae6958",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 1300,
        "y": 340,
        "wires": [
            [
                "6f6d139f8a0f235e"
            ]
        ]
    },
    {
        "id": "6f6d139f8a0f235e",
        "type": "function",
        "z": "51a740fcee7d2bf2",
        "name": "",
        "func": "msg.payload = {\n    RenogyBattState: msg.payload\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1500,
        "y": 340,
        "wires": [
            [
                "87f5417001aa3137",
                "b9dabd5ea9e5b78f"
            ]
        ]
    },
    {
        "id": "87f5417001aa3137",
        "type": "influxdb out",
        "z": "51a740fcee7d2bf2",
        "influxdb": "8906a2bc97d7619b",
        "name": "Influx",
        "measurement": "SolarRegulator",
        "precision": "",
        "retentionPolicy": "",
        "database": "database",
        "precisionV18FluxV20": "ms",
        "retentionPolicyV18Flux": "",
        "org": "Home",
        "bucket": "AntonioBucket",
        "x": 1690,
        "y": 340,
        "wires": []
    },
    {
        "id": "607f4bb0a0ae6958",
        "type": "mqtt-broker",
        "name": "",
        "broker": "192.168.1.139",
        "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": {},
        "sessionExpiry": ""
    },
    {
        "id": "8906a2bc97d7619b",
        "type": "influxdb",
        "hostname": "127.0.0.1",
        "port": "8086",
        "protocol": "http",
        "database": "database",
        "name": "",
        "usetls": false,
        "tls": "",
        "influxdbVersion": "2.0",
        "url": "http://192.168.1.139:8086",
        "rejectUnauthorized": true
    }
]```

Please feed the data you are passing to influx into a debug node and show us what is there.
Where are you seeing that error? It sounds more like an error you would get when querying the database rather than writing to it. Usually it means that you have specified mean in a query where the thing you are trying to query is a string rather than a number. Obviously it is not possible to take the mean of string values.

here's the data that is going into the InfluxDB node:

{ RenogyBattState: "discharging" }

3/11/2022, 1:34:20 PM[node: b9dabd5ea9e5b78f](http://192.168.1.143:1880/#)solar/regulator/input_current/state : msg.payload : Object

The error is on the InfluxDB side (in the actualy program, InfluxDB). Hence I don't now if this is an InfluxDB configuration issue or a Node-Red configuration issue.

Generally InfluxDB only likes numbers as values, strings would normally be saved as a tag.

1 Like

That's what I thought.

I'll probably just bypass influxdb and send it directly to the grafana dashboard via the Mqtt-datasource plugin ....but thats another forum!

It depends on what the purpose is. There is nothing wrong with storing state as strings. It will not give that error.

I am absolutely conviced that the error is coming when something tries to query the database and is attempting to take a mean. For example, are you trying to plot it on a chart in Grafana? Obviously you cannot chart strings.
If it isn't that then exactly where and when do you see the error?

@Colin you're right it was because of the mean setting in InfluxDB that it gave that error, see when I change from mean to last it does report the value as zero (but no error).

Now, to actually send the payload value (string) state, I guess I have to figure that out in Grafana as Grafana is just showing a 0 as this value.

You can, doesn't mean you should. :slight_smile:

Which is exactly why it is best to keep values as numbers and use tags for strings.

If you want to plot the value then you will have to store a number representing the state, rather than the state itself. If you just want to show the value in a text box (I forget what they are called in grafana) then it will be fine as a string.

1 Like

thanks, I have some homework to do!

:slight_smile:
You will find some guidance buried elsewhere in the forum. I know I'm not the only one who has posted recommendations about how to structure data for InfluxDB.

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