Error: attempt to index global 'domoticz' (a nil value)  [Solved]

Easy to use, 100% Lua-based event scripting framework.
Post Reply
jeroenb42
Posts: 8
Joined: Wednesday 09 May 2018 10:36
Target OS: Raspberry Pi
Domoticz version:
Contact:

Error: attempt to index global 'domoticz' (a nil value)

Post by jeroenb42 » Thursday 28 June 2018 10:06

It's going to be something completely silly but I can't figure it out, nor can I Google the answer.

I created a global_data.lua file in the domoticz/scripts/dzVents/scripts directory. I know that Domoticz finds it because it contained a typo which showed up in the log. Fixed the typo to give this result:

Code: Select all

return {
  helpers = {
    urlEncode = function (str)
      if (str) then
        str = string.gsub (str, "\n", "\r\n")
        str = string.gsub (str, "([^%w %-%_%.%~])",
          function (c) return string.format ("%%%02X", string.byte(c)) end)
        str = string.gsub (str, " ", "+")
      end
      return str
    end
  }
}
Very simple to start with. Next I want to use that function but I can't figure it out. According to the documentation I should use:

Code: Select all

domoticz.helpers.urlEncode("test test")
but I get an error:

Code: Select all

Error: EventSystem: in Button1: [string "--..."]:10: attempt to index global 'domoticz' (a nil value)
I tried it without domoticz. or even without domoticz.helpers. but nothing works. I read that you should pass the domoticz variable if you need it but in this case it is a simple string manipulation. What am I doing wrong?

My full code (device trigger):

Code: Select all

--
-- Button1
--

commandArray = {}

for deviceName,deviceValue in pairs(devicechanged) do
    if (deviceName=="Button1" and deviceValue~="Off") then
        if (deviceValue=="Click") then
            print(domoticz.helpers.urlEncode("test test"))
        elseif (deviceValue=="Double Click") then
        elseif (deviceValue=="Long Click") then
        elseif (deviceValue=="Long Click Release") then
        end
        
        -- reset button
        commandArray[#commandArray + 1] = {["UpdateDevice"] = "8|Off|0"}
    end
end

return commandArray

elmortero
Posts: 237
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Error: attempt to index global 'domoticz' (a nil value)

Post by elmortero » Thursday 28 June 2018 11:52

Can't find the info right now but if I am not mistaken you also need to pass domoticz as 1st value.
Try if changing urlEncode = function (str) to
urlEncode = function (domoticz, str) in helpers and
domoticz.helpers.urlEncode("test test") to
domoticz.helpers.urlEncode(domoticz, "test test") in your script solves it.

jeroenb42
Posts: 8
Joined: Wednesday 09 May 2018 10:36
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Error: attempt to index global 'domoticz' (a nil value)

Post by jeroenb42 » Thursday 28 June 2018 12:01

elmortero wrote:
Thursday 28 June 2018 11:52
Can't find the info right now but if I am not mistaken you also need to pass domoticz as 1st value.
Try if changing urlEncode = function (str) to
urlEncode = function (domoticz, str) in helpers and
domoticz.helpers.urlEncode("test test") to
domoticz.helpers.urlEncode(domoticz, "test test") in your script solves it.
Thanks for the suggestion. I just tried it but I still get the same error.

Andyf66
Posts: 49
Joined: Wednesday 13 June 2018 12:13
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Error: attempt to index global 'domoticz' (a nil value)

Post by Andyf66 » Thursday 28 June 2018 12:24

The problem is where you are calling the function, not the function itself. You need to show more of the calling code for us to see if ‘domticz’ is actually in scope?

The problem cannot be in the helper function as you dont appear to even reference ‘domoticz’ there?

dannybloe
Posts: 1481
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: Error: attempt to index global 'domoticz' (a nil value)  [Solved]

Post by dannybloe » Thursday 28 June 2018 12:29

You are mixing two worlds of scripting. Your trigger code for Button1 is classic Lua and not dzVents. There is no domoticz object in classic Lua there. Rewrite your trigger code using dzVents and it should work.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

elmortero
Posts: 237
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Error: attempt to index global 'domoticz' (a nil value)

Post by elmortero » Thursday 28 June 2018 12:52

dannybloe wrote:
Thursday 28 June 2018 12:29
Your trigger code for Button1 is classic Lua and not dzVents
I totally overlooked they one.. :roll:

jeroenb42
Posts: 8
Joined: Wednesday 09 May 2018 10:36
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Error: attempt to index global 'domoticz' (a nil value)

Post by jeroenb42 » Thursday 28 June 2018 13:15

Ah, thanks for that. Did not realize there was a LUA with and without dzVents.
Followed this thread: viewtopic.php?t=7642
and got what I wanted, global functions.
Thanks all

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests