InfluxDB2: Data storage

Hi everyone,

after searching without finding any result I decide to write down my solution for storing fetched data into the InfluxDB2 database using InfluxDB line protocol and OpenHAB’s HTTP actions.

For doing so, I add a simple rule for each data point I wanted to store in InfluxDB.

InfluxDb.rules

rule "Set Temperature - Thermal collectors"
when
    Item Collector_Temp changed  or
    Time cron "0 0 * * * ?" //everyHour
then
    //Server URL
    val url = "http://<InfluxDbServerIp>:8086/api/v2/write?org=YouOrganization&bucket=YourBucket&precision=s"

    //Timestamp
    val timestamp = Collector_Time.state
    //alternatively using the system time
    //val timestamp = now.toInstant.getEpochSecond()

    //Data
    //Using InfluxDB line protocol (https://docs.influxdata.com/influxdb/cloud/reference/syntax/line-protocol/)/line_protocol_tutorial/
    val data = "Collector_Temp,Sensor=Collector temperatur=" + (Collector_Temp.state as Number).toString.replace(' °C','') + " " + timestamp

    //Header information
    val headers = newHashMap("Authorization" -> "Token <YoutAuthenticationToken>")

    //Send HTTP post Request
    sendHttpPostRequest(url, "text/plain", data, headers, 5000)
end

In the above snipped I changed the following user specified values:

  • <InfluxDbServerIp>
  • YouOrganization
  • YourBucket
  • <YoutAuthenticationToken>

I would prefer a more convenient way for storing my data, unfortunately, I did not build a whole understanding of persistence in Openhab till now.

Maybe one will have a hint or o better solution for doing this. If so, I am glad to hear about it.

Best regards
Stephan

Collector_Temp.persist("influxdb")

1 Like

Hi,

thank you for the quick hint!
Now I recognized, that I should have also change the InfluxDB config file (services/influxdb.cfg), as the documentation denotes:
InfluxDB (0.9 and newer) Persistence - Configuration. This file changed kind of massively regarding to V1.8.

With doing so I can still use the Persistence Configuration file as got used to doing with Influx DB V1.8. It’s like magic!

Best regards
Stephan

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