Error in LUA timer-event: "attempt to index global 'time"

Moderator: leecollings

Post Reply
HansR
Posts: 4
Joined: Monday 06 May 2019 19:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10705
Location: Netherlands
Contact:

Error in LUA timer-event: "attempt to index global 'time"

Post by HansR »

I have the code below in a timer event (name is Weather) which belongs to weatherstation (but is not in de linked thread). I reduced the code such that all comment and some unwanted code is taken out but I still get the following errors:
2019-05-09 08:19:00.444 Error: EventSystem: in Weather: [string " local domo_ip = "192.168.178.5:8080..."]:30: attempt to index global 'time' (a nil value)
And I really have no idea where to go from here.
Any clue what is happening and why this error occurs?

Code: Select all

    local domo_ip       = "192.168.178.5:8080"
    local weerstation   = 31                  
    local api_key       = "myKey"
    local debug         = 1
    local language      = "nl"

    local dev_DewPoint      = "Dauwpunt"
    local dev_CloudCover    = "Bewolkingsgraad"
    local dev_UV            = "UV"
    local dev_Ozone         = "Ozon"

    function UpdateDev(device,nvalue,svalues)
        print("UpdateDev "..device..'/'..nvalue..'/'..svalues)
        
        commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[device]..'|'..tostring(nvalue)..'|'..tostring(svalues)}
    end
    
    function round(num, dec)
        if num == 0 then
            return 0
        else
            local mult = 10^(dec or 0)
            return math.floor(num * mult + 0.5) / mult
        end
    end

    if  ((time.min+2)%5)==0  then
        json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
        local config=assert(io.popen('curl "http://'..domo_ip..'/json.htm?type=settings"'))
        local Stringjson = config:read('*all')
        config:close()
        local jsonData = json:decode(Stringjson)

        lat          = jsonData.Location.Latitude
        lon          = jsonData.Location.Longitude
        local coord = lat..","..lon
        print("Darksky function coordinates"..coord)
     
        json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
        local config=assert(io.popen('curl "https://api.darksky.net/forecast/'..api_key..'/'..coord..'?lang='..language..'&units=si&exclude=hourly,flags"'))
        local Stringjson = config:read('*all')
        config:close()
        local jsonData = json:decode(Stringjson)

        if jsonData ~= nil then
            val_UV          = jsonData.currently.uvIndex
            val_Ozone       = jsonData.currently.ozone
            val_DewPoint    = round(jsonData.currently.dewPoint,1)
            val_CloudCover  = jsonData.currently.cloudCover*100
        end
        
        if debug==1 then
            print("Bewolkingsgraad : "..val_CloudCover.. " %")
            print("UV-kracht :"..val_UV)
            print("Ozone : De ozone-waarde is "..val_Ozone) 
            print("Dauwpunt : "..val_DewPoint.. " °C")
        end
        
        UpdateDev(dev_CloudCover,0,val_CloudCover)
        UpdateDev(dev_DewPoint,0,val_DewPoint)
        UpdateDev(dev_UV,0,val_UV..";0")
        UpdateDev(dev_Ozone,0,val_Ozone)
     
        if debug==1 then
            print ("Dauwpunt: "..val_DewPoint.." °C")
            print ("Bewolkingsgraad: "..val_CloudCover.." %")
            print ("UV-Index: "..val_UV)
            print ("Ozone: "..val_Ozone)
        end
    end
