Reading data from influx with an index on mean

@E1cid I have tried your solution:

The array structure is OK, but the "y"-data is undefined.

please supply a sample flow with an inject node with sample data you used.
As this is the result i get with data i used in my example

[
    {
        "seires": [
            "mean",
            "mean_1",
            "mean_2"
        ],
        "data": [
            [
                {
                    "x": 1504029632890,
                    "y": 2
                },
                {
                    "x": 1504022335690,
                    "y": 2
                },
                {
                    "x": 1504022335690,
                    "y": 2
                }
            ],
            [
                {
                    "x": 1504029632890,
                    "y": 6
                },
                {
                    "x": 1504022335690,
                    "y": 6
                },
                {
                    "x": 1504022335690,
                    "y": 6
                }
            ],
            [
                {
                    "x": 1504029632890,
                    "y": 3
                },
                {
                    "x": 1504022335690,
                    "y": 3
                },
                {
                    "x": 1504022335690,
                    "y": 3
                }
            ]
        ],
        "labels": [
            "mean",
            "mean_1",
            "mean_2"
        ]
    }
]

I used the data you supplied in post 16.

@E1cid That is the output data I want to create to the chart node. The input I get from the influxDB request node, looks like this:

Please supply what i requested, as an image tells me nothing about your function and i can not copy an image for values to test.

I do not require all 300 plus elements you can just supply a few for testing purposes.

To do that, hover over payload in the debug node and some little buttons popup. Click Copy Value and paste it here, using the </> button at the top of the forum entry window when pasting. Do change the query first though, so it only returns a few values.

This canned reply may be useful for you:

Thereā€™s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

@E1cid I must stay corrected.

I implemented your solution in my project, but made a minor mistake. That's why I didn't got values.
It took me a while, I recreated my original fixed solution and your dynamic solution, and every time the same output. So there must have been a mistake that I made, but I found it after all, although it took me a while.

I tested with my chart node and works greatly.

Thank you very much.

also here by the revised test flow you send me, with fixed and dynamic solution.

[{"id":"6c1ddc2ef2fdc716","type":"inject","z":"cfd6ed84a742a9a1","name":"data","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":630,"y":2340,"wires":[["a14b390e01a05b6f"]]},{"id":"535b6ab94712f30a","type":"function","z":"cfd6ed84a742a9a1","name":"E1cid","func":"const series = Object.keys(msg.payload[0]) ?? [];\nseries.splice(series.indexOf(\"time\"),1);\nconst data = [];\nseries.forEach((prop, index) =>{\n    data[index] = [];\n    msg.payload.forEach(obj => {\n        data[index].push({x: obj.time, y: obj[prop]})\n    })\n})\n\nmsg.payload = [\n    {\n        series: [\"S1\",\"S2\", \"S3\"],\n        data: data,\n        labels: [\"\"]\n    }\n]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1010,"y":2380,"wires":[["7462b8b0fcd05535"]]},{"id":"7462b8b0fcd05535","type":"debug","z":"cfd6ed84a742a9a1","name":"E1cid","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1190,"y":2380,"wires":[]},{"id":"a14b390e01a05b6f","type":"function","z":"cfd6ed84a742a9a1","name":"influxdb","func":"msg.payload = [\n    {\n        time: 1504029632890,\n        mean: 2,\n        mean_1: 6,\n        mean_2: 3\n    },\n    {\n        time: 1504022335690,\n        mean: 2,\n        mean_1: 6,\n        mean_2: 3\n    },\n    {\n        time: 1504022335690,\n        mean: 2,\n        mean_1: 6,\n        mean_2: 3\n    },\n    {\n        time: 1504022335690,\n        mean: 2,\n        mean_1: 6,\n        mean_2: 3\n    },\n    {\n        time: 1504022335690,\n        mean: 2,\n        mean_1: 6,\n        mean_2: 3\n    }\n];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":820,"y":2340,"wires":[["535b6ab94712f30a","cd120b9075ad4aca"]]},{"id":"cd120b9075ad4aca","type":"function","z":"cfd6ed84a742a9a1","name":"Henkkas fixed solution","func":"let data0 = [];\nlet data1 = [];\nlet data2 = [];\n\nfor (let i = 0; i < msg.payload.length; i++) {\n    let thetime = Number(msg.payload[i].time);\n    let signal_1 = msg.payload[i].mean;\n    let signal_2 = msg.payload[i].mean_1;\n    let signal_3 = msg.payload[i].mean_2;\n    data0.push({ x: thetime, y: signal_1 });\n    data1.push({ x: thetime, y: signal_2 });\n    data2.push({ x: thetime, y: signal_3 });\n}\n\nlet series = [\"S1\", \"S2\", \"S3\"]\nlet labels = [\"\"];\nlet data = [data0, data1, data2];\n\nmsg.payload = [{ \"series\": series, \"data\": data, \"labels\": labels }];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1060,"y":2300,"wires":[["8be56d1a7e8b7848"]]},{"id":"8be56d1a7e8b7848","type":"debug","z":"cfd6ed84a742a9a1","name":"Henkkas","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1260,"y":2300,"wires":[]}]

@Colin I already know those tricks, I use them many times, Colin. Mostly for path. Thank you.

Also @TotallyInformation , thanks for the help.

I learned a lot from you guys again, today and yesterday!!

Yes, it was a feature introduced in node.js v14 and most modern browsers should also now support it. I've just started converting some of my uibuilder code to use it. || doesn't work if the value of the variable you are working with is Boolean for example but ?? does work. Basically the right hand component is applied if the left hand is null or undefined only.

@TotallyInformation Indeed, I have sometimes struggled with boolean, now that you say it. I will replace them now also. Thanks for your advice.

@E1cid I have some more questions about this subject, if you don't mind.

Why are you using const and not let.

Why doesn't my solution work, I mean the statement
data[i][j-1].push(obj);

const is used as the variable series is an array and always will be in the code i provided. If it were to be set to a string or other type later then i would use let. Var, Let, and Const ā€“ What's the Difference?

i would need to see more of your function and errors to be sure, but your code probably is trying to use data[i], but data[i] has not been initialised as an array. I do this in my loop with data[index] = [];

@E1cid Ok, I understand now about var. let and const. I already was told to use let instead of var.
I my case I have changed data to let instead of const, because I had to re-declare data[ ] later in my function.

Now I just encountered another problem. My data are floating point and I had to round them for the chart node. So this is your solution that is working now.

const influxObjects = Object.keys(msg.payload[0]) ?? [];
influxObjects.splice(influxObjects.indexOf("time"), 1);
let data = [];
influxObjects.forEach((element, index) => {
    data[index] = [];
    msg.payload.forEach(obj => {
        data[index].push({ x: obj.time, y: obj[element] })
    })
})

So in order to round the values. I thought I could change element like this.

const influxObjects = Object.keys(msg.payload[0]) ?? [];
influxObjects.splice(influxObjects.indexOf("time"), 1);
let data = [];
influxObjects.forEach((element, index) => {
    data[index] = [];
    msg.payload.forEach(obj => {
        data[index].push({ x: obj.time, y: obj[Math.round(element)] })
    })
})

But that didn't really work. My Y-values are now all undefined
Is there a way that it will work?

Math.round(obj[element])
element is a string and is used to select which object property to use.

@E1cid Yes, I can see that now. I am sorry. Still learning ...:slight_smile:

The chart node does not need the data to be rounded.

@Colin Not really, indeed. But if you hoover over the lines, you get the legend of the signals with values with 7 decimals. That is not very readable.

Ok, so it is you that needed it rounded, not the chart node :slight_smile:

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