# Updated 'Water All' code

**URL:** <https://forum.farmbot.org/t/updated-water-all-code/9767>\
**Category:** Software\
**Created:** [May 5, 2025, 9:47pm UTC](https://forum.farmbot.org/t/updated-water-all-code/9767 "2025-05-05T21:47:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mvillion](https://forum.farmbot.org/user_avatar/forum.farmbot.org/mvillion/32/5152_2.png) [@mvillion](https://forum.farmbot.org/u/mvillion)\
**Post date:** [May 5, 2025, 9:47pm UTC](https://forum.farmbot.org/t/updated-water-all-code/9767/1 "2025-05-05T21:47:24Z")

</div>

Hi there  
I have rewritten the Water All code

This is my expression of the code. It includes a network retry function to prevent errors during network glitches and an optimised ‘serpentine sequence’ as it snakes through the plant bed.

Not sure what to do with it so I am putting it here for feedback.

No comments. Short variable names and no whitespace as I was up against the 3000 character limit.

```auto
local wt=variable("Watering Time (Seconds)")
local st=os.time()*1000
local tries=9
local delay=20
local fail=1
local pts
while tries>0 do
  tries=tries-1
  pts=api({method="GET",url="/api/points"})
  if pts then
    toast("Plant list obtained","info")
    tries=0
    fail=0
  else
    toast("Error - Retrying","warn")
    wait(delay*1000)
  end
end
if fail==1 then
  toast("Fatal Error - No plants","error")
  return
end
local pls={}
for _,v in pairs(pts)do
  if v.pointer_type=="Plant" then
    table.insert(pls,{n=v.name,x=v.x,y=v.y})
  end
end
-- Dynamic X clustering with serpentine Y sweep
local tol=50
table.sort(pls,function(a,b)return a.x<b.x end)
local grps={{}}
for _,p in ipairs(pls)do
  local g=grps[#grps]
  if #g==0 then
    table.insert(g,p)
  else
    if math.abs(p.x-g[1].x)<=tol then
      table.insert(g,p)
    else
      table.insert(grps,{p})
    end
  end
end
local s_pls={}
for i,g in ipairs(grps)do
  table.sort(g,function(a,b)return a.y<b.y end)
  if i%2==0 then
    -- Reverse Y order for even groups
    local rev={}
    for j=#g,1,-1 do table.insert(rev,g[j])end
    g=rev
  end
  for _,p in ipairs(g)do table.insert(s_pls,p)end
end
pls=s_pls
local c=0
local t=#pls
local job="Watering "..t.." plants"
send_message("info",job.." for "..wt.."s each","toast")
for _,v in pairs(pls)do
  local cn="("..math.floor(v.x)..","..math.floor(v.y)..")"
  local pn=v.n or"plant"
  if #pn>20 then pn=string.sub(pn,1,20).."..."end
  set_job_progress(job,{percent=100*c/t,status="Moving to "..pn.." "..cn,time=st})
  move_absolute(v.x,v.y,-280)
  set_job_progress(job,{percent=100*(c+0.5)/t,status="Watering "..pn,time=st})
  write_pin(8,"digital",1)
  wait(wt*1000)
  write_pin(8,"digital",0)
  c=c+1
end
set_job_progress(job,{percent=100,status="Complete",time=st})

```

---

<div class="post-metadata">

**Author:** ![system](https://forum.farmbot.org/user_avatar/forum.farmbot.org/system/32/4564_2.png) [@system](https://forum.farmbot.org/u/system)\
**Post date:** [June 4, 2025, 9:48pm UTC](https://forum.farmbot.org/t/updated-water-all-code/9767/2 "2025-06-04T21:48:21Z")

</div>

Closed on June 4, 2025, 9:48pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