Domoticz: 4.10705 / dzVents version: 2.4.19
System: Raspberry Pi 3B+ (with UPS PIco HV3.0A/B/B+ HAT)
OS: Raspbian (Linux raspberrypi 4.14.98-v7+ #1200 SMP)
Currently: 'Slimme Meter' (Sagemcom XS210 ESMR5); Dummy hardware for weatherstation;
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Error in LUA timer-event: "attempt to index global 'time"

Post by waaren »

HansR wrote: Thursday 09 May 2019 8:25 I have the code below in a timer event (name is Weather) which belongs to weatherstation (but is not in de linked thread). I reduced the code such that all comment and some unwanted code is taken out but I still get the following errors:
I don''t see in your code snippet where the Lua table 'time' is declared and filled. Somewhere in your code you should include something like

Code: Select all

local time = os.date("*t") 
The url you included points to a dzVents script.
dzVents is build on Lua using Lua code therefore you can include Lua code in dzVents scripts but you cannot use dzVents functions or methods in standard Lua. By the looks of it, it seems that you try to mix the two. Using standard Lua in domoticz requires a

Code: Select all

return  commandArray
to end the script
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
HansR
Posts: 4
Joined: Monday 06 May 2019 19:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10705
Location: Netherlands
Contact:

Re: Error in LUA timer-event: "attempt to index global 'time"

Post by HansR »

I modified the code according to your remarks (see new code below) but I keep getting:
2019-05-09 09:18:01.357 Error: EventSystem: in Weather: [string " local domo_ip = "192.168.178.5:8080..."]:15: attempt to get length of global 'commandArray' (a nil value)
And btw: maybe there is an uodate to the script I originally copied from here in which case I could go from there.

Code: Select all

    local domo_ip       = "192.168.178.5:8080"
    local weerstation   = 31                  
    local api_key       = "f338a6dd148e62699842f0f604cca3e8"
    local debug         = 1
    local language      = "nl"

    local dev_DewPoint      = "Dauwpunt"
    local dev_CloudCover    = "Bewolkingsgraad"
    local dev_UV            = "UV"
    local dev_Ozone         = "Ozon"

    function UpdateDev(device,nvalue,svalues)
        print("UpdateDev "..device..'/'..nvalue..'/'..svalues)
        
        commandArray[#commandArray+1] = {['UpdateDevice'] = otherdevices_idx[device]..'|'..tostring(nvalue)..'|'..tostring(svalues)}
    end
    
    function round(num, dec)
        if num == 0 then
            return 0
        else
            local mult = 10^(dec or 0)
            return math.floor(num * mult + 0.5) / mult
        end
    end

    time = os.date("*t")

    if  ((time.min+2)%5)==0  then
        json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
        local config=assert(io.popen('curl "http://'..domo_ip..'/json.htm?type=settings"'))
        local Stringjson = config:read('*all')
        config:close()
        local jsonData = json:decode(Stringjson)

        lat          = jsonData.Location.Latitude
        lon          = jsonData.Location.Longitude
        local coord = lat..","..lon
        print("Darksky function coordinates"..coord)
     
        json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
        local config=assert(io.popen('curl "https://api.darksky.net/forecast/'..api_key..'/'..coord..'?lang='..language..'&units=si&exclude=hourly,flags"'))
        local Stringjson = config:read('*all')
        config:close()
        local jsonData = json:decode(Stringjson)

        if jsonData ~= nil then
            val_UV          = jsonData.currently.uvIndex
            val_Ozone       = jsonData.currently.ozone
            val_DewPoint    = round(jsonData.currently.dewPoint,1)
            val_CloudCover  = jsonData.currently.cloudCover*100
        end
        
        if debug==1 then
            print("Bewolkingsgraad : "..val_CloudCover.. " %")
            print("UV-kracht :"..val_UV)
            print("Ozone : De ozone-waarde is "..val_Ozone) 
            print("Dauwpunt : "..val_DewPoint.. " °C")
        end
        
        UpdateDev(dev_CloudCover,0,val_CloudCover)
        UpdateDev(dev_DewPoint,0,val_DewPoint)
        UpdateDev(dev_UV,0,val_UV..";0")
        UpdateDev(dev_Ozone,0,val_Ozone)
     
        if debug==1 then
            print ("Dauwpunt: "..val_DewPoint.." °C")
            print ("Bewolkingsgraad: "..val_CloudCover.." %")
            print ("UV-Index: "..val_UV)
            print ("Ozone: "..val_Ozone)
        end
    end

    return  commandArray
Domoticz: 4.10705 / dzVents version: 2.4.19
System: Raspberry Pi 3B+ (with UPS PIco HV3.0A/B/B+ HAT)
OS: Raspbian (Linux raspberrypi 4.14.98-v7+ #1200 SMP)
Currently: 'Slimme Meter' (Sagemcom XS210 ESMR5); Dummy hardware for weatherstation;
HansR
Posts: 4
Joined: Monday 06 May 2019 19:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10705
Location: Netherlands
Contact:

Re: Error in LUA timer-event: "attempt to index global 'time"

Post by HansR »

OK, I found the solution, my devices are updated correctly.
Thnx for getting me on track.

The first line of the timer event must be:

Code: Select all

commandArray={}
The last line of the code must be:

Code: Select all

return commandArray
This is not self-evident for starters in Domoticz / Events coding in the absent of a starting / code templates location. Wouldn't that be a good idea? Or does it already exist in some hidden place?
Domoticz: 4.10705 / dzVents version: 2.4.19
System: Raspberry Pi 3B+ (with UPS PIco HV3.0A/B/B+ HAT)
OS: Raspbian (Linux raspberrypi 4.14.98-v7+ #1200 SMP)
Currently: 'Slimme Meter' (Sagemcom XS210 ESMR5); Dummy hardware for weatherstation;
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Error in LUA timer-event: "attempt to index global 'time"

Post by waaren »

HansR wrote: Thursday 09 May 2019 10:50 This is not self-evident for starters in Domoticz / Events coding in the absent of a starting / code templates location. Wouldn't that be a good idea? Or does it already exist in some hidden place?
It's there ! Just go to the event editor within domoticz, - choose a script type by pressing the plus, choose the trigger and you are good to go.

hidden ~= (ICanNotFindIT) :D
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
HansR
Posts: 4
Joined: Monday 06 May 2019 19:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10705
Location: Netherlands
Contact:

Re: Error in LUA timer-event: "attempt to index global 'time"

Post by HansR »

Found it. That really helps, thnx.
Making the first steps and finding your way is always the hardest of times :))
Domoticz: 4.10705 / dzVents version: 2.4.19
System: Raspberry Pi 3B+ (with UPS PIco HV3.0A/B/B+ HAT)
OS: Raspbian (Linux raspberrypi 4.14.98-v7+ #1200 SMP)
Currently: 'Slimme Meter' (Sagemcom XS210 ESMR5); Dummy hardware for weatherstation;
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest