How obtain the name of the current event? [Solved]

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

Moderator: leecollings

Post Reply
OldPensionado
Posts: 28
Joined: Wednesday 01 March 2017 9:07
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

How obtain the name of the current event? [Solved]

Post by OldPensionado »

Hi guys,
It is not a very disrupting question but I would like to have an answer. Oh yes, I searched this forum and the rest of internet but no luck so far.
I would like to know the name of the event which is running. This offers the opportunity to pass debug information in a very simple manner to my own DebugPrintEvent without typing the event name as a parameter.
I hope I've made myself clear enough.

Thanks in advance!
Last edited by OldPensionado on Wednesday 15 January 2025 18:43, edited 1 time in total.
RPi 1, model B, Rev. 2; RPi 3B+; RPi Zero; RPi 4B
Raspbian 11 (Bullseye)
Domoticz V2024.7

Well, such is life. And it's getting sucher and sucher...
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How obtain the name of the current event?

Post by waltervl »

Do you want to create a special DebugPrintEvent script that is debugging running dzvents events?
What do you want to debug? Is the debug functionality where you can set the debug level per script not enough?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
OldPensionado
Posts: 28
Joined: Wednesday 01 March 2017 9:07
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: How obtain the name of the current event?

Post by OldPensionado »

Gosh, you're fast!
I have already created a helper (named "MyDebugPrint") which prints the text which is passed in a parameter. And this helper only prints info into the logfile if a global variable (named "FlagDebugPrint") is true.
But I would like to pass the name of the event which is running too.
It could be something like:
MyDebugPrint(<RunningEvent.name>, 'Updating counters now...').
In this case I would have to define only the specific text ('Updating counters now').

This would be nice, though?
RPi 1, model B, Rev. 2; RPi 3B+; RPi Zero; RPi 4B
Raspbian 11 (Bullseye)
Domoticz V2024.7

Well, such is life. And it's getting sucher and sucher...
FlyingDomotic
Posts: 328
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: How obtain the name of the current event?

Post by FlyingDomotic »

If I correctly understand, idea may be getting current script name, in order to have a generic print/log function using the script name without getting it as parameter.

Something like "scriptName = debug.getinfo(1,'S')" or equivalent, but working only under certain conditions
OldPensionado
Posts: 28
Joined: Wednesday 01 March 2017 9:07
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: How obtain the name of the current event?

Post by OldPensionado »

Thanks a lot, I will try this tomorrow!
RPi 1, model B, Rev. 2; RPi 3B+; RPi Zero; RPi 4B
Raspbian 11 (Bullseye)
Domoticz V2024.7

Well, such is life. And it's getting sucher and sucher...
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How obtain the name of the current event?

Post by waltervl »

What is wrong with default Dzvents debug logging with global and script level log level options?
https://wiki.domoticz.com/DzVents:_next ... (optional)

See also the demo template in the event editor and added here as code:

Code: Select all

-- This example shows you how to work with various log levels
--
-- The script is triggered by a dummy switch called 'test'
--
-- when no log level is specified, the level is taken from the default application settings (dzVents)
--
-- when you specify LOG_STATUS as minimum log level, no logs will be displayed when using LOG_INFO or LOG_DEBUG
--
-- Order is: LOG_DEBUG, LOG_INFO, LOG_STATUS, LOG_ERROR
--
-- LOG_DEBUG will also cause Execution Info to be displayed (start/finished)
--
return {
	on = {
		devices = {
			'test'
		}
	},
	logging = {
		marker = 'log_script',
		level = domoticz.LOG_DEBUG
	},
	execute = function(domoticz, device)
		domoticz.log('INFO log line!', domoticz.LOG_INFO)
		domoticz.log('STATUS log line!', domoticz.LOG_STATUS)
		domoticz.log('ERROR log line!', domoticz.LOG_ERROR)
		domoticz.log('DEBUG log line!', domoticz.LOG_DEBUG)
	end
}
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
OldPensionado
Posts: 28
Joined: Wednesday 01 March 2017 9:07
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: How obtain the name of the current event?

Post by OldPensionado »

FlyingDomotic wrote: Tuesday 14 January 2025 21:56 If I correctly understand, idea may be getting current script name, in order to have a generic print/log function using the script name without getting it as parameter.

Something like "scriptName = debug.getinfo(1,'S')" or equivalent, but working only under certain conditions
Yes, that's what I would like to have.
RPi 1, model B, Rev. 2; RPi 3B+; RPi Zero; RPi 4B
Raspbian 11 (Bullseye)
Domoticz V2024.7

Well, such is life. And it's getting sucher and sucher...
OldPensionado
Posts: 28
Joined: Wednesday 01 March 2017 9:07
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: How obtain the name of the current event?

Post by OldPensionado »

