Getting the script own name

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
jeanclic
Posts: 68
Joined: Saturday 28 January 2017 11:32
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France / Burgundy
Contact:

Getting the script own name

Post by jeanclic »

Hello everyone,

I am currently beginning to move all of my "standard" lua scripts to dzVents 2.2.0, and I am stuck on one thing : How get my script name...

For the story, I use a "personal" logging function, that I will use in dzVents too because I designed it to handle the "debug" mode for only one script, and not all of them as it is when you use the dzVents log level.

So I am using the helpers as explained in the wiki, in the "global_data.lua" file.

I would like to know if I can get the name of the script triggering this function, so I could write it on the log.

For now, I write in each script the following lines :

Code: Select all

local path = debug.getinfo(1,'S').short_src
while string.find(path, '/') do path = string.sub(path, string.find(path, '/')+1) end
local File = tostring(string.sub(path, 1, string.len(path)-4))
and then I use "File" as an argument when calling my function...

I have seen in "EventHelpers.lua" that the name of the module currently executed is coming from "eventHandler.name". Is there a way to use this directly inside of my own function ?
RPI3B (Domoticz 4.9700 + ~60 scripts) + RFXtrx433E (fw1020) + Z-Stick Gen5 + MySensors / RPI2B+ (RasPlex)
x1 Eth. wired tablet
x8 T/H Oregon / x1 T/H Bresser
x7 heaters / x2 QUBINO "Flush 1 relay"
x10 plugs / x15 contacts
x4 fibaro / x1 PIR
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Getting the script own name

Post by dannybloe »

There's nothing for this currently but why not pass a name to your helper function instead?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jeanclic
Posts: 68
Joined: Saturday 28 January 2017 11:32
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France / Burgundy
Contact:

Re: Getting the script own name

Post by jeanclic »

Hello Danny,
Thanks for answering so quickly !
I did not explained well, but I am actually passing the name (through my "File" variable) to my helper function. My concern is to remove those 3 lines that I put in each one of my scripts (so I do not care of miswriting the script name, or forgot to give a proper name each time I create a script).
I will keep it like that for now.

I have another question, maybe not in the good thread : is there a "middle" level of the scope of a variable ? -> when I used "standard" lua scripts, if I used a Global variable in 1 script, the next script did not keep the variable value. But, due to the total integration of the dzVents scripts in a single one, if I declare a Global in the first script, and re-declare it with another value in the last script, the first script will be executed with the var value set by the last one, and so the result is not the one expected in my code.
Knowing that, is there a way to declare a Global "Local" to only one script ?
RPI3B (Domoticz 4.9700 + ~60 scripts) + RFXtrx433E (fw1020) + Z-Stick Gen5 + MySensors / RPI2B+ (RasPlex)
x1 Eth. wired tablet
x8 T/H Oregon / x1 T/H Bresser
x7 heaters / x2 QUBINO "Flush 1 relay"
x10 plugs / x15 contacts
x4 fibaro / x1 PIR
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Getting the script own name

Post by dannybloe »

and how about setting a custom logging on the script itself. Isn't that enough? I'd say it does just that.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Getting the script own name

Post by dannybloe »

jeanclic wrote: Tuesday 15 August 2017 11:57 I have another question, maybe not in the good thread : is there a "middle" level of the scope of a variable ? -> when I used "standard" lua scripts, if I used a Global variable in 1 script, the next script did not keep the variable value. But, due to the total integration of the dzVents scripts in a single one, if I declare a Global in the first script, and re-declare it with another value in the last script, the first script will be executed with the var value set by the last one, and so the result is not the one expected in my code.
Knowing that, is there a way to declare a Global "Local" to only one script ?
That's exactly why you should never use globals in the first place because you never know if it causes undesired side-effects. It is considered as a very bad practice in programming. Always use local when defining variables in your script. Always.. unless you have a very very good reason to use globals.

My two cents...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jeanclic
Posts: 68
Joined: Saturday 28 January 2017 11:32
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France / Burgundy
Contact:

Re: Getting the script own name

Post by jeanclic »

dannybloe wrote: Tuesday 15 August 2017 12:27 and how about setting a custom logging on the script itself. Isn't that enough? I'd say it does just that.
Yes ! you are obviously right ! I'd see that but I wasn't gone far enough in my readings ! I will definitely change my habits fort this logging option !

OK for the scope of the variables : that is exactly how I see "best practice", but you know, between words and acts ... ;)
RPI3B (Domoticz 4.9700 + ~60 scripts) + RFXtrx433E (fw1020) + Z-Stick Gen5 + MySensors / RPI2B+ (RasPlex)
x1 Eth. wired tablet
x8 T/H Oregon / x1 T/H Bresser
x7 heaters / x2 QUBINO "Flush 1 relay"
x10 plugs / x15 contacts
x4 fibaro / x1 PIR
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest