Page 1 of 1

bad argument #1 to 'sub' (string expected, got nil)

Posted: Sunday 19 February 2017 22:02
by DreaMare
Hello,

I got this error on this script, is someone can help me ?
2017-02-19 22:00:00.128 Error: EventSystem: in script_device_cube.lua: [string "-- script_device_cube.lua..."]:5: attempt to index global 'devicechanged' (a nil value)

=> 2017-02-20 18:16:37.635 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_porte2.lua: /home/pi/domoticz/scripts/lua/script_device_porte2.lua:6: bad argument #1 to 'sub' (string expected, got nil)

Thank you !

Code: Select all

-- script_device_porte2.lua

function lastSeen(device)
   t1 = os.time()
   s = otherdevices_lastupdate[device]
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   return (os.difftime (t1, t2))
   
end

commandArray = {}

local porteOverte = devicechanged['Xiaomi Door Sensor'] == 'Open'
local pieceVide = lastSeen('Motion entrée') > '5' and lastSeen('Motion Salon') > '5'
local pieceNoire = otherdevices['Lux Salon'] < '10' and otherdevices['Lux entrée']  < '10'
    
if (porteOuverte and pieceVide and pieceNoire) then
    
    commandArray['Scene Détente'] = "on"

end

return commandArray

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Sunday 19 February 2017 23:09
by simonrg
The name of script that has the error is not the same script as you list in your question.

However, your script will be called every time any device changes, so devicechanged['Xiaomi Door Sensor'] will be nil, unless 'Xiaomi Door Sensor' is the device which has changed.

You need an extra devicechanged test so you only do a test when 'Xiaomi Door Sensor' has changed - lua treats nil as false, any other value as true, so simple if devicechanged['Xiaomi Door Sensor'] suffices. on also needs a capital O, I think.

Code: Select all

-- script_device_porte2.lua

function lastSeen(device)
   t1 = os.time()
   s = otherdevices_lastupdate[device]
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   return (os.difftime (t1, t2))
end

commandArray = {}
if devicechanged['Xiaomi Door Sensor'] then

    local porteOverte = devicechanged['Xiaomi Door Sensor'] == 'Open'
    local pieceVide = lastSeen('Motion entrée') > '5' and lastSeen('Motion Salon') > '5'
    local pieceNoire = otherdevices['Lux Salon'] < '10' and otherdevices['Lux entrée']  < '10'

    if (porteOuverte and pieceVide and pieceNoire) then
   
        commandArray['Scene Détente'] = "On"

    end
    
end

return commandArray

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Sunday 19 February 2017 23:24
by zak45
and maybe that :

local porteOverte = devicechanged['Xiaomi Door Sensor'] == 'Open'
local pieceVide = lastSeen('Motion entrée') > '5' and lastSeen('Motion Salon') > '5'
local pieceNoire = otherdevices['Lux Salon'] < '10' and otherdevices['Lux entrée'] < '10'

if (porteOuverte and pieceVide and pieceNoire) then

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Monday 20 February 2017 18:23
by DreaMare
Thanks for reply,

sorry wrong past for the error, this is the real error :
2017-02-20 18:16:37.635 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_porte2.lua: /home/pi/domoticz/scripts/lua/script_device_porte2.lua:6: bad argument #1 to 'sub' (string expected, got nil)

thank's for the tip

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Monday 20 February 2017 20:09
by jvdz
It would mean that one of these 2 devices do not exist:
lastSeen('Motion entrée') > '5' and lastSeen('Motion Salon') > '5'

That would/could case that error.

Jos

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Tuesday 21 February 2017 12:11
by DreaMare
ok thank you : )
I'll try to check it

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Thursday 23 February 2017 21:13
by DreaMare
Motion entrée Motion Salon exist, they are my PIR, does it work with '5' to say "at least 5 min PIR is off' ?

i Need an empty room so I want my PIR motion are not active (at least 5min with no mouvement), what is the right line to write ?

Thank you

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Thursday 23 February 2017 21:45
by simonrg
lastSeen returns time in seconds, so your comparisons should be for 300 and 600, not 5 and 10, as numbers not as strings.
You could use tonumber for the other comparisons.
Check that the spelling of all your devices is correct, upper and lower case are correct, then add print statements to spot where things go wrong.

Code: Select all

-- script_device_porte2.lua

function lastSeen(device)
   t1 = os.time()
 print('What is device called: '..device)
   s = otherdevices_lastupdate[device]
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   return (os.difftime (t1, t2))
end

commandArray = {}
if devicechanged['Xiaomi Door Sensor'] then

    local porteOuverte = devicechanged['Xiaomi Door Sensor'] == 'Open'
    local pieceVide = lastSeen('Motion entrée') > 300 and lastSeen('Motion Salon') > 300
    local pieceNoire = tonumber(otherdevices['Lux Salon']) < 10 and tonumber(otherdevices['Lux entrée']  < 600)

    if (porteOuverte and pieceVide and pieceNoire) then
   
        commandArray['Scene Détente'] = "On"

    end
   
end

return commandArray
If you are then still having problems, include the whole script as a code block and the exact error, as differences in line numbr make it difficult to track down your problem.

Re: bad argument #1 to 'sub' (string expected, got nil)

Posted: Thursday 23 February 2017 22:24
by DreaMare
thanks a lot for your tips, I check it, hope this will be ok : )

thank's again !