dzVents Using a Function From Another Event

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

Moderator: leecollings

Post Reply
dpcreel
Posts: 44
Joined: Friday 25 November 2016 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

dzVents Using a Function From Another Event

Post 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?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents Using a Function From Another Event

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dpcreel
Posts: 44
Joined: Friday 25 November 2016 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents Using a Function From Another Event

Post 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?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents Using a Function From Another Event

Post by dannybloe »

What about (domoticz.time.secondsSinceMidnight / 60)?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
dpcreel
Posts: 44
Joined: Friday 25 November 2016 14:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents Using a Function From Another Event

Post 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.
User avatar
Nateonas
Posts: 6
Joined: Wednesday 20 June 2018 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: NL
Contact:

Re: dzVents Using a Function From Another Event

Post by Nateonas »

Or domoticz.time.secondsSinceMidnight // 60 for integer division. Just the whole minutes, no need to round down.
Or domoticz.time.minutesSinceMidnight :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest