Page 1 of 1

dzVents Using a Function From Another Event

Posted: Saturday 09 September 2017 4:14
by dpcreel
I currently have several dzVents events in Domoticz. I have an identical function in two events called getMinutes() which gets me the current time of the day in minutes. The first one I forgot to pass the domoticz time object into it properly, the second was done correctly. When the first event ran and used the function it created an error that came from the other script with the same function. The first event is called BT Occupancy, the second event is called Garage dZ.

Function:

Code: Select all

function getMinutes(dTime)
    local raw = dTime.rawTime
    local a = {}
    local i = 0
    for v in string.gmatch(raw, "%d+") do
        a[i] = tonumber(v)
        i = i + 1
    end
    local min = a[1] + a[0] * 60
    return min
end
dTime is the domoticz time object

Log:
2017-09-07 16:16:48.705 Error: dzVents: Error: An error occured when calling event handler BT Occupancy
2017-09-07 16:16:48.705 Error: dzVents: Error: ...domoticz/scripts/dzVents/generated_scripts/Garage dZ.lua:5: bad argument #1 to 'gmatch' (string expected, got nil)

Notice the first line, there is no explanation of what the error is! The second one looks like a normal error.

I suspected that dzVents was sharing or somehow looking at functions by their name and also looking outside the event (there are no helper functions) and into other events somehow, so I re-named one of the functions and its variables and the errors went away. I am running the latest beta version of domoticz on a PI3.

Am I off track?

Re: dzVents Using a Function From Another Event

Posted: Saturday 09 September 2017 10:30
by dannybloe
Well, that's what you get if you declare stuff in the global namespace. Hence my advice: always declare everything locally:

Code: Select all

local function getMinutes()
end

local myVar = 'bla'
Even if you need a variable inside your code somewhere!! Use local. You have no idea what is already loaded and defined. When dzVents starts it loads all available modules to see if it needs to be executed. If a module declares something in the global space (not using local) then things may get nasty as it is immediately added and all modules will be affected by it. So: don't!

Did I say you have to use local everywhere??

That said, why create that function in the first place? Just use domoticz.time.min.

Re: dzVents Using a Function From Another Event

Posted: Saturday 09 September 2017 15:06
by dpcreel
My bad. I was aware of the local variable declaration but not the local function. Thanks. Domoticz.time.min doesn't work here because it returns the current minute of the hour, I need the current minute of the day. I am comparing the current minute of the day to domoticz.time.sunriseInMinutes which returns in minutes of the day.

This is another garage door event. I want certain things to happen if the time is between sunset - 60 and sunrise + 60

Here is a section where the function is called:

Code: Select all

        elseif domoticz.devices('Lights Group').bState
            and (getMinutes(domoticz.time) >= domoticz.time.sunsetInMinutes - 60 or getMinutes(domoticz.time) <= domoticz.time.sunriseInMinutes + 60) then
                    domoticz.setScene('House Lights','On')
                    domoticz.log('House lights activated',domoticz.LOG_INFO)
Am I doing something wrong? Is there another way to get the same result?

Re: dzVents Using a Function From Another Event

Posted: Saturday 09 September 2017 15:21
by dannybloe
What about (domoticz.time.secondsSinceMidnight / 60)?

Re: dzVents Using a Function From Another Event

Posted: Saturday 09 September 2017 15:45
by dpcreel
I just ran its return to the log, secondsSinceMidnight should work. I'll give it a try and thanks for your input, you've always been a big help.

Re: dzVents Using a Function From Another Event

Posted: Monday 24 June 2024 15:58
by Nateonas
Or domoticz.time.secondsSinceMidnight // 60 for integer division. Just the whole minutes, no need to round down.
Or domoticz.time.minutesSinceMidnight :)