# Influxdb query problem

**URL:** <https://discourse.nodered.org/t/influxdb-query-problem/73077>\
**Category:** General\
**Created:** [2 January 2023 18:38 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077 "2023-01-02T18:38:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [2 January 2023 18:38 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/1 "2023-01-02T18:38:07Z")

</div>

Is it possible to include a variable in the SELECT phrase retriving data from Influxdb?

Eg: SELECT xxx[varA] FROM yyy

If yes, what is the correct syntax?

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [2 January 2023 19:10 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/2 "2023-01-02T19:10:29Z")

</div>

Can you provide more info please ?

eg Are you talking about the **node-red-contrib-influxdb** node ?

If so what version of database is setup in the config node eg 1.x or 1.8 flux or 2.0

---

<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:** [2 January 2023 19:14 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/3 "2023-01-02T19:14:37Z")

</div>

Are you asking about what you should put in the query, or how to build the query? If the latter then show us an example of what you want the query to be, and where the variable part comes from.

---

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [2 January 2023 22:26 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/4 "2023-01-02T22:26:10Z")

</div>

This is about an energy display, NodeRed-Influxdb-Grafana. I am retriving the energy prices via the "Nordpool-node" which is inserting the prices in influx with the **node-red-contrib-influxdb** node. This is specified in a **Function node** where every hour get its own field key with a "general" timestamp for all field keys. This is actually working, I get new prices every midnight inserted in the database as desired.

I would then like to get back the price hour by hour with a **SELECT nphour[hr] FROM energyprice** where **nphour** is the field key and **hr** is the actual hour stated in another Function node. Here is obviosly the problem, with this hr-variable. I have tried a lot of "('[{-setups but nothing seems to work, I can not find any helpful info at the [docs.influxdata.com](http://docs.influxdata.com)

Any ideas would be so much appreciated!

This is done with version 1.8 installed one month ago.

---

<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:** [2 January 2023 22:42 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/5 "2023-01-02T22:42:04Z")

</div>

> [@Mrfox](#):
>
> SELECT nphour[hr] FROM energyprice

What are the actual key names? Always try and give real examples in cases like this, so there is no confusion about what you actually mean.

---

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [2 January 2023 22:56 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/6 "2023-01-02T22:56:18Z")

</div>

Key names in database is: **nphour00, nphour01... nphour23** , is the last one.

---

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [2 January 2023 23:57 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/7 "2023-01-02T23:57:12Z")

</div>

When I write eg **SELECT nphour14 FROM energyprice** I get the right value replied.

---

<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:** [3 January 2023 07:18 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/8 "2023-01-03T07:18:28Z")

</div>

Use a Template node to build the query to pass to the influx node. The template node help text shows how to do that

---

<div class="post-metadata">

**Author:** ![ScheepersJohan](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/scheepersjohan/32/39556_2.png) [@ScheepersJohan](https://discourse.nodered.org/u/ScheepersJohan)\
**Post date:** [3 January 2023 09:32 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/9 "2023-01-03T09:32:03Z")

</div>

> [@Mrfox](#):
>
> SELECT nphour14 FROM energyprice

You can do something like this, I don't know how the rest of your flow looks like but this should give you an idea of what to do.

```auto
[{"id":"abcdd40cf46442c7","type":"inject","z":"5924732647e43e8f","name":"11","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"11","payloadType":"num","x":390,"y":880,"wires":[["02e37dc5bb0c1f81"]]},{"id":"b3d35fe7e04bb93f","type":"inject","z":"5924732647e43e8f","name":"12","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"12","payloadType":"num","x":390,"y":920,"wires":[["02e37dc5bb0c1f81"]]},{"id":"70a3fabcb3f85a77","type":"inject","z":"5924732647e43e8f","name":"20","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":390,"y":980,"wires":[["02e37dc5bb0c1f81"]]},{"id":"02e37dc5bb0c1f81","type":"function","z":"5924732647e43e8f","name":"function 13","func":"\nmsg.query = \"SELECT nphour\" + msg.payload +\" FROM energyprice\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":940,"wires":[["4f7bd9d0738230c9"]]},{"id":"4f7bd9d0738230c9","type":"debug","z":"5924732647e43e8f","name":"debug 20","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":940,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Mrfox](https://avatars.discourse-cdn.com/v4/letter/m/f19dbf/32.png) [@Mrfox](https://discourse.nodered.org/u/Mrfox)\
**Post date:** [5 January 2023 22:06 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/10 "2023-01-05T22:06:20Z")

</div>

Finally I got it to work. Thank you so much for your assistance!!!

---

<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 2023 22:06 UTC](https://discourse.nodered.org/t/influxdb-query-problem/73077/11 "2023-01-19T22:06:26Z")

</div>

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