can I find script name in LUA code?

Moderator: leecollings

Post Reply
manjh
Posts: 748
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

can I find script name in LUA code?

Post 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...
Hans
manjh
Posts: 748
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: can I find script name in LUA code?

Post by manjh »

any ideas? Is there a simple way to use the script name in the LUA code?
Hans
lost
Posts: 660
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: can I find script name in LUA code?

Post 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.
Doler
Posts: 147
Joined: Friday 31 July 2015 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Sint-Oedenrode, Netherlands
Contact:

Re: can I find script name in LUA code?

Post 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.
Mark: Domoticz Beta on Raspberry Pi 4 running Debian Bookworm - Z-Stick 7 - RFXCom - P1 - MySensors - SolarEdge - Dahua - Philips Hue - Docker - Zigbee2mqtt (plugin) - Zwave-js-ui - dzVents - Nodered
plugge

Re: can I find script name in LUA code?

Post by plugge »

@Doler Nice! Very useful, thanks!
mgugu
Posts: 218
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: can I find script name in LUA code?

Post 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 !
manjh
Posts: 748
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: can I find script name in LUA code?

Post 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...
Hans
manjh
Posts: 748
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: can I find script name in LUA code?

Post 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.
Hans
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest