HC3 into InfluxDB using LUA scripting HTTP POST method - Error 404 - bucket not found

Hi everyone.

I have hit a stumbling block getting data from Home Center 3 into InfluxDB v2. I have setup InfluxDB correctly as testing using curl from a Ubuntu server works a treat, data manually imports fine.

curl --request POST "http://172.16.3.101:8086/api/v2/write?org=HomeLAB&bucket=HC3&precision=ns" --header "Authorization: Token 019gWkLe2DJ8jsOvav0TvPjJyILiYjO3UctuHpgrNfGIWmgXrgPmenQj8UXAuaCfdmUa-CUA00gSid4Vh0VPkw==" --header "Content-Type: text/plain; charset=utf-8" --header "Accept: application/json" --data-binary 'temperature,sensor_id=28 temperature=24.3'

When I convert this into LUA for HC3 I get a status 404 error and a message relating to bucket not found. The URL formed by the LUA code is correct as the output shows it is the same as the URL used when testing with curl.

Are there any Home Center 3 gurus on the forum? The LUA code for HC3 is slightly different to LUA, I have posted the function I use to send data to InfluxDB, if anyone can take a look and see if there are any glaring errors that would be great.

function sendDataToInflux(body)
    -- local url = "http://" .. host .. ":" .. port .. "/api/v2/write?org=" .. dbOrg .. "&bucket=" .. dbName .. "&precision=ns"
    local url = "http://172.16.3.101:8086/api/v2/write?org=HomeLAB&bucket=HC3&precision=ns"

    local headers = {
        ["Authorization"] = "Token 019gWkLe2DJ8jsOvav0TvPjJyILiYjO3UctuHpgrNfGIWmgXrgPmenQj8UXAuaCfdmUa-CUA00gSid4Vh0VPkw==",
        ["content-type"] = "application/binary; charset=utf-8",
        ["Accept"] = "application/json"
    }

    fibaro.debug("Scene55", "url: " .. url)
    fibaro.debug("Scene55", "headers: " .. json.encode(headers))
    fibaro.debug("Scene55", "body: " .. body)

    local http = net.HTTPClient()
    http:request(
        url,
        {
            options = {
                method = "POST",
                data = body,
                headers = headers
            },
            success = function(response)
                if (response.status < 200 or response.status >= 300) then
                    -- print(json.encode(response))
                    fibaro.debug("Scene55", "Response Status: " .. response.status)
                    fibaro.debug("Scene55", "Response Data: " .. response.data)
                    fibaro.debug("Scene55", "Headers: " .. json.encode(response.headers))
                    print("Wrong status '" .. response.status .. "' in response!")
                else
                    fibaro.debug("Scene55", "Success: " .. response.data)
                end
            end,
            error = function(response)
                -- print("Connection error: " .. message)
                fibaro.debug("Scene55", "Connection error: " .. response)
            end
        }
    )
end

The various debug outputs are detailed below.

Response Status: 404
Response Data: {"code":"not found","message":"bucket not found"}
Headers: {"X-Platform-Error-Code":"not found","Content-Length":"49","Date":"Sat, 01 Jun 2024 23:58:39 GMT","X-Influxdb-Version":"v2.7.6","Content-Type":"application/json; charset=utf-8","X-Influxdb-Build":"OSS"}

Thanks in advance for any advise that can be offered :slight_smile:

For anyone who is interested I have found a work around, or maybe added functionality for data manipulation.

I have installed an app called Flask onto my Ubuntu VM, same VM as InfluxDB and Grafana. This app is configured to listen for HTTP POST requests on port 8000. When detected a Python script is run to convert the HTTP POST streams from my Home Center 3 LUA Scenes to CURL HTTP POST requests.

The CURL HTTP POST request is the sent onward to InfluxDB over port 8086.

Data flow summary:

HC3 Lua Script:

  • Sends an HTTP POST request to the Flask server.
  • Runs every 5 minutes, sending the formatted data payload to Flask server.

Flask Server:

  • Listens on port 8000 for incoming HTTP POST requests from the HC3.
  • Receives the data payload and processes it.
  • Constructs a curl command to forward the data to InfluxDB.

InfluxDB:

  • Receives the data from the Flask server.
  • Stores the data in the specified bucket with the provided precision.

If there are any HC3 LUA gurus out there I am still interested in understanding why InfluxDB isn’t seeing the HTTP POST requests.

Thanks

@Ta2oo
Thank you so much for sharing your solution. I don’t have a home center so this is hard for me to test.