Posts: 19
Threads: 6
Joined: Apr 2018
Reputation:
0
I want to store LM object (values) data in an influxdb database, does anybody have any experience with this?
Their documentation (
https://docs.influxdata.com/influxdb/v1....ting_data/ ) says that you can use the http api for storing data like this;
Code:
1
curl -
i -
XPOST 'http://localhost:8086/write?db=mydb'
Is this possible from lua in the LM?
Thanks Mischa
Posts: 8100
Threads: 43
Joined: Jun 2015
Reputation:
471
Code:
1 2 3 4
http =
require (
'socket.http' )
http.TIMEOUT =
5
res ,
err =
http.request (
'http://192.168.1.2:8086/write?db=mydb' ,
'cpu_load_short,host=server01,region=us-west value=0.64' )
log (
res ,
err )
Posts: 19
Threads: 6
Joined: Apr 2018
Reputation:
0
(13.08.2018, 11:25) admin Wrote: Code:
1 2 3 4
http =
require (
'socket.http' )
http.TIMEOUT =
5
res ,
err =
http.request (
'http://192.168.1.2:8086/write?db=mydb' ,
'cpu_load_short,host=server01,region=us-west value=0.64' )
log (
res ,
err )
Thanks, works perfectly.
Posts: 32
Threads: 12
Joined: Oct 2015
Reputation:
0
Hi
I have tried this influxdb-connection and got it to work.
I want to write several tags to influxdb and can't really figure out the best way to do this in scripts.....
Data like temperatures, Co2 and also presence objects are data I need to log i influx.
Could anyone give me a push in the right direction here.
Planning to use this with grafana.
Everything is up and running but can't figure out the most vital part:-)
Eirik
Posts: 45
Threads: 4
Joined: Jan 2020
Reputation:
4
28.05.2020, 16:47
(This post was last modified: 28.05.2020, 16:49 by myg .)
This code will send everything it captures to influx, then you can filter whatever you want in grafana. Create resident script with zero delay
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
local socket =
require (
'socket' )
local http =
require (
'socket.http' )
http.TIMEOUT =
5
local dt_cache = {}
function get_value (
addr ,
datahex )
local dt =
dt_cache [
addr ]
if (
not dt )
then
dt =
grp.find (
addr )
dt_cache [
addr ] =
dt
end
return knxdatatype.decode (
datahex ,
dt.datatype ),
dt
end
function knx_callback (
event )
local addr =
event.dst
local value ,
dt =
get_value (
addr ,
event.datahex )
send_metric (
'rawdata' ,
dt.name ,
addr ,
value )
end
function send_metric (
table ,
name ,
addr ,
value )
if (
name ==
nil or name ==
'' )
then
return
end
name =
string.gsub (
name ,
' ' ,
'\\ ' )
local url =
'http://IP:8086/write?db=knxdb'
local args
if (
type (
value ) ==
'boolean' )
then
args =
string.format (
'%s,name=%s,addr=%s state=%s' ,
table ,
name ,
addr ,
value )
else
args =
string.format (
'%s,name=%s,addr=%s value=%s' ,
table ,
name ,
addr ,
value )
end
res ,
err =
http.request (
url ,
args )
if (
err ~=
204 )
then
log (
'error sending to influx' ,
res ,
err ,
args )
end
end
function run_loop ()
local bus =
require (
'localbus' ).
new (
1 )
bus :
sethandler (
'groupwrite' ,
knx_callback )
local busfd =
socket.fdmaskset (
bus :
getfd (),
'r' )
while (
true )
do
res ,
fbus =
socket.selectfds (
10 ,
busfd )
if (
fbus )
then
bus :
step ()
end
end
end
run_loop ()
Posts: 41
Threads: 9
Joined: Apr 2018
Reputation:
0
Hi, I am wondering if there is a working script to do the same for Influx V2, where you need a token, define organization and push to correct bucket.
kind regards
Thomas
Posts: 8100
Threads: 43
Joined: Jun 2015
Reputation:
471
Modified send_metric function for the previous example. Replace IP/BUCKET/ORG/TOKEN placeholders with your parameters.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
function send_metric (
table ,
name ,
addr ,
value )
if name ==
nil or name ==
'' then
return
end
name =
string.gsub (
name ,
' ' ,
'\\ ' )
local url =
'http://IP:8086/api/v2/write?bucket=BUCKET&org=ORG'
local body
if type (
value ) ==
'boolean' then
body =
string.format (
'%s,name=%s,addr=%s state=%s' ,
table ,
name ,
addr ,
value )
else
body =
string.format (
'%s,name=%s,addr=%s value=%s' ,
table ,
name ,
addr ,
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
Posts: 41
Threads: 9
Joined: Apr 2018
Reputation:
0
14.12.2021, 13:07
(This post was last modified: 14.12.2021, 13:13 by thomasoppida .)
[attachment=2455 Wrote: admin pid='24163' dateline='1639476013']Modified send_metric function for the previous example. Replace IP/BUCKET/ORG/TOKEN placeholders with your parameters.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
function send_metric (
table ,
name ,
addr ,
value )
if name ==
nil or name ==
'' then
return
end
name =
string.gsub (
name ,
' ' ,
'\\ ' )
local url =
'http://IP:8086/api/v2/write?bucket=BUCKET&org=ORG'
local body
if type (
value ) ==
'boolean' then
body =
string.format (
'%s,name=%s,addr=%s state=%s' ,
table ,
name ,
addr ,
value )
else
body =
string.format (
'%s,name=%s,addr=%s value=%s' ,
table ,
name ,
addr ,
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
Thank you for responding Admin. I tried to put in your code in Postman just to verify that access is ok.
I can see to get response in postman I have to set Authorization to Oauth 2.0 and I get the header prefix Bearer.
Do I replace Token with Bearer instead in the script? I didn't get this to work either... Suggestions?
Attached Files
Thumbnail(s)
Posts: 8100
Threads: 43
Joined: Jun 2015
Reputation:
471
It should be Token not Bearer. That's what the documentation states and what worked for me. What kind of error are you getting? Have you checked that the user with this token has correct write access rights to the bucket?
Posts: 2
Threads: 0
Joined: Dec 2021
Reputation:
0
Hi, I have tried the influxdb V2 script above. Get's the following response in the log for all tags (here an example writing brightness value from weather station). Any ideas?
------------------
InfluxWrite 23.12.2021 15:50:52
* arg: 1
* string: error sending to influx
* arg: 2
* string: <html>
<head><title>405 Not Allowed</title></head>
<body>
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
* arg: 3
* number: 405
* arg: 4
* string: rawdata,name=Brightness,addr=0/0/3 value=328.96
Posts: 8100
Threads: 43
Joined: Jun 2015
Reputation:
471
Check correctness of the url that you are sending to.
Posts: 2
Threads: 0
Joined: Dec 2021
Reputation:
0
(23.12.2021, 14:59) admin Wrote: Check correctness of the url that you are sending to.
Hi, I finally managed to write to my Influxdb. The url needs to be at this form: 'https://IP/api/v2/write?bucket=BUCKET&org=ORG'
Posts: 24
Threads: 2
Joined: Mar 2021
Reputation:
0
(23.12.2021, 14:59) admin Wrote: Check correctness of the url that you are sending to.
Hi.
How to set filter for addresses which are sending to influx. I would like to send just part of it (yes i know i can filter them in grafana) for example:
send address form range:
from 1/0/0 to 2/3/15 and from 4/6/7 to 5/1/15.
Can You help me.
Thanks.
Posts: 8100
Threads: 43
Joined: Jun 2015
Reputation:
471
Replace this script part:
Code:
1 2 3 4 5 6
function knx_callback (
event )
local addr =
event.dst
local value ,
dt =
get_value (
addr ,
event.datahex )
send_metric (
'rawdata' ,
dt.name ,
addr ,
value )
end
With this:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
local range1s =
buslib.encodega (
'1/0/0' )
local range1e =
buslib.encodega (
'2/3/15' )
local range2s =
buslib.encodega (
'4/6/7' )
local range2e =
buslib.encodega (
'5/1/15' )
function knx_callback (
event )
local id =
event.dstraw
if (
range1s <=
id and id <=
range1e )
or (
range2s <=
id and id <=
range2e )
then
local addr =
event.dst
local value ,
dt =
get_value (
addr ,
event.datahex )
send_metric (
'rawdata' ,
dt.name ,
addr ,
value )
end
end
Posts: 24
Threads: 2
Joined: Mar 2021
Reputation:
0
(07.01.2022, 12:12) admin Wrote: Replace this script part:
Code:
1 2 3 4 5 6
function knx_callback (
event )
local addr =
event.dst
local value ,
dt =
get_value (
addr ,
event.datahex )
send_metric (
'rawdata' ,
dt.name ,
addr ,
value )
end
With this:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
local range1s =
buslib.encodega (
'1/0/0' )
local range1e =
buslib.encodega (
'2/3/15' )
local range2s =
buslib.encodega (
'4/6/7' )
local range2e =
buslib.encodega (
'5/1/15' )
function knx_callback (
event )
local id =
event.dstraw
if (
range1s <=
id and id <=
range1e )
or (
range2s <=
id and id <=
range2e )
then
local addr =
event.dst
local value ,
dt =
get_value (
addr ,
event.datahex )
send_metric (
'rawdata' ,
dt.name ,
addr ,
value )
end
end
thanks.
Posts: 74
Threads: 21
Joined: Sep 2016
Reputation:
0
06.10.2022, 16:14
(This post was last modified: 06.10.2022, 16:14 by sx3 .)
Hello,
I'm trying out this influx integration. I get the following error in the error-log.
It managed to write about 20 adresses to influx, before failing.
It's a resident script with 0 deley.
Quote: Resident script:15: attempt to index local 'dt' (a nil value)
stack traceback:
Resident script:15: in function 'get_value'
Resident script:20: in function <Resident script:18>
Library localbus: in function ''
Library localbus: in function ''
Library localbus: in function 'step'
Resident script:66: in function 'run_loop'
And the code (a little bit copy paste here and there from this thread)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
local socket =
require (
'socket' )
local http =
require (
'socket.http' )
http.TIMEOUT =
5
local dt_cache = {}
function get_value (
addr ,
datahex )
local dt =
dt_cache [
addr ]
if (
not dt )
then
dt =
grp.find (
addr )
dt_cache [
addr ] =
dt
end
return knxdatatype.decode (
datahex ,
dt.datatype ),
dt
end
function knx_callback (
event )
local addr =
event.dst
local value ,
dt =
get_value (
addr ,
event.datahex )
send_metric (
'rawdata' ,
dt.name ,
addr ,
value )
end
function send_metric (
table ,
name ,
addr ,
value )
if name ==
nil or name ==
'' then
return
end
name =
string.gsub (
name ,
' ' ,
'\\ ' )
local url =
'http://192.168.1.200:8086/api/v2/write?bucket=XXX&org=XXX'
local body
if type (
value ) ==
'boolean' then
body =
string.format (
'%s,name=%s,addr=%s state=%s' ,
table ,
name ,
addr ,
value )
else
body =
string.format (
'%s,name=%s,addr=%s value=%s' ,
table ,
name ,
addr ,
value )
end
local res ,
code =
http.request ({
url =
url ,
method =
'POST' ,
body =
body ,
headers = {
Authorization =
'Token XXXXX'
}
})
if code ~=
204 then
log (
'error sending to influx' ,
res ,
code ,
body )
end
end
function run_loop ()
local bus =
require (
'localbus' ).
new (
1 )
bus :
sethandler (
'groupwrite' ,
knx_callback )
local busfd =
socket.fdmaskset (
bus :
getfd (),
'r' )
while (
true )
do
res ,
fbus =
socket.selectfds (
10 ,
busfd )
if (
fbus )
then
bus :
step ()
end
end
end
run_loop ()
Posts: 8100
Threads: 43
Joined: Jun 2015
Reputation:
471
Most likely you have some group writes coming from addresses that are not present on LM.
Replace get_value/knx_callback functions with this and see if it works:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
function get_value (
addr ,
datahex )
local obj =
dt_cache [
addr ]
if obj ==
nil then
obj =
grp.find (
addr )
dt_cache [
addr ] =
obj or false
end
if obj then
return knxdatatype.decode (
datahex ,
obj.datatype ),
obj
end
end
function knx_callback (
event )
local addr =
event.dst
local value ,
obj =
get_value (
addr ,
event.datahex )
if value ~=
nil then
send_metric (
'rawdata' ,
obj.name ,
addr ,
value )
end
end
Posts: 74
Threads: 21
Joined: Sep 2016
Reputation:
0
07.10.2022, 14:31
(This post was last modified: 10.10.2022, 05:42 by sx3 .)
Yes, I have alot of GA that is beeing used outside om LM.
Tried your code but it gives me following error
Also wonder how the string would like like if I want to transmit a modified Timestamp, and where should it be placed?
For example (strTime = os.time() + 3600)
Code:
1 2 3 4 5 6 7 8
*
arg :
1
*
string :
error sending to influx
*
arg :
2
*
string : {
"code" :
"invalid" ,
"message" :
"unable to parse 'rawdata,name=Heru\\ -\\ Max\\ exhaust\\ fan\\ speed,\\ EC,addr=2/3/5 value=72': missing tag value" }
*
arg :
3
*
number :
400
*
arg :
4
*
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
local socket =
require (
'socket' )
local http =
require (
'socket.http' )
http.TIMEOUT =
5
local dt_cache = {}
function get_value (
addr ,
datahex )
local obj =
dt_cache [
addr ]
if obj ==
nil then
obj =
grp.find (
addr )
dt_cache [
addr ] =
obj or false
end
if obj then
return knxdatatype.decode (
datahex ,
obj.datatype ),
obj
end
end
function knx_callback (
event )
local addr =
event.dst
local value ,
obj =
get_value (
addr ,
event.datahex )
if value ~=
nil then
send_metric (
'rawdata' ,
obj.name ,
addr ,
value )
end
end
function send_metric (
table ,
name ,
addr ,
value )
if name ==
nil or name ==
'' then
return
end
name =
string.gsub (
name ,
' ' ,
'\\ ' )
local url =
'http://192.168.1.200:8086/api/v2/write?bucket=xxx&org=xxx'
local body
if type (
value ) ==
'boolean' then
body =
string.format (
'%s,name=%s,addr=%s state=%s' ,
table ,
name ,
addr ,
value )
else
body =
string.format (
'%s,name=%s,addr=%s value=%s' ,
table ,
name ,
addr ,
value )
end
local res ,
code =
http.request ({
url =
url ,
method =
'POST' ,
body =
body ,
headers = {
Authorization =
'Token XXXX'
}
})
if code ~=
204 then
log (
'error sending to influx' ,
res ,
code ,
body )
end
end
function run_loop ()
local bus =
require (
'localbus' ).
new (
1 )
bus :
sethandler (
'groupwrite' ,
knx_callback )
local busfd =
socket.fdmaskset (
bus :
getfd (),
'r' )
while (
true )
do
res ,
fbus =
socket.selectfds (
10 ,
busfd )
if (
fbus )
then
bus :
step ()
end
end
end
run_loop ()
Posts: 74
Threads: 21
Joined: Sep 2016
Reputation:
0
Anyone have suggestions? Do I have too much "spaces" in my GA names?
And also the Timestamp would be interesting to learn more about.
Posts: 8100
Threads: 43
Joined: Jun 2015
Reputation:
471
The problem is not with spaces but with comma character in the object name.