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

Moderator: leecollings

Post Reply
DreaMare
Posts: 26
Joined: Friday 03 February 2017 16:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
Last edited by DreaMare on Monday 20 February 2017 18:24, edited 1 time in total.
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

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

Post 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
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
zak45
Posts: 953
Joined: Sunday 22 January 2017 11:37
Target OS: Windows
Domoticz version: V2024.4
Contact:

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

Post 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
DreaMare
Posts: 26
Joined: Friday 03 February 2017 16:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

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

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
DreaMare
Posts: 26
Joined: Friday 03 February 2017 16:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by DreaMare »

ok thank you : )
I'll try to check it
DreaMare
Posts: 26
Joined: Friday 03 February 2017 16:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

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

Post 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.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
DreaMare
Posts: 26
Joined: Friday 03 February 2017 16:45
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by DreaMare »

thanks a lot for your tips, I check it, hope this will be ok : )

thank's again !
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest