Page 1 of 1

Lua Code problem

Posted: Saturday 02 September 2017 20:03
by ironrider
Hi,

i have a very simple Lua Code but it wont run :

Code: Select all

commandArray = {}

swasserverbrauch = tonumber(uservariables['wasserverbrauch'])

if (devicechanged['Ventilator'] == 'On') then 

   swasserverbrauch = ( swasserverbrauch + 1 )

   commandArray['Variable:wasserverbrauch'] = tostring(swasserverbrauch)

  

   commandArray['OpenURL']='http://192.168.178.56:8080/json.htm?type=command&param=udevice&idx=118&svalue='..swasserverbrauch..''

end

return commandArray
I always get:

" Error executing script command (/home/pi/domoticz/scripts/lua/script_device_wasserverbrauch.lua). returned: 512
2017-09-02 18:27:47.238 Error: Error executing script command (/home/pi/domoticz/scripts/lua/script_device_wasserverbrauch.lua). returned: 512
2017-09-02 18:28:27.656 Error: Error executing script command (/home/pi/domoticz/scripts/lua/script_device_wasserverbrauch.lua). returned: 512
2017-09-02 19:14:27.871 Error: Error executing script command (/home/pi/domoticz/scripts/lua/script_device_wasserverbrauch.lua). returned: 512
2017-09-02 19:14:35.266 Error: Error executing script command (/home/pi/domoticz/scripts/lua/script_device_wasserverbrauch.lua). returned: 512

Can someone please help me ?
Thanks

Re: Lua Code problem

Posted: Saturday 02 September 2017 20:16
by zicht
You could try using tostring(wasservebrauch) for openurl as you add an integer to a sting what does not
work

Second do you have password security enabled? Then you should add password string ( base64)

Hope it points you in the right direction

Re: Lua Code problem

Posted: Saturday 09 September 2017 23:12
by Amsterdam020
Hi,

I try to get my first lua script to work but it fails. Can somebody give a hint how to get it working?

Code: Select all

os.execute('curl -s -H "Accept: application/json" -X PUT --data \'{"on":true, "sat":'254', "bri":'69',"hue":'0'}\'http://192.168.1.xx/api/my_api_key_here/lights/5/state')
2017-09-09 23:08:04.198 Error: Error executing script command (/home/pi/domoticz/scripts/lua/hue_light5.lua). returned: 512

I do not use a password and i believe there is also no error in the url as is mentioned in the soluation menetioned earlier.

I am grateful for your answers!

Re: Lua Code problem

Posted: Sunday 10 September 2017 9:02
by Amsterdam020
Still stuggeling with my first lua script

On Action url: script:///lua/hue_light5.lua
2017-09-10 08:51:31.937 User: Admin initiated a switch command (670/huedummy/On)
2017-09-10 08:51:31.939 Error: SQLHelper: Error script not found '/lua/hue_light5.lua'
2017-09-10 08:51:31.946 (dummyhue) Lighting 1 (huedummy)

Off action url script://lua/hue_light5.lua (note same file but with only two slashes)
2017-09-10 08:51:33.878 User: Admin initiated a switch command (670/huedummy/Off)
2017-09-10 08:51:33.887 (dummyhue) Lighting 1 (huedummy)
2017-09-10 08:51:34.117 Executing script: /home/pi/domoticz/scripts/lua/hue_light5.lua
2017-09-10 08:51:34.130 Error: Error executing script command (/home/pi/domoticz/scripts/lua/hue_light5.lua). returned: 512

So i believe dubble slash is right. Why do i get an error 512 code and how to solve this error?

Re: Lua Code problem

Posted: Sunday 10 September 2017 14:28
by simonrg
Amsterdam020 wrote: Sunday 10 September 2017 9:02 On Action url: script:///lua/hue_light5.lua
Off action url script://lua/hue_light5.lua (note same file but with only two slashes)
As you are running on a Raspberry Pi:
Then 3 slashes is giving an absolute path (the 3rd / slash being the root) and should really be used i.e. /home/pi/domoticz/scripts/lua/hue_light5.lua
Then 2 slashes is a relative path so ./lua/hue_light5.lua - the running path may change so you would be better to use the fully qualified path.

While this is a Lua script you have written it is not a Lua Script that is run by Domoticz, the On Action is a way in Domoticz to get a script run at the command line, the command line running is Bash, so it will expect a Bash script - which this isn't and it also hasn't been made executable hence the 512 error.

I suggest on the you create a Bash script,

Code: Select all

curl -s -H "Accept: application/json" -X PUT --data '{"on":true, "sat":'254', "bri":'69',"hue":'0'}'http://192.168.1.xx/api/my_api_key_here/lights/5/state'
in a Bash directory and make the script executable. First test the exact command on the command line as I am not sure your use of quotes will work as stated.

Code: Select all

cd ~
cd domoticz/scripts
mkdir bash
nano hue_light5.sh
chmod +x hue_light5.sh

On Action: ///home/pi/domoticz/scripts/bash/hue_light5.sh

You should also test your lua script by running it outside Domoticz, by running the lua interpreter (command lua) - you will find that your quotes probably need adjustment.

Code: Select all

os.execute('curl -s -H "Accept: application/json" -X PUT --data \'{"on":true, "sat":'254', "bri":'69',"hue":'0'}\'http://192.168.1.xx/api/my_api_key_here/lights/5/state')

Re: Lua Code problem

Posted: Sunday 10 September 2017 21:37
by Amsterdam020
Thank you Simonrg y for all your advice. I got it working now with your instruction and i learned a lot in the meantime.

btw, changed the quotes to the following.

Code: Select all

#!/bin/bash
curl -H "Accept: application/json" -X PUT --data '{"on":true,"bri":4,"hue":1}' http://192.168.1.xxx/api/my_key/lights/5/state