influxdb library - Matt - 20.10.2018
I am sharing a way to send easily object values to influxdb.
Update server and database name before using it.
influx_clim send object with tag "influx_clim"
add "influx_clim()" to a schedule for automatic export
Code: require 'ltn12'
require 'socket.http'
local https = require("ssl.https")
local influxdb_debug = true
local influxdb_server = "https://user:pass@host:8086"
local influxdb_database = "db_name"
local influxdb_buffer
--function used to convert object name (logicmachine) to valid influxdb field key
function influxdb_normalize(name)
return string.gsub(name, " ", "_")
end
--add an entry to be send
function influxdb_add(line_protocol)
if (influxdb_debug) then
alert("influxdb_add start")
end
if influxdb_buffer then
influxdb_buffer = influxdb_buffer .. "\n" .. tostring(line_protocol)
else
influxdb_buffer = tostring(line_protocol)
end
if (influxdb_debug) then
alert("influxdb_add done")
end
end
function influxdb_current()
return influxdb_buffer
end
-- send the buffer to influxdb
function influxdb_send()
if (influxdb_debug) then
alert("influxdb_send start")
end
local request_insert_url = influxdb_server .. "/write?db=" .. influxdb_database
-- log(influxdb_buffer)
local response_body = { }
local body, code, hdrs, stat = https.request
{
url = request_insert_url;
verify = "none";
method = "POST";
headers =
{
["Content-Length"] = #influxdb_buffer;
};
source = ltn12.source.string(influxdb_buffer);
sink = ltn12.sink.table(response_body);
}
influxdb_buffer = nil
if (code ~= 204) then
alert("influxdb error code:" .. tostring(code))
alert("influxdb_send http post error")
end
if (influxdb_debug) then
alert("influxdb_send done")
end
end
--export all objects with tag "influx_clim"
function influx_clim()
local values=grp.tag("influx_clim")
if (influxdb_debug) then
alert("influx_cilm start")
end
local txtvalues = ""
for index, id in ipairs(values) do
-- add separator
if (#txtvalues>0) then
txtvalues = txtvalues .. ","
end
-- calculate value to be sent
if (type(id["data"])=="boolean") then
value=id["data"] and 1 or 0
else
value=id["data"]
end
-- add key value pair
txtvalues = txtvalues .. influxdb_normalize(id["name"]) .. "=" .. value
end
influxdb_add("clim " .. txtvalues)
influxdb_send()
if (debug) then
alert("influx_cilm end")
end
end
RE: influxdb library - Domoticatorino - 03.11.2020
(20.10.2018, 21:13)Matt Wrote: I am sharing a way to send easily object values to influxdb.
Update server and database name before using it.
influx_clim send object with tag "influx_clim"
add "influx_clim()" to a schedule for automatic export
Code: require 'ltn12'
require 'socket.http'
local https = require("ssl.https")
local influxdb_debug = true
local influxdb_server = "https://user:pass@host:8086"
local influxdb_database = "db_name"
local influxdb_buffer
--function used to convert object name (logicmachine) to valid influxdb field key
function influxdb_normalize(name)
return string.gsub(name, " ", "_")
end
--add an entry to be send
function influxdb_add(line_protocol)
if (influxdb_debug) then
alert("influxdb_add start")
end
if influxdb_buffer then
influxdb_buffer = influxdb_buffer .. "\n" .. tostring(line_protocol)
else
influxdb_buffer = tostring(line_protocol)
end
if (influxdb_debug) then
alert("influxdb_add done")
end
end
function influxdb_current()
return influxdb_buffer
end
-- send the buffer to influxdb
function influxdb_send()
if (influxdb_debug) then
alert("influxdb_send start")
end
local request_insert_url = influxdb_server .. "/write?db=" .. influxdb_database
-- log(influxdb_buffer)
local response_body = { }
local body, code, hdrs, stat = https.request
{
url = request_insert_url;
verify = "none";
method = "POST";
headers =
{
["Content-Length"] = #influxdb_buffer;
};
source = ltn12.source.string(influxdb_buffer);
sink = ltn12.sink.table(response_body);
}
influxdb_buffer = nil
if (code ~= 204) then
alert("influxdb error code:" .. tostring(code))
alert("influxdb_send http post error")
end
if (influxdb_debug) then
alert("influxdb_send done")
end
end
--export all objects with tag "influx_clim"
function influx_clim()
local values=grp.tag("influx_clim")
if (influxdb_debug) then
alert("influx_cilm start")
end
local txtvalues = ""
for index, id in ipairs(values) do
-- add separator
if (#txtvalues>0) then
txtvalues = txtvalues .. ","
end
-- calculate value to be sent
if (type(id["data"])=="boolean") then
value=id["data"] and 1 or 0
else
value=id["data"]
end
-- add key value pair
txtvalues = txtvalues .. influxdb_normalize(id["name"]) .. "=" .. value
end
influxdb_add("clim " .. txtvalues)
influxdb_send()
if (debug) then
alert("influx_cilm end")
end
end
Hi, I am working on this script but It seems it does not work.
I mean that I use the script above in a library and in a scheduled script which runs everyminute I wrote as followed:
objs = grp.tag('influx_clim')
require ('user.influx')
The problem is that I expect some alert message (correct or incorrect) but nothing happened. How can sort it out?
Thanks.
RE: influxdb library - Matt - 04.11.2020
In the schedule, you only need to have:
RE: influxdb library - Domoticatorino - 18.11.2020
(04.11.2020, 12:42)Matt Wrote: In the schedule, you only need to have:
Sorry but I forgot to leave the feedback.
Everything works fine.
Thanks.
BR
|