Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Fibaro Influxdb Telegraf plugin


1forrest

Question

Hi!

I have started to use the Telegraf plugin in Influxdb to pull in Fibaro device data through the http API. It looks it only pulls the real devices but not the virtual ones. Does anyone has the same problem? Any idea how to workaround it?

I checked directly the API calling a virtual devise ID and that works of course but the plugin does not pull in.

Thanks!

Link to comment
Share on other sites

14 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • Thanks for the link. I have tried to use it but it looks it is more for HC3. I have HC2. Going through the code now but struggle to rewrite. I have argument error now. By any change do not you have a HC2 version?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Similar to the HC3 code, but still getting error. Since I am using the Influxdb 2.0 the auhtentication part has to be rewritten to toke, bucket, eorg, etc as far as I know according to the new API.

    Link to comment
    Share on other sites

    • 0
    5 hours ago, SmartHomeEddy said:

    Someone else also tried v2.0, but doesn't work (indeed authentication). You have to downgrade to v1.8

    no. just add u / p

    Edited by hater
    Link to comment
    Share on other sites

    • 0
  • Inquirer
  •  

    I still want to resolve in v2 rather than downgrade. But maybe later give up and ending in 1.8:)

     

    As far as I know user and password not enough since the organization and token concept has been introduced in 

    Please login or register to see this link.

    . Hence I rewrote the url and now it is: [DEBUG] 18:24:19: 192.168.1.124:8086/api/v2/write?org=fibaro&bucket=fibaro3&precisions=s

    And the token is also provided as a parameter in the header:  --  ["Authorization"] = "Authothorization: Token " .. deviceToken,

     

    Still having issue and I think this is not because of theauthentication (yet) When the function called  "sendDataToInflux(body)" then I get 

    [DEBUG] 18:26:14: 2021-04-26 18:26:14.191664 [ fatal] Unknown exception: std::runtime_error: 'Invalid argument'

    My body also looks ok:

     

    [DEBUG] 18:26:14: Body: fibaro,sectionName=Outside,deviceID=105,roomName=Outside,deviceName=WindSpeed\ (km/h) value=2.5678057167419 1.619454374e+18

     

    Any suggestion what argument can cause the problem? I created a brand new bucket (fibaro3) in influx to feed tha data to. 

    Link to comment
    Share on other sites

    • 0

     

    On 3/28/2021 at 12:09 PM, JcBorgs said:

    @claudent I do believe that the difference is what version of Influx you are running. I have two RaspBerryPi, one running InfluxDB 1.8 (32bit) and one running InfluxDb 2.0 (64bit).

     

    I see the exact same error messages on the InfuxDb 2.0 which is related to that this version have a different authentication system. I have so far not been able to solve that and have given up and only use the InfluxDB 1.8.

     

     

     

    you are not alone ?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I will try to get some help through influxdb forum. I hope get some valuablw info bwfore I gave up:) 

    Link to comment
    Share on other sites

    • 0

    I have a scene on HC2 pushing the data to influxdb. I found it

    Please login or register to see this link.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Finally I could rewrite the HC2 code to Influxdb V2. Works fine for me. Here is the code:

     

     

    --[[
    %% properties
    %% events
    %% globals
    --]]

    function urlencode(str)
      if (str) then
        str = string.gsub (str, "\n", "\r\n")
        str = string.gsub (str, "([^%w %-%_%.%~])",
            function (c) return string.format ("%%%02X", string.byte(c)) end)
        str = string.gsub (str, " ", "+")
      end

      return str    
    end

    local host = "IP" -- Host of Influxdb
    local port = "<PORT>" -- Port of Influxdb, default 8086
    local bucket = "<BUCKET>" -- Your Bucket name
    local deviceToken = "<TOKEN>" -- Token of the bucket. Must has read/write access to bucket
    local org = "<ORGANIZATION>" -- Influx Organization name
    local org_id = "<ORGANIZATION ID>" -- Influx Organization ID
    local tcpTimeout = 10 * 10000
    local httpClient = net.HTTPClient({timeout=tcpTimeout})


    function sendDataToInflux(body)
        local url = host .. ":" .. port .. "/api/v2" .."/write?" .. "org=" .. org .. "&bucket=" .. bucket
      -- .. "&precisions=ns" 
    --print(url)
        local headers = {
            ["Authorization"] = "Token " .. deviceToken,
            ["Content-Type"] = "application/json",
           }

        httpClient:request(
            url,
            {
                options = {
                    headers = headers,
                    data = body,
                    method = "POST",
                    timeout = tcpTimeout
                            },
                success = function(response)
                    if (response.status < 200 or response.status >= 300) then
                        print(json.encode(response))
                        print("Wrong status '" .. response.status .. "' in response!")
                    else
                         print(json.encode(response))
                    end
                end,
                error = function(response)
                    print("Connection error: ")
                end
            }
        )
    end

    function saveToInflux(measurement_name, value, deviceID, deviceName, roomName, sectionName)
        local data = {
            measurement = measurement_name,
            time = os.time(os.date("*t")),
        tags = {
                deviceID = deviceID,
                deviceName = deviceName,
                roomName = roomName,
                sectionName = sectionName
                 },
            fields = {
                value = value
                  }
           }
        
      local body = data.measurement
        for k, v in pairs(data.tags) do
            if v ~= nil then
                v = tostring(v)
                v = v:gsub("%s+", "\\ ")
                body = body .. (body == "" and "" or ",") .. (k .. "=" .. v)
            end
        end
        body = body .. " " .. "value=" .. tostring(data.fields.value)
      --.. " " .. tostring(data.time)
        print("Body: " .. body)
       sendDataToInflux(body)
    end

    function saveByDeviceID(measurement, deviceID, property)
        property = property or "value"
        local roomID = fibaro:getRoomID(deviceID)
        local room = api.get("/rooms?id="..roomID)
        local section = api.get("/sections?id="..room.sectionID)
        local value = fibaro:getValue(deviceID, property)
      

       saveToInflux(measurement, value, deviceID, fibaro:getName(deviceID), fibaro:getRoomNameByDeviceID(deviceID), section.name)

     end

    -- Devices to be sent

    saveByDeviceID("fibaro3", 105) -- Provide the measurement name and the device ID
     

    Edited by 1forrest
    • Like 1
    • Thanks 1
    Link to comment
    Share on other sites

    • 0

    Hello, I used influxdb on HC2, HC2 has automatically push all data to influxdb.

    I'm on HC3 now, is there any step-by-step instruction for setup? Where exactly I need to put above code?

    Influxd is up&running, but what to do in HC3?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi!

    The above code I put is to HC2 on v2 Influx. This should not work on HC3. You should use the one that was linked above. Here is also the link: 

    Please login or register to see this link.

    The question what version of Influxdb you are using. This code works for 1.8 as far as I know. If you have v2 then you have to rewrite the authentication part since bucket, organization and token required. 

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...