I'd like to build a dynamic query string to pass to InfluxDB node from an Inject node.
The standard string
SELECT sum("diff_rain_mm") AS "sum_diff_rain_mm" FROM "telegraf"."autogen"."emon_input" WHERE time > now() - 10m
works fine, but I'd like to make the time element dynamic such that I can query from midnight (I can't find a way to do this directly with the builtin InfluxDB functions)
I am thinking JSONata can probably help, but I cannot find how to build a string (the quotes are required).
[edit]
I could do it in a function node, but wondered if there is an easier way.
Grafana really is your friend here because you can easily create a query with point and click and then use the query inspector to see the InfluxDB query:
SELECT mean("value") FROM "one_week"."environment" WHERE ("type" = 'humidity') AND time >= now() - 1h GROUP BY time(1m), "location", "type" fill(linear)
Here is a query that Grafana gave when I asked it to show "Today":
SELECT mean("value") FROM "one_week"."environment" WHERE ("type" = 'humidity') AND time >= 1613779200000ms and time <= 1613865599999ms GROUP BY time(2m), "location", "type" fill(previous)
So you can see that it has dynamically added the ms values for midnight and now.
If what you don't know how to do is to determine the time to enter for midnight of today then if you take the current time in ms let now = new Date().getTime() and divide that by the number of msec in a day, truncate it to an integer and multiply it by the number of msec in a day again you will end up with the millisecond timestamp for the start of the day, GMT. If you need that in local time then adjust by your timezone offset.
Thanks, but I'm doing this in NR to create a sensor in HA so Grafana isn't really the right tool.
This is more about how do I build a string in JSONata into which I can insert a calculation to give midnight. I am expecting to create the string in the form of
"SELECT \"x\" FROM y WHERE X AND time " & $a_datecalc
I can do that bit (actually I'd use Modulo to do it) - it is the building of the resultant string that is currently defeating me.
Whilst this gives me 2 time periods, one is the previous full 24Hr data from now() - 1d to midnight (the 'time' is always the period start time), and the second is the data since midnight.
I didn't mean that you needed to switch to it wholesale
Just use it as a discovery tool for InfluxDB queries. It doesn't really take up that many resources. Though if you are short, simply install it and then leave it stopped.
I think the group by groups on whole intervals, if you see what I mean, so group by 1d groups on whole days. The query then gives two results, the first is for yesterday and the second is today, starting at midnight.
SELECT sum("diff_rain_mm") AS "sum_diff_rain_mm" FROM "telegraf"."autogen"."emon_input" WHERE time > now() - 1d GROUP BY time(1d) tz('America/Chicago')
Interestingly, I'm not sure that date string is ISO compliant. The Z on the end is not correct as there is a "+06:00". I just noticed there is no '+' so it is definitely not compliant!!!!
I think that must be added by the NR node. The query from the command line;
> SELECT sum("diff_rain_mm") AS "sum_diff_rain_mm" FROM "telegraf"."autogen"."emon_input" WHERE time > now() - 1d GROUP BY time(1d) tz('America/Chicago')
name: emon_input
time sum_diff_rain_mm
---- ----------------
1614146400000000000 0.8999999999999986
1614232800000000000 0.6000000000000014
> SELECT sum("diff_rain_mm") AS "sum_diff_rain_mm" FROM "telegraf"."autogen"."emon_input" WHERE time > now() - 1d GROUP BY time(1d)
name: emon_input
time sum_diff_rain_mm
---- ----------------
1614124800000000000 0.8999999999999986
1614211200000000000 0.6000000000000014
I think that one of my answers was for you to create a ms timestamp for midnight and append that to the time part of the query with a trailing ms text. I didn't write that in the answer directly but the format was in the example and you already had most of the JSONata.
I did try in it's most basic form and it didn't work. Not sure what I have just tried differently but I can get a string built now which works in a JSONata field.
"SELECT sum(\"diff_rain_mm\") AS \"sum_diff_rain_mm\" FROM \"telegraf\".\"autogen\".\"emon_input\" WHERE time > - 1d GROUP BY time(1d) tz('America\/Chicago')"
I have looked around and cannot find anything about building a bigger expression. How do I transfer the variable into the final string?
Is there a good resource for using the expression builder?