Lua script to run a php page

Moderator: leecollings

Post Reply
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Lua script to run a php page

Post by miel »

Ok, i am trying to run a Lua script which runs a certain php page as soon as a switch is marked as "on"

I have written this code:

Code: Select all

--[[
This script runs a php page when a certain switch is "on"
--]]

commandArray = {}
 
-- declare some constants
-- adjust to your specific situation

SENSOR_NAME = 'Beweging badkamer'     -- exact device name of the motion sensor
    if (otherdevices[SENSOR_NAME]=='On' ) then
        
 http.request_batch({
     {"GET", "http://192.168.1.82/~Miel/HWtemp.php"}
 }) 
       end
return commandArray
What i see in my log is that this script is not run...... I don't see the mistake.... Any clues?

(Reason for using this script is that i would like my temperature sensors from Homewizard to send updates to Domoticz... if i do that every minute, it will lead to slow performance of my Homewizard..... I run Domoticz on a Synology Nas).
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Lua script to run a php page

Post by Egregius »

Why not run the PHP script by cron every x minutes?
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

Egregius wrote:Why not run the PHP script by cron every x minutes?
Because of performance.....

My Homewizard gets really slow when i run that php job every minute.......
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to run a php page

Post by jvdz »

What is the name of this script to determine on which event is should fire?
Do you really want to run the PHP page each time the script gets fired and the switch is on or only when it changes to on?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

jvdz wrote:What is the name of this script to determine on which event is should fire?
Do you really want to run the PHP page each time the script gets fired and the switch is on or only when it changes to on?

Jos
Jos, the script is named script_run_php.lua

And yes, i really want to run this script every minute, so that every minute the PHP file gets called as long as the motion sensor is "on"... If the sensor is "Off", it should just do nothing...

(so that i can do humidity control in my bathroom).
pj-r
Posts: 140
Joined: Wednesday 17 December 2014 17:30
Target OS: Linux
Domoticz version: V3.8650
Location: Jyväskylä, Finland
Contact:

Re: Lua script to run a php page

Post by pj-r »

miel wrote:Jos, the script is named script_run_php.lua
The lua scripts folder has two demo scripts that can serve as a starting point. The script filename has 3 components each sub-divided by an underscore : script_trigger_name.lua Trigger can be 'time', 'device', 'variable' or 'security', name can be any string for your reference, script_device_MyOtherDeviceNameOnWhenMyDeviceNameOn.lua for instance.
https://www.domoticz.com/wiki/Events#2. ... on_folder.
LXC(x64 Ubuntu Xenial), RFXtrx433E, MySensors
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to run a php page

Post by jvdz »

so, just rename it to " script_time_run_php.lua" and you are all set. ;)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

Ok script runs now, but i get the following error:

Error: /usr/local/domoticz/scripts/lua/script_time_run_php.lua:19: attempt to index global 'http' (a nil value)

That is the http request line..... Weird...
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to run a php page

Post by jvdz »

Do you have the HTTP addon loaded somewhere in your script with something like this:?

Code: Select all

http = require "socket.http";
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

jvdz wrote:Do you have the HTTP addon loaded somewhere in your script with something like this:?

Code: Select all

http = require "socket.http";
Jos
No,

The entire script i have copied/paste is in my opening message... So i should enter these lines in as well??
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to run a php page

Post by jvdz »

You will need something to define the http.xxxx function as that is not a standard available LUA command/
Where did you find this command?

I am using http.request(url) myself which is part of the library I refereed to in my previous post. This one is an add on which is made available in the DTGBOT project. ( https://www.domoticz.com/wiki/Remote_Co ... _Libraries )

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

jvdz wrote:You will need something to define the http.xxxx function as that is not a standard available LUA command/
Where did you find this command?

I am using http.request(url) myself which is part of the library I refereed to in my previous post. This one is an add on which is made available in the DTGBOT project. ( https://www.domoticz.com/wiki/Remote_Co ... _Libraries )

Jos
i found it in a wiki.....

DTGBOT, should i install that on my synology as well?
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua script to run a php page

Post by jvdz »

I am just talking about the linked support libraries, not the whole DTGBOT.
Doesn't the Wiki page tell you the prerequisites that needs to be installed?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

jvdz wrote:I am just talking about the linked support libraries, not the whole DTGBOT.
Doesn't the Wiki page tell you the prerequisites that needs to be installed?

Jos
unfortunately not...

Is there no standard HTTP command in Lua? (Without having to load libraries?)
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Lua script to run a php page

Post by G3rard »

I use the following command in LUA to run a PHP page.

Code: Select all

os.execute('curl --url "http://ip/page.php"')
Not using Domoticz anymore
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

It is working!

Thanks!!!
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Lua script to run a php page

Post by Egregius »

Great, I was just about to write you a php script that would only query the HW when the switch was on in domoticz.
Anyway, I am wondering what you ask the HW to get him really slow. I used to query him every 15sec.
miel
Posts: 9
Joined: Monday 02 November 2015 11:58
Target OS: -
Domoticz version:
Contact:

Re: Lua script to run a php page

Post by miel »

Every 15 seconds??? I just used a simple php to get the status of 5 thermometers..

I noticed the homewizard was often "not available" after i ran the php..

Which php script do you use?
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Lua script to run a php page

Post by Egregius »

PHP Floorplan ;)
There's also a part to import data from homewizard into Domoticz in secure/hw2domoticz.php
The every 15 sec was before I had domoticz and 'everything' was controlled by HW.
Even on the very last use of my HW I often get not available messages, even with only one thermometer and a reading every 2 minutes to domoticz.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest