LUA Post method

Moderator: leecollings

Post Reply
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

LUA Post method

Post 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!
11101101 - www.machinon.com
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: LUA Post method

Post 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
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: LUA Post method

Post 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/
11101101 - www.machinon.com
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

Re: LUA Post method

Post 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.
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: LUA Post method

Post 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. :)
11101101 - www.machinon.com
NietGiftig
Posts: 121
Joined: Sunday 11 October 2015 8:50
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6224
Location: Holland
Contact:

Re: LUA Post method

Post 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.
RPI-2 + SSD / ESPEasy Sensors & Switches / Sonoff / RFLink / Action Switches / TP-Link switch / Node-Red / Reacticz
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: LUA Post method

Post by EdddieN »

:D yes you right. I'll keep trying :P
11101101 - www.machinon.com
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest