Logic Machine Forum
influxdb - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: influxdb (/showthread.php?tid=1531)

Pages: 1 2


RE: influxdb - sx3 - 17.10.2022

Thanks admin, that worked fine!
However, the script pushes a hell load of data to influx that is not really necessary.
I could use the "range" tip from above, but using "tags" would be easier and more flexible for future changes.
I tried my best, but I don't think I understand event.getvalue to be honest.

This particular GA have 2 tags, one of them is "influxdb"

BTW, any suggestion on the timestamp manipulation? Smile

Code:
function knx_callback(event)
  local id = event.dstraw
  local tagname = event.getvalue(tag)

  if tagname == "influxdb" then
    local addr = event.dst
    local value, dt = get_value(addr, event.datahex)
    if value ~= nil then
    send_metric('rawdata', dt.name, addr, value)
    end
  end
end



RE: influxdb - admin - 18.10.2022

Try this rewritten version:
Code:
local http = require('socket.http')
http.TIMEOUT = 5

local url = 'http://192.168.1.200:8086/api/v2/write?bucket=XXX&org=XXX'
local token = 'XXXXX'
local influxtable = 'rawdata'
local tagobjs = grp.tag('influxdb')

local function escape(value)
  return tostring(value):gsub('[\\ ,=]', '\\%1')
end

local objects = {}

for _, obj in ipairs(tagobjs) do
  objects[ obj.id ] = {
    name = escape(obj.name),
    datatype = obj.datatype,
  }
end

local function send(name, addr, value)
  local body

  if type(value) == 'boolean' then
    body = string.format('%s,name=%s,addr=%s state=%s', influxtable, name, addr, value)
  else
    body = string.format('%s,name=%s,addr=%s value=%s', influxtable, name, addr, escape(value))
  end

  local res, code = http.request({
    url = url,
    method = 'POST',
    body = body,
    headers = {
      Authorization = 'Token ' .. token
    }
  })

  if code ~= 204 then
    log('error sending to influx', res, code, body)
  end
end

local busclient = require('localbus').new(60)
busclient:sethandler('groupwrite', function(event)
  local obj = objects[ event.dstraw ]

  if obj then
    local value = busdatatype.decode(event.datahex, obj.datatype)

    if value ~= nil then
      send(obj.name, event.dst, value)
    end
  end
end)

while true do
  busclient:loop(60)
end



RE: influxdb - tigi - 27.10.2022

Hi admin,

Above script is working perfectly however is there a way I can use this as a scheduled script?
I see it's now kept resident and sends data according what is generated by the objects and that can be alot making one object hijacking the stream.

I tried changing the busclient and while loop part but without any success, it only sends one objects data and then stops.
Is there any info on the localbus and how to use it?

Thanks!


RE: influxdb - admin - 28.10.2022

You don't need localbus for this as the current object value is already stored in the database.

Code:
local http = require('socket.http')
http.TIMEOUT = 5

local url = 'http://192.168.1.200:8086/api/v2/write?bucket=XXX&org=XXX'
local token = 'XXXXX'
local influxtable = 'rawdata'
local tagobjs = grp.tag('influxdb')

local function escape(value)
  return tostring(value):gsub('[\\ ,=]', '\\%1')
end

local function send(name, addr, value)
  local body

  if type(value) == 'boolean' then
    body = string.format('%s,name=%s,addr=%s state=%s', influxtable, name, addr, value)
  else
    body = string.format('%s,name=%s,addr=%s value=%s', influxtable, name, addr, escape(value))
  end

  local res, code = http.request({
    url = url,
    method = 'POST',
    body = body,
    headers = {
      Authorization = 'Token ' .. token
    }
  })

  if code ~= 204 then
    log('error sending to influx', res, code, body)
  end
end

for _, obj in ipairs(tagobjs) do
  send(escape(obj.name), obj.address, obj.value)
end



RE: influxdb - tigi - 28.10.2022

Hi Admin,

Thank you very much for clearing that out and for the solution.

Greetings


RE: influxdb - sx3 - 23.12.2022

I have noted that influxDB somehow doesn't like the BOOL values sent to it. When trying to show the values I get the error "unsupported input type for mean aggregate: boolean"
If I set aggregate function to last in influxDB, it just give me an infinite line. Doesn't show the actual state of True/false.

Should the BOOL values also be "escape(value)" as the strings?


RE: influxdb - admin - 23.12.2022

A possible solution is to send 1/0 instead of true/false. Then you can use aggregate functions.
Code:
  if type(value) == 'boolean' then
    value = value and 1 or 0
  end

  body = string.format('%s,name=%s,addr=%s value=%s', influxtable, name, addr, escape(value))



RE: influxdb - DGrandes - 04.01.2024

Hi!

I have a problem sending strings to influxdb

How can I send 250bytes or 14bytes strings?

I've tried with

Code:
local function escape(value)
  return tostring(value):gsub('[\\ ,=]', '\\%1')
end

if type(value) == 'string' then
    body = string.format('%s,name=%s,addr=%s string=%s', influxtable, name, addr, escape(value))
end

if type(value) == 'string' then
    body = string.format('%s,name=%s,addr=%s string=%s', influxtable, name, addr, value)
end

but doesn´t work. This is the error:

Code:
  * string: {"code":"invalid","message":"unable to parse 'DatosLM,name=PruebaString,addr=32/1/4 value=Probando\\ String': invalid boolean"}



RE: influxdb - admin - 04.01.2024

You need to use a different field name for each data type (boolean, number, string). See how it's done here: https://kb.logicmachine.net/integration/influxdb/

It looks like you've already written a boolean value into "string" field so it won't accept string data into this field anymore. Either choose a different field name and modify the script accordingly. Or delete the bucket and create it again.


RE: influxdb - DGrandes - 04.01.2024

Thanks! It works

I don´t know whiy doesn´t work because I try with diferent "field" names. But now it works.


RE: influxdb - DGrandes - 12.01.2024

Hi!

I have a question about Grafana representacion of influxdb data. I know that is grafana question but anyone says nothing, this forum is not active... Mayby somebody made.
I send data from LM to influxdb when they change, no in scheduled script.

If I put time filter I can't see the first value (the value before the change). And if there are no changes in the data in the filter, it says "No data". Is there a way to represent the last value in the database if there are no changes?


RE: influxdb - admin - 12.01.2024

You can setup a scheduled script that periodically sends the current value. I don't see any other solution.


RE: influxdb - DGrandes - 13.01.2024

(12.01.2024, 15:08)admin Wrote: You can setup a scheduled script that periodically sends the current value. I don't see any other solution.

Ok thanks! that's what I had thought.
I was wondering if there was another way to save the data in real time.

Thanks!


RE: influxdb - admin - 13.01.2024

You can use both resident and scheduled scripts for influxdb in parallel.