28.10.2022, 06:06
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