I am trying to get data from a table in influx database with a query. Query is passed through influxDB in node. In the query I use WHERE condition with a variable absolute time range which can be manipulated.
I get an error from influxDB:
"Error from InfluxDB: invalid operation: time and *influxql.VarRef are not compatible"
Time format in the var. tstart and tend is: "2021-10-30T14:50:30.833Z"of
(I also tried with number of msec.)
The influxDB in node is set on msec.
In the CLI this selection is working correct.
Maybe it has something to do with single and double quotes?
CLI doesn't work if I change selection where dates are double quotes.
Datetime shows double quotes in debug window.
Anybody can hint me in the right direction?
// Function prepare chart data
if ((topic == "back") || (topic == "forward") || (topic == "periode")) {
let tstart = context.get("chart.starttime");
let tend = context.get("chart.endtime");
let sel = "SELECT time, selfuse, totalsolar FROM power ";
let q = sel + "WHERE time >= tstart AND time <= tend";
msg.query = q;
//node.warn("tstart = " + tstart);
//node.warn("tend = " + tend);
}
Feed the function into a debug node set to Output Full Message and check what is in msg.topic. If you cannot see what is wrong with it then post the debug output here.
@E1cid , I changed the query as you suggested, it made sense to me, also added a space before AND. But not quite the solution it seems. I got next error in the debug window:
Error: A 400 Bad Request error occurred: {"error":"error parsing query: found Date, expected ; at line 1, char 67"}
@Colin and @E1cid , sorry I still had some expirements active, hence the invalid dates,
I now seem to have a valid query, need to solve some more time calculations, though.
Thanks you all for help.
// Query maken op de influx database
if ((topic == "back") || (topic == "forward") || (topic == "periode")) {
let tstart = "2021-10-31T13:29:52.738Z";
let tend = "2021-10-31T16:29:52.738Z";
// let tstart = context.get("chart.starttime");
// let tend = context.get("chart.endtime");
//node.warn("tstart = " + tstart);
//node.warn("tend = " + tend);
let sel = "SELECT time, selfuse, totalsolar FROM power ";
let q = sel + "WHERE time >= '" + tstart + "' AND time <= '" + tend + "'";
msg.query = q;
msg.trenddate = "Hallo";
}
return msg;
The resulting query on influxDB is
SELECT time, selfuse, totalsolar FROM power WHERE time >= '2021-10-31T13:29:52.738Z' AND time <= '2021-10-31T16:29:52.738Z'
That's where I was looking for, thanks you guys.
Although, I must say, it's a bit confusing with the quotes.....pfff...
@Colin , I am sorry. I correct myself. I only took part of the query.
After complete copy of statement it does work.
Now I have to learn about string literals....and the new kind of quote