Page 1 sur 2

attempt to concatenate a nil value

Publié : 24 nov. 2019, 21:27
par damolc
bonjour
j ai fait un script Lua pour récupérer ma consomation de gaz sur un eco compteur Legrand
je récupère un fichier Json de type :

Code : Tout sélectionner

2019-11-24 21:19:00.407 "data2":141.000000,
2019-11-24 21:19:00.407 "data3":96.000000,
2019-11-24 21:19:00.407 "data4":0.000000,
2019-11-24 21:19:00.407 "data5":0.000000,
2019-11-24 21:19:00.407 "data6":0.000000,
2019-11-24 21:19:00.407 "data6m3":0.000000,
2019-11-24 21:19:00.407 "data7":25.421001,
2019-11-24 21:19:00.407 "data7m3":25.421001,
2019-11-24 21:19:00.407 "heure":21,
2019-11-24 21:19:00.407 "minute":18,
2019-11-24 21:19:00.407 "CIR1_Nrj":0.000000,
2019-11-24 21:19:00.407 "CIR1_Vol":0.000000,
2019-11-24 21:19:00.407 "CIR2_Nrj":0.000000,
2019-11-24 21:19:00.407 "CIR2_Vol":0.000000,
2019-11-24 21:19:00.407 "CIR3_Nrj":0.000000,
2019-11-24 21:19:00.407 "CIR3_Vol":0.000000,
2019-11-24 21:19:00.407 "CIR4_Nrj":0.000000,
2019-11-24 21:19:00.407 "CIR4_Vol":0.000000,
2019-11-24 21:19:00.407 "Date_Time":1574630339
2019-11-24 21:19:00.407 }
2019-11-24 21:19:00.407 Status: LUA: 25.421001,
ma consomation de gaz est 25.421 m3
mon script :

Code : Tout sélectionner

local url = 'http://192.168.1.46/inst.json' -- url locale de votre écocompteur legrand
gazidx = otherdevices_idx ["Gaz"]

local function removeZeros(Id)
while true do
if Id:sub(1,1) == '0' then
Id = Id:sub(2)
else
break
end
end
return Id
end

----------------------------------------------
-- Fin de la partie configuration --
----------------------------------------------

commandArray={}

--import des fontions pour lire le JSON
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
--recuperation des mesure  sur data.json
local config=assert(io.popen("curl '"..url.."'"))
local location = removeZeros(config:read('*all'))
local trouve = string.find(location,'"data7"')
local consoGaz = removeZeros(string.sub(location,trouve+8,trouve +17))


if Debug == "YES" then
print(trouve)
print(location)
print(consoGaz)

end

commandArray[gazidx] = { ['UpdateDevice'] = gazidx ..'|0|'.. consoGaz }

return commandArray
mais j ai une erreur sur le log et mon device ( RFXMeter) non mis a jour :

Code : Tout sélectionner

2019-11-24 21:24:00.460 Error: EventSystem: in script_time_Gaz: [string "Debug = "YES" -- Turn debugging on ("YES") or..."]:44: attempt to concatenate a nil value (global 'gazidx')
je doit faire une erreur sur : commandArray[gazidx] = { ['UpdateDevice'] = gazidx ..'|0|'.. consoGaz }
Pourriez vous m aider ?

Re: attempt to concatenate a nil value

Publié : 24 nov. 2019, 22:13
par lmet
Salut,
Le message d'erreur semble être à la ligne "44" alors que le script que tu envoies ne comporte que 39 lignes !

Re: attempt to concatenate a nil value

Publié : 24 nov. 2019, 22:36
par damolc
oui tu as raison voila le script complet :

Code : Tout sélectionner

Debug = "YES" -- Turn debugging on ("YES") or off ("NO")

----------------------------------------------
------ Configuration ------
----------------------------------------------

--domoticz
local url = 'http://192.168.1.46/inst.json' -- url locale de votre écocompteur legrand
gazidx = otherdevices_idx ["Gaz"]

local function removeZeros(Id)
while true do
if Id:sub(1,1) == '0' then
Id = Id:sub(2)
else
break
end
end
return Id
end

----------------------------------------------
-- Fin de la partie configuration --
----------------------------------------------

commandArray={}

--import des fontions pour lire le JSON
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
--recuperation des mesure  sur data.json
local config=assert(io.popen("curl '"..url.."'"))
local location = removeZeros(config:read('*all'))
local trouve = string.find(location,'"data7"')
local consoGaz = removeZeros(string.sub(location,trouve+8,trouve +17))


if Debug == "YES" then
print(trouve)
print(location)
print(consoGaz)

end

commandArray[gazidx] = { ['UpdateDevice'] = gazidx ..'|0|'.. consoGaz }

return commandArray

Re: attempt to concatenate a nil value

Publié : 24 nov. 2019, 23:05
par damolc
Peut-être le fait que le nombre recupéré n est pas entier ?

Re: attempt to concatenate a nil value

Publié : 24 nov. 2019, 23:14
par vil1driver
Un espace de trop avant [

Code : Tout sélectionner

gazidx = otherdevices_idx ["Gaz"]

Re: attempt to concatenate a nil value

Publié : 24 nov. 2019, 23:29
par damolc
Merci
J ai corrigé mais toujours la même erreur :oops:

Re: attempt to concatenate a nil value

Publié : 25 nov. 2019, 01:53
par lmet
C'est que "Gaz" n'existe pas ... Ce n'est pas "gaz" par exemple ...

Re: attempt to concatenate a nil value

Publié : 25 nov. 2019, 05:16
par denis_brasseur

Code : Tout sélectionner

commandArray[gazidx] = { ['UpdateDevice'] = gazidx ..'|0|'.. consoGaz }
C'est bien gazidx qui pose problème, mais peux etre pas la ou on l'imagine.
Que vient faire gazidx dans commandArray[gazidx] ?
Essaye plutot

Code : Tout sélectionner

commandArray[#commandArray+1]={['UpdateDevice'] = gazidx ..'|0|'..consoGaz}
ou bien

Code : Tout sélectionner

commandArray['UpdateDevice'] = gazidx .. '|0|' .. consoGaz 

Re: attempt to concatenate a nil value

Publié : 25 nov. 2019, 07:51
par damolc
C était bien une majuscule tout bêtement
Merci

Re: attempt to concatenate a nil value

Publié : 25 nov. 2019, 20:15
par denis_brasseur
damolc a écrit : 25 nov. 2019, 07:51 C était bien une majuscule tout bêtement
Merci
Je ne comprend pas, si il s'agissait bien d'un majuscule, quelle est alors l'anomalie ?