Page 1 of 1

While Loop - Last true condition start

Posted: Saturday 22 September 2018 11:54
by Ascari
Hello I need help.

My loop while work but in this I have a if condition with a commandArray and the command is running only one not for every true condition.
I put a print to Debug and I can see multiple true but only the last run the commandArray.

Help :)

Code: Select all

commandArray = {}
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
-- Tableau Hue/Domotiz Exemple Idlamp Hue1 = idz108
lamp = {1,2,3,4,5,6,7,8} --Put your Hue lamp ID here
device = {108,109,110,111,112,113,114,115} --Put your Domotiz ID here
nblamp = 8 --Put here nbr lamp you want check

i = 1
while i <= nblamp 
do
	huelightid=lamp[i]
	domohueid=device[i]
	idjson = tostring(huelightid)
	urlid = tostring(domohueid) 
			--  API call
	local config=assert(io.popen('curl "http://IPHue/api/UserHue/lights/'..idjson..'"')) -- change Iphue and login hue
	local Stringjson = config:read('*all')
	config:close()
	local jsonData = json:decode(Stringjson)
	lampe = jsonData.name
	reachable = jsonData.state.reachable
			-- print (Stringjson)  -- debug json
	reachable = tostring(reachable)
			-- print('Hue Reachable Log: '..reachable..lampe) -- Test value for Debuging.
	if reachable == "false"
		then
		commandArray['OpenURL']='http://domoticzip:port/json.htm?type=command&param=switchlight&idx='..urlid..'&switchcmd=Off' -- Changedomoticzip and port
		print ('url : '..urlid) -- Log Debug Help
	end
i = i + 1
end
return commandArray

Re: While Loop - Last true condition start

Posted: Saturday 22 September 2018 12:24
by waaren
Ascari wrote: Saturday 22 September 2018 11:54 Hello I need help.

My loop while work but in this I have a if condition with a commandArray and the command is running only one not for every true condition.
I put a print to Debug and I can see multiple true but only the last run the commandArray.

Help :)

Code: Select all

commandArray = {}
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
-- Tableau Hue/Domotiz Exemple Idlamp Hue1 = idz108
lamp = {1,2,3,4,5,6,7,8} --Put your Hue lamp ID here
device = {108,109,110,111,112,113,114,115} --Put your Domotiz ID here
nblamp = 8 --Put here nbr lamp you want check

i = 1
while i <= nblamp 
do
	huelightid=lamp[i]
	domohueid=device[i]
	idjson = tostring(huelightid)
	urlid = tostring(domohueid) 
			--  API call
	local config=assert(io.popen('curl "http://IPHue/api/UserHue/lights/'..idjson..'"')) -- change Iphue and login hue
	local Stringjson = config:read('*all')
	config:close()
	local jsonData = json:decode(Stringjson)
	lampe = jsonData.name
	reachable = jsonData.state.reachable
			-- print (Stringjson)  -- debug json
	reachable = tostring(reachable)
			-- print('Hue Reachable Log: '..reachable..lampe) -- Test value for Debuging.
	if reachable == "false"
		then
		commandArray['OpenURL']='http://domoticzip:port/json.htm?type=command&param=switchlight&idx='..urlid..'&switchcmd=Off' -- Changedomoticzip and port
		print ('url : '..urlid) -- Log Debug Help
	end
i = i + 1
end
return commandArray
You are overwriting your own previous commandArray['OpenURL'] in the next iteration.
Use something like

Code: Select all

commandArray[#commandArray+1] = {['OpenURL']='http://domoticzip:port/json.htm?type=command&param=switchlight&idx='..urlid..'&switchcmd=Off'}  
(#commandArray is the number of entries in the comandArray table)

Re: While Loop - Last true condition start

Posted: Saturday 22 September 2018 12:31
by Ascari
Nice it's work :D