waltervl wrote: Tuesday 14 January 2025 23:37 What is wrong with default Dzvents debug logging with global and script level log level options?
https://wiki.domoticz.com/DzVents:_next ... (optional)
The default debug logging options aren't wrong but in my view too abundant. And possible caused by my own history as programmer in Basic, various assembler languages and Pascal, I very much like to control the logging by myself.
That is why I am looking for a generic printlog option. Also I use a global variable FlagDebugPrint - controlled by a virtual switch - by which debug info is printed or not. This works for me faster and smoother than go to Setup > Settings > Other and finally press Apply Settings.
Last edited by OldPensionado on Wednesday 15 January 2025 7:31, edited 1 time in total.
RPi 1, model B, Rev. 2; RPi 3B+; RPi Zero; RPi 4B
Raspbian 11 (Bullseye)
Domoticz V2024.7

Well, such is life. And it's getting sucher and sucher...
mgugu
Posts: 217
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: How obtain the name of the current event?

Post by mgugu »

I use that for years :

Code: Select all

local scriptName = string.gsub(string.match(debug.getinfo(1, 'S').short_src, "[^/]+$"), '.lua', '')
OldPensionado
Posts: 28
Joined: Wednesday 01 March 2017 9:07
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: How obtain the name of the current event?

Post by OldPensionado »

Wow, fast response and briljant. It works like a charm.
Thank you @mgugu very much!
RPi 1, model B, Rev. 2; RPi 3B+; RPi Zero; RPi 4B
Raspbian 11 (Bullseye)
Domoticz V2024.7

Well, such is life. And it's getting sucher and sucher...
User avatar
waltervl
Posts: 5397
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: How obtain the name of the current event?

Post by waltervl »

OldPensionado wrote: Wednesday 15 January 2025 7:23
waltervl wrote: Tuesday 14 January 2025 23:37 What is wrong with default Dzvents debug logging with global and script level log level options?
https://wiki.domoticz.com/DzVents:_next ... (optional)
The default debug logging options aren't wrong but in my view too abundant. And possible caused by my own history as programmer in Basic, various assembler languages and Pascal, I very much like to control the logging by myself.
That is why I am looking for a generic printlog option. Also I use a global variable FlagDebugPrint - controlled by a virtual switch - by which debug info is printed or not. This works for me faster and smoother than go to Setup > Settings > Other and finally press Apply Settings.
But then you get debug for all running scripts....
I prefer to set global debug level to INFO (and never change it) and when I need debug in a script I change the debug level in that script....
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
FlyingDomotic
Posts: 328
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: How obtain the name of the current event?

Post by FlyingDomotic »

For those wanting, in addition to the script name, getting caller(s) line number, there's a small function returning the information:

Code: Select all

-- Returns source name(s) and line(s) of caller(s)
function getTraceback()
	local level = 2														-- Start with our caller
	local source = ""													-- Source name
	local trace = ""													-- Trace message
	while true do														-- Loop forever
		local info = debug.getinfo(level, "Sl")							-- Get level info
		if not info or info.currentline <= 0 then						-- No more level or Domoticz root level reached
			break														-- Exit loop
		end
		if source ~= info.short_src then								-- Any change in source name?
			source = info.short_src										-- Save it
			trace = trace .. ":" .. 									-- Add name to traceback
				string.gsub(string.match(source, "[^/]+$"), '.lua', '')
		end    
		trace = trace .. ":" .. info.currentline						-- Add line number
		level = level + 1												-- Increase level
	end
	return string.sub(trace,2)											-- Return trace except first character
FlyingDomotic
Posts: 328
Joined: Saturday 27 February 2016 0:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: How obtain the name of the current event?

Post by FlyingDomotic »

In addition, for those not using dzVents, but "classical" Domoticz LUA, here's a small function extracting comment of first line of script.

Code: Select all

-- Returns Domoticz (non dzVent) LUA first line
function luaSourceName(level)
    if level == nil then                                                -- You can specify stack level 
        level = 1                                                       -- Default to 1 if not given
    end
    index = 0
    source = debug.getinfo(level, "Sl").short_src                       -- Get source name (content for Domoticz)
    source = string.sub(source, 1, string.find(source.."\n", "\n") -1)  -- Keep only first line
    for str in string.gmatch(source, '([^"]+)') do                      -- Split source using double quote
        index = index + 1                                               -- Increment index
        if index == 2 then                                              -- Consider data at index 2
            if string.sub(str, 1, 3) == "-- " then                      -- Does string start with "-- "?
                source = string.sub(str, 4)                             -- Keep comment
                if string.sub(source, -3) == "..." then                 -- If source ends with "..."
                    source = string.sub(source, 1, -4)                  -- Remove them
                end
                return source                                           -- Return comment part
            end
        end
    end        
    return source                                                       -- Return first line (no comment found)
end
You just have to begin your script with

Code: Select all

-- script name
for the routine to return "script name"
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests