Page 1 of 1

LUA On Action and Off Action tables?

Posted: Tuesday 07 February 2017 17:57
by emme
...Ok..
I understand the script name pattern and where to store them on the PI3 (/home/pi/dommoticz/scripts/lua)
I understand that:

script_device_whatever.lua run on ANY devicechange
script_time_whatever.lua run once a minute
script_variable_whatever.lua runs on ANY variable change
script_security_whatever.lua runs on ANY security event (...even if I did not yet well understood which are they)

but... what is I would need to:
Run a script ONLY on triggering a device ON/OFF (or a pushbutton)?
I assume I would use ON Action and OFF Action using prefix script:// but this doesn't work (error 512)... and by my [poor] understanding this would work only for windows and anyhow no tables (otherdevices, othersevices_lasupdate, devicechanged, uservariables) are passed to script.
So.. what shall I do in order to run just a script in case I trigger just one button in a specific state?
Wiki is not really clear about... it just specify to use script_device_ and trigger the correct otherdevices
thanks
ciao
M

Re: LUA On Action and Off Action tables?

Posted: Tuesday 07 February 2017 18:53
by Westcott
The usual way is to use a 'script_device_whatever.lua run on ANY devicechange'.
In the script, test for your devices by name to see which one has changed.
See script_device_demo.lua in the scripts/lua directory

Re: LUA On Action and Off Action tables?

Posted: Tuesday 07 February 2017 20:36
by emme
ok...it's confimed that there is no possibility to launch a specific script for a specific device unless using the On/Off Action but it does NOT forward the system tables (like otherdevices etc etc...)

Thanks for your clarification :)
ciao
M

Re: LUA On Action and Off Action tables?

Posted: Wednesday 08 February 2017 9:24
by Siewert308SW
Maybe i totally misunderstand you.
But you want to run a script only when a specific trigger is triggered?

If so, then it aint so hard.
Create a script_device_whatever.lua in /lua
Move all other device scripts outside the /lua folder

Let say you have moved "a_lua_script.lua" to "/home/pi/domoticz/scripts/lua_events/"
In this script something has to be triggered when opening the frontdoor.
Then do the following in "script_device_whatever.lua" which is the only file in "/home/pi/domoticz/scripts/lua/"

Code: Select all

-- Set Lua Path
	lua_path = "/home/pi/domoticz/scripts/lua_events/"
	
commandArray = {}

	if devicechanged['frontdoor'] then	
		dofile(lua_path.."a_lua_script.lua")	
	end	

return commandArray	
and for your "a_lua_script.lua"

Code: Select all

	if devicechanged['frontdoor'] == 'Open' and otherdevices['Light'] == 'Off' then	
		commandArray['Light']='On'	
	end
	
	if devicechanged['frontdoor'] == 'Closed' and otherdevices['Light'] == 'On' then	
		commandArray['Light']='Off'	
	end			
Be aware that in your "a_lua_script.lua" there is no "commandArray {}" and "return commandArray"
If it does contain and multiple scripts are triggered then your scripts will fail.
This way as seen above your main lua "script_device_whatever.lua" does the "commandArray {}" and "return commandArray"