# Reading data from influx with an index on mean

**URL:** <https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351>\
**Category:** General\
**Created:** [17 December 2022 12:40 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351 "2022-12-17T12:40:10Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 09:56 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/21 "2022-12-18T09:56:03Z")

</div>

@E1cid I have tried your solution:

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

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f653bb43b05b1fbaf7b71d244ee66ec66b8f7d38.png)

---

<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:** [18 December 2022 09:58 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/22 "2022-12-18T09:58:41Z")

</div>

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

```auto
[
    {
        "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.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 10:08 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/23 "2022-12-18T10:08:21Z")

</div>

@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:

 ![afbeelding](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f42d1a097d0b89abaffc37dcb95669a64c66d99d.png)

---

<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:** [18 December 2022 10:10 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/24 "2022-12-18T10:10:07Z")

</div>

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.

---

<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:** [18 December 2022 10:26 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/25 "2022-12-18T10:26:21Z")

</div>

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](https://nodered.org/docs/user-guide/messages)) 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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 11:16 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/26 "2022-12-18T11:16:25Z")

</div>

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

```auto
[{"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!!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 December 2022 11:25 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/27 "2022-12-18T11:25:14Z")

</div>

> [@henkkas](#):
>
> Maybe I should better use ?? instead of || everywhere I use this?

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.

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 12:12 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/28 "2022-12-18T12:12:32Z")

</div>

> [@TotallyInformation](#):
>
> `||` doesn't work if the value of the variable you are working with is Boolean for example but `??` does work.

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

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 14:04 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/29 "2022-12-18T14:04:47Z")

</div>

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

---

<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:** [18 December 2022 14:11 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/30 "2022-12-18T14:11:17Z")

</div>

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?](https://www.freecodecamp.org/news/var-let-and-const-whats-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] = [];`

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 14:36 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/31 "2022-12-18T14:36:07Z")

</div>

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

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

```auto
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?

---

<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:** [18 December 2022 14:38 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/32 "2022-12-18T14:38:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 14:42 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/33 "2022-12-18T14:42:49Z")

</div>

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

---

<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:** [18 December 2022 14:48 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/34 "2022-12-18T14:48:50Z")

</div>

> [@henkkas](#):
>
> I had to round them for the chart node

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

---

<div class="post-metadata">

**Author:** ![henkkas](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/henkkas/32/39844_2.png) [@henkkas](https://discourse.nodered.org/u/henkkas)\
**Post date:** [18 December 2022 14:53 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/35 "2022-12-18T14:53:17Z")

</div>

> [@Colin](#):
>
> 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.

---

<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:** [18 December 2022 15:06 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/36 "2022-12-18T15:06:28Z")

</div>

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

---

<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:** [1 January 2023 15:07 UTC](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351/37 "2023-01-01T15:07:11Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/reading-data-from-influx-with-an-index-on-mean/72351.md?page=1)
