Passing Argument/s to Lua Script

Moderator: leecollings

Post Reply
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Passing Argument/s to Lua Script

Post by Lorccan »

I have found that if I add a bash script to a switch I can pass argument/s when it's triggered: script:///<path>/script.sh arg

However, if I try the same pattern with a Lua script it doesn't seem to work. So my question is: should it be possible to pass an argument to a Lua script?

(I know how to handle the incoming arguments in bash and Lua, so I don't think that that's the issue, but I'll give up trying if it can't be done!)
Scones
Posts: 47
Joined: Wednesday 10 December 2014 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Passing Argument/s to Lua Script

Post by Scones »

I'm just bouncing this question as I want the same answer.

What I'm looking for is a more efficient way of triggering LUA scripts rather than a script_device_<whatever>.lua

It seems inefficient running every device script script every time any device state changes.

Is there a way to add a LUA to the 'On Script' and 'Off Script' so that it does the same but more efficiently?
jannl
Posts: 666
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Passing Argument/s to Lua Script

Post by jannl »

You could create a container devicescript which calls the individual devices script.
In this case you only need commandArray in the container script, which really speeds up the handling.

Why would you want to pass an argument to a LUA script?
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Passing Argument/s to Lua Script

Post by Lorccan »

I am the OP. My reason for wanting to pass an argument is, for example, to change a TV channel. I would have one script, but pass the channel as the argument. That's exactly what I can do with a bash script (in Domoticz) and Lua can take arguments usually (outside Domoticz), so I don't understand why it seems restricted here.
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Passing Argument/s to Lua Script

Post by Nautilus »

Lorccan wrote:I am the OP. My reason for wanting to pass an argument is, for example, to change a TV channel. I would have one script, but pass the channel as the argument. That's exactly what I can do with a bash script (in Domoticz) and Lua can take arguments usually (outside Domoticz), so I don't understand why it seems restricted here.
Have not tested but cannot see any reason it wouldn't accept. Trust you have added something like this to your script:

Code: Select all

local params = {...}
params[1] -- first parameter, if any.
params[2] -- second parameter, if any.
#params   -- number of parameters.
(not sure if it is exactly like this, first google result of "pass parameters to lua script" -> http://stackoverflow.com/questions/9778 ... he-lua-cli).

Also, I suggest looking into the Lua manual for stand alone usage, http://www.lua.org/manual/5.1/manual.html#6. Note the following:
..Lua has been designed as an extension language, to be embedded in a host C program..
What people often seem to forget is that the resources of the embedded program are not available in this kind of usage, like in this case the devicechanged, otherdevices etc.tables and passing commands directly to switches via commandArray. So I don't see any real benefit in using Lua in the on/off action scripts. Only in the event system when the resources of the host program are available.
jannl
Posts: 666
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Passing Argument/s to Lua Script

Post by jannl »

Lua scripts are alled in a different way in Domoticz, so it is not possible to pass arguments.

How do you determine to which channel to change and pass this value to another script?
Lorccan
Posts: 32
Joined: Wednesday 21 December 2016 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Passing Argument/s to Lua Script

Post by Lorccan »

OK. That answers the question: it is not possible to pass arguments to a Lua script in Domoticz.

I'll continue to use the bash script.

Thanks for your replies.
User avatar
Siewert308SW
Posts: 290
Joined: Monday 29 December 2014 15:47
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: Passing Argument/s to Lua Script

Post by Siewert308SW »

Setup:
- RPi4 - Domo Stable / Aeotec Z-stick7 / PiHole Unbound Gemini
- RPi4 - PiHole / PiVPN Unbound Gemini
- Synology DS923+ / DS218j
- P1 Gas/Power, SmartGateway watermeter
- Fibaro switches, contacts, plugs, smoke/Co2 ect
- rootfs @ USB HDD
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: Passing Argument/s to Lua Script

Post by Westcott »

As noted by Siewert308SW, all non-local variables in the calling script are available in the called script.
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest