Page 1 of 1

LUA Post method

Posted: Thursday 02 February 2017 16:04
by EdddieN
Hello there,

I'm trying to do a very simple LUA script to take the last value stored in a sensor ro status and be forwarded to a website (like a widget). Is my first time attempting LUA and I'm not a coder/developer by far. What I'm trying to do is something like the below:

http://domain/something.php?value1=a&value2=b

and then having this executed to a fix interval of time, ie every few minutes.

Looking into some LUA examples I found this, but still a bit lost on how to make this work within Domoticz:

Code: Select all

require'socket.http'
require'ltn12'
response_body = {}
request_body = "a=1"
socket.http.request{
    url = "http://mysite.com/test.php";,
    method = "GET",
    headers = {
         ["Content-Length"] = string.len(request_body)
     },
     source = ltn12.source.string(request_body),
     sink = ltn12.sink.table(response_body)
}
Any hints would be great!

Re: LUA Post method

Posted: Thursday 02 February 2017 16:16
by Egregius
Maybe this could work:

Code: Select all

var = whatevervalue
os.execute('curl -s -d "a='..var..'" http://mysite.com/test.php &')
commandArray={}
return commandArray

Re: LUA Post method

Posted: Thursday 02 February 2017 20:04
by EdddieN
Interesting.

I was hoping to get it in pure LUA. I managed to get it to work like this:

Code: Select all

local my_sensor = 'Server CPU'
local my_values = otherdevices_svalues[my_sensor]

local http = require 'socket.http'

-- returns a date time like 2013-07-11 17:23:12

local t1 = os.date("%M")

commandArray = {}
 
 -- execute only every 10 minutes
 if t1%10 == 0 then
   datatosend = .. tostring(my_values)
   body,c,l,h = http.request("http://yourURL/yourphpfile.php?yourdatavar=" .. datatosend)
 end 

commandArray['body'] = body
return commandArray
I'm sure it can be simplified (not very pretty), just don't know enough of LUA, just picking pieces from different places...
This I found quite useful http://ideone.com/

Re: LUA Post method

Posted: Friday 03 February 2017 8:32
by NietGiftig
Maybe you find this interesting:
https://github.com/dannybloe/dzVents#ad ... ur-scripts
dzVents (|diː ziː vɛnts| short for Domoticz Easy Events) brings Lua scripting in Domoticz to a whole new level. Writing scripts for Domoticz has never been so easy. Not only can you define triggers more easily, and have full control over timer-based scripts with extensive scheduling support, dzVents presents you with an easy to use API to all necessary information in Domoticz. No longer do you have to combine all kinds of information given to you by Domoticzs in many different data tables. You don't have to construct complex commandArrays anymore. dzVents encapsulates all the Domoticz peculiarities regarding controlling and querying your devices. And on top of that, script performance has increased a lot if you have many scripts because Domoticz will fetch all device information only once for all your device scripts and timer scripts.

Re: LUA Post method

Posted: Friday 03 February 2017 13:34
by EdddieN
Very interesting, is going to take me a while to get through it. My objective is to do things as simple as possible.

I definitely like the example of humidity within the last 5 minutes, that is useful and the average function. :)

Re: LUA Post method

Posted: Friday 03 February 2017 15:15
by NietGiftig
EdddieN wrote:My objective is to do things as simple as possible.
Is that not what everybody wants?
But sometimes it is not so simple as you would it to be.
And then you have to adjust.

Re: LUA Post method

Posted: Friday 03 February 2017 19:09
by EdddieN
:D yes you right. I'll keep trying :P