Build dynamic query string for InfluxDB

I've decided that in this case it is better suited to using a Function node to build the correct query string.

Thanks for all your help.

Is this what you are trying to do?

[{"id":"f7518725.54f5f8","type":"template","z":"37f5b818.697af8","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"SELECT sum(\"diff_rain_mm\") AS \"sum_diff_rain_mm\" FROM \"telegraf\".\"autogen\".\"emon_input\" WHERE time > {{payload}} - 1d GROUP BY time(1d) tz('America\\/Chicago')","output":"str","x":260,"y":1340,"wires":[["65683d57.e14f5c"]]},{"id":"d0b40bda.bbc7e8","type":"inject","z":"37f5b818.697af8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":1340,"wires":[["f7518725.54f5f8"]]},{"id":"65683d57.e14f5c","type":"debug","z":"37f5b818.697af8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":410,"y":1340,"wires":[]}]

Yes, sort of. I think I am going to calculate the actual timespan in a function node. I thought using some of the builtin features would be easier, but I think it is more complicated :slight_smile:

Thanks for your help.

Well if you are using a function node to calculate the timespan then build the query there too. I find when building strings with embedded quotes it is much easier to use the string interpolation syntax, so you might have something like

let msg.query = `SELECT sum("diff_rain_mm") AS "sum_diff_rain_mm" FROM "telegraf"."autogen"."emon_input" WHERE time > ${msg.payload} - 1d GROUP BY time(1d) tz('America\/Chicago') `
1 Like

Thanks for the tip :grinning:

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