mblackstock / node-red-contrib-influxdb

Node-RED nodes to save and query data from an influxdb time series database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Changing Timezone in Query Doesn't Work

paulsalibe opened this issue · comments

I am trying to query data from my InfluxDB database in Node-Red to fill a table. Currently the format of the time is something like 2021-05-11T13:40:35.629Z in UTC time. I simply want to make this in my timezone (4 hours behind). So in my query I tried using the code SELECT Dept,Pounds,Category,time from digfeed where time > now() - 5h tz('America/New_York') but it still spits out UTC time. I'm not sure if I am using the tz() command wrong or what, but I really would like this to be a little more human readable for whatever timezone we need.

Thanks for the report, but from what I'm reading here, this isn't an issue with the InfluxDb node, but how you'd like your InfluxDb installation to handle time zones.

I think that querying InfluxDb using UTC is a good idea. I'd suggest that before you make the query, you convert it to UTC. When you get the results, convert the times into the timezone you'd like to display.

Sorry I am relatively new with all of this, but isn't that what I am currently doing? When I query it, it pulls it up in UTC. And then I am trying to convert to EST with the tz('America/New_York')?

It looks like you're querying data for the last 5 hours. I don't think its possible to have InfluxDb return the timestamps in a specified timezone based on this SO article. https://stackoverflow.com/questions/28160462/configuring-timezones-in-influxdb unless you've saved your data with the relevant timezone offset, but I could be wrong. I don't know how to do that offhand.

My suggestion is to convert the UTC timestamps returned after the query is returned, don't try to convert the timestamps to local time in the query as it looks like you're attempting.

To do that in Node-RED, you may want to add a function node after the query that loops through the results, generating a version of the time as a local time in a different format for display.

e.g. you could add a function node that adds a 'localtime' property to each element as a string.

for (element of msg.payload) {
    element.localtime = element.time.toLocaleString();
    console.log(element.localtime);
}
return msg;

All that said, I'm really not sure how or where you want to display the data. It might be best to leave the times as UTC in your messages and have your dashboard or other client code convert them to local time.

If you have any other questions on how to use the function node with Node-RED, or with your flow in general, I suggest try the Node-RED slack channel https://nodered.org/slack/, or https://discourse.nodered.org/ where the community can help you out.

Good luck!