Write to InfluxDB in rule with custom timestamp

(Openhab 3.3 with InfluxDB (v1.8.10))

I have an item which represents the power consumption yesterday. Because of that, the time stamp in InfluxDB is not correct. (E.g., the power consumption of July 16 has the timestamp July 17, 2am). Since I didn’t find a way to move the data in a chart page by one day, I want to create an item representing the power consumption with a corrected date.

Here an example. On July 17 at 2am openhab receives the value “power consumption yesterday”. This value should be stored to the item “power consumption corrected” in InfluxDB with the time stamp July 16, 12 o’clock noon.
My approach is to do this daily by a rule. But I didn’t find a way to write data to influxdb with a specific date and time by a rule. I know this can be done by command line in the influx console with the “insert” command, but I didn’t find a way to do this in an openhab rule. Is there a possibility to do this?

Have you tried dbQuery binding?

Have a look to the REST API

If you are able to run the command manually on command line it also should be possible to use the influxdb client via executeCommandLine action.

Thanks a lot for your help!
@glhopital: That looks very interesting, but it seems it is only working with InfluxDB v2.0 and not v1.8 which I’m still using.
@Wolfgang_S: Thanks for the hint. I figured out how to write value and time stamp by using the curl command. I can do that by e.g. curl -i -XPOST 'http://localhost:8086/write?u=USER&p=PASSWORD&db=DATABASE' --data-binary 'item_corrected value=10 1658163599945000000'.

But I’m still struggling how to do that in the openhab rule. I assumed best way is to use the http binding. But I didn’t manage to pass the --data-binary correctly.

I tried the following syntax but it is not working.
val output = sendHttpPostRequest('http://localhost:8086/write?u=USER&p=PASSWORD&db=DATABASE',"application/octet-string", 'item_corrected value=10 1658163599945000000', 8000)

Any hint where I’m wrong?

In case you would go to executeCommandLine together with curl this Can't get InfluxDB data to Grafana when seetting my own timestamp - #2 by kosken post shows a possible implementation for that call ( just focus on the call to the curl function ).

With regard to the http binding I am not that well experienced :slight_smile: . …
But what is the content of ( val ) output ? Shouldn’t that contain information about the error in case somethign is wrong ? Does it report success or a problem ? Is there any information in the influxdb log files that could help ?

Great thanks. That works, here my final command:
val input = executeCommandLine(Duration.ofSeconds(5), "curl", "-i", "-XPOST", "http://localhost:8086/write?u=USER&p=PASSWORD&db=DATABASE", "--data-binary", "item_corrected value=10 1658163599945000000" )

1 Like