Page 1 of 1

can I find script name in LUA code?

Posted: Saturday 21 January 2023 12:05
by manjh
Is there a way to use the name of the current script in LUA, so I can use it in logging?
I know I can do it "hard coded", but it would be more elegant if there is a way to pull it...

Re: can I find script name in LUA code?

Posted: Monday 06 February 2023 9:21
by manjh
any ideas? Is there a simple way to use the script name in the LUA code?

Re: can I find script name in LUA code?

Posted: Monday 06 February 2023 11:31
by lost
manjh wrote: Monday 06 February 2023 9:21 any ideas? Is there a simple way to use the script name in the LUA code?
Hello,

A quick search shows this possibility using a Lua debug module, if available in Lua interpreter build included with Domoticz?
https://stackoverflow.com/questions/214 ... rrent-file

But looks there is no luck as $0 in bash, __FILE in python or __FILE__ etc even here in good old C. Lua just misses this common feature builtin.

Re: can I find script name in LUA code?

Posted: Monday 06 February 2023 13:21
by Doler
I use:

Code: Select all

local SCRIPT_NAME = string.gsub(string.match(debug.getinfo(1, 'S').short_src, "[^/]+$"), '.lua', '')
in all my scripts.

Re: can I find script name in LUA code?

Posted: Monday 06 February 2023 13:40
by plugge
@Doler Nice! Very useful, thanks!

Re: can I find script name in LUA code?

Posted: Monday 06 February 2023 14:40
by mgugu
Doler wrote: Monday 06 February 2023 13:21 I use:

Code: Select all

local SCRIPT_NAME = string.gsub(string.match(debug.getinfo(1, 'S').short_src, "[^/]+$"), '.lua', '')
in all my scripts.
Excellent find !

Re: can I find script name in LUA code?

Posted: Tuesday 07 February 2023 9:10
by manjh
lost wrote: Monday 06 February 2023 11:31
manjh wrote: Monday 06 February 2023 9:21 any ideas? Is there a simple way to use the script name in the LUA code?
Hello,

A quick search shows this possibility using a Lua debug module, if available in Lua interpreter build included with Domoticz?
https://stackoverflow.com/questions/214 ... rrent-file

But looks there is no luck as $0 in bash, __FILE in python or __FILE__ etc even here in good old C. Lua just misses this common feature builtin.
Yes...

Re: can I find script name in LUA code?

Posted: Tuesday 07 February 2023 9:12
by manjh
Doler wrote: Monday 06 February 2023 13:21 I use:

Code: Select all

local SCRIPT_NAME = string.gsub(string.match(debug.getinfo(1, 'S').short_src, "[^/]+$"), '.lua', '')
in all my scripts.
Good tip, thank you. Although it does not really give me the actual name of the script, but rather the contents of the very first line of the script.
I could add your line of code to each script, but I might as well hard code the name as first LUA statement into SCRIPT_NAME.
Anyway, it gives me a way forward. Thanks.