While Loop - Last true condition start

Moderator: leecollings

Post Reply
User avatar
Ascari
Posts: 30
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9
Location: Belgium
Contact:

While Loop - Last true condition start

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: While Loop - Last true condition start

Post 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)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Ascari
Posts: 30
Joined: Thursday 28 September 2017 21:35
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9
Location: Belgium
Contact:

Re: While Loop - Last true condition start

Post by Ascari »

Nice it's work :D
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest