# Lua Script not working cause of not planted plant

**URL:** <https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113>\
**Category:** Troubleshooting\
**Created:** [May 18, 2024, 1:41pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113 "2024-05-18T13:41:04Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian](https://forum.farmbot.org/user_avatar/forum.farmbot.org/christian/32/5172_2.png) [@Christian](https://forum.farmbot.org/u/Christian)\
**Post date:** [May 18, 2024, 1:41pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/1 "2024-05-18T13:41:04Z")

</div>

Hello dear Farmbot Community,

I recently treated myself to a Farmbot. I am currently setting up the sequences. I would first like to determine the measured moisture value for each plant group and then irrigate. I found a tip in the forum that this can be easily implemented using a LUA script. I use the following for this

> ## – Mount Soil Sensor Tool – soil\_sensor\_tool = variable(“Soil Sensor Tool”) – mount\_tool(soil\_sensor\_tool) – Carrots – Move to Measure Point measure\_point\_carrots = variable(“Measure Point Carrots”) move{ x = measure\_point\_carrots.x, y = measure\_point\_carrots.y, z = measure\_point\_carrots.z, safe\_z = true } – Measure Soil Moisture soil\_sensor\_pin = 59 soil\_moisture\_carrots = read\_pin(soil\_sensor\_pin, “analog”) – Unmount Soil Moisture tool dismount\_tool() – Check if Moisture is below threshold - Over all moisture\_threshold = 500 if soil\_moisture\_carrots \< moisture\_threshold then – Mount Water Tool wait(5000) water\_tool = variable(“Water Tool”) mount\_tool(water\_tool) – Iterate over plants in group Carrots for i,member in ipairs(group(variable(“Group ID Carrots”))) do – Get plant object plant = api({ method = “get”, url = “/api/points/” … member }) – Move to plant and water move\_absolute(plant.x, plant.y, 0) water(plant) end – Unmount Water Tool dismount\_tool() end

Unfortunately, the sequence always returns the error that I have not yet planted the plant.

 ![image](https://forum.farmbot.org/uploads/default/original/2X/d/d58e5603c464dbaf57a9d15e5fbeaf7d8279f7d5.png)

However, when I go to the farmbot webapp, the plant is active.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.farmbot.org/user_avatar/forum.farmbot.org/christian/32/5172_2.png) [@Christian](https://forum.farmbot.org/u/Christian)\
**Post date:** [May 18, 2024, 2:04pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/2 "2024-05-18T14:04:58Z")

</div>

![image](https://forum.farmbot.org/uploads/default/original/2X/e/e44d74e87e02e4dc6b4c30108387d0f7832975b7.png)

---

<div class="post-metadata">

**Author:** ![jsimmonds](https://forum.farmbot.org/user_avatar/forum.farmbot.org/jsimmonds/32/1470_2.png) [@jsimmonds](https://forum.farmbot.org/u/jsimmonds)\
**Post date:** [May 19, 2024, 9:24am UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/3 "2024-05-19T09:24:08Z")

</div>

Hi @Christian

The May 9, 2024 Software Update mentions a fix that might apply to you ?

> [@May 9, 2024 Software Update](https://forum.farmbot.org/t/may-9-2024-software-update/9093/1):
>
> Plant age filtering fix

Or maybe not . . it refers to **age** and not **status**. . I’ll have a play with your Lua code.  
That **water()** function is yours ?

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.farmbot.org/user_avatar/forum.farmbot.org/christian/32/5172_2.png) [@Christian](https://forum.farmbot.org/u/Christian)\
**Post date:** [May 19, 2024, 8:05pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/4 "2024-05-19T20:05:46Z")

</div>

Hi @jsimmonds

My Farmbot is running the latest version. I took the code from this forum entry [Nested sequences and Plant Groups - #13 by roryaronson](https://forum.farmbot.org/t/nested-sequences-and-plant-groups/7247/13)

The “plant” feature is described in more detail in this documentation

> **[April 11, 2023 Software and Documentation Update](https://farm.bot/blogs/news/april-11-2023-software-and-documentation-update)**
>
> Hello all,
> Today we released a new version of FarmBot OS, the web app, several of our featured sequences, and published updates to our developer documentation. Here’s what’s new:
> 
> water(plant) Lua helper
> We’ve added a new...

I have also already executed this shared sequence [The FarmBot Web App](https://my.farm.bot/app/shared/sequence/158) which should update all plants.

Unfortunately no success. It still returns the error that the plant has not yet been planted.

I have now rebuilt the LUA script and use “dispense” instead of “water(plant)”.

However, my aim is for the Farmbot to create a soil moisture reading for each group of plants and use this as a basis for deciding whether or not to water a group.

I have extended the script from the forum to measure three plant groups. This works well so far but …

It only waters the first plant in the group and then jumps to the next group. It seems as if the loop from the script is not working properly.

 ![image](https://forum.farmbot.org/uploads/default/original/2X/e/e44c4153a89283f3d736b1bca05dbbf3f583cb1e.png)

> if soil\_moisture\_1 \< moisture\_threshold\_all then  
> for i, member in ipairs(group(variable(“Group ID #1”))) do  
> plant = api({  
> method = “get”,  
> url = “/api/points/” … member  
> })  
> move\_absolute(plant.x, plant.y, 0)  
> dispense1 = variable(“Dispense #1”)  
> dispense(dispense1)  
> end  
> end

@roryaronson You had provided the LUA code in the forum. Perhaps you have an idea what I am doing wrong here?

Thank you in advance for your support

Greetings Christian

---

<div class="post-metadata">

**Author:** ![jsimmonds](https://forum.farmbot.org/user_avatar/forum.farmbot.org/jsimmonds/32/1470_2.png) [@jsimmonds](https://forum.farmbot.org/u/jsimmonds)\
**Post date:** [May 20, 2024, 10:28am UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/5 "2024-05-20T10:28:24Z")

</div>

> [@Christian](#):
>
> I have now rebuilt the LUA script and use “dispense” instead of “water(plant)”.

Does this fix the “ **has not been planted yet. Skipping.** ” error ?

> [@Christian](#):
>
> It seems as if the loop from the script is not working properly.

I’ll see if I can reproduce your reported behaviour.

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.farmbot.org/user_avatar/forum.farmbot.org/christian/32/5172_2.png) [@Christian](https://forum.farmbot.org/u/Christian)\
**Post date:** [May 20, 2024, 10:35am UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/6 "2024-05-20T10:35:48Z")

</div>

> Does this fix the “ **has not been planted yet. Skipping.** ” error ?

Yes, with the “dispense” function the error does not appear

> I’ll see if I can reproduce your reported behaviour.

That’s really great! Many thanks for your efforts

---

<div class="post-metadata">

**Author:** ![jsimmonds](https://forum.farmbot.org/user_avatar/forum.farmbot.org/jsimmonds/32/1470_2.png) [@jsimmonds](https://forum.farmbot.org/u/jsimmonds)\
**Post date:** [May 21, 2024, 12:59pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/7 "2024-05-21T12:59:01Z")

</div>

Hi @Christian

Looking at the code, the Lua **water()** function _requires_ that a Plant object has an **age** property. I’m not sure how this property comes into being. Likely a coding bug / change of idea.

May I suggest you email **support@farm.bot** or PM Rory if it’s a show-stopper for you.

---

<div class="post-metadata">

**Author:** ![roryaronson](https://forum.farmbot.org/user_avatar/forum.farmbot.org/roryaronson/32/24_2.png) [@roryaronson](https://forum.farmbot.org/u/roryaronson)\
**Post date:** [May 21, 2024, 10:06pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/8 "2024-05-21T22:06:21Z")

</div>

@Christian thanks for bringing this up and providing so many details. There seems to be an issue where your plants might not have an age (even though the frontend is showing they do) which might be a bug with the migration script from May 9th. I am still looking into it all now. And just for a sanity check, could you try rebooting your FarmBot to rule out any issues where there could have been a synchronization failure?

The `water()` function requires the plant to have an **age** so that it can get the watering amount from the water curve assigned to the plant. [Here is the code in FarmBot OS](https://github.com/FarmBot/farmbot_os/blob/eade856c48e7d242dd76f4e7ec2d0113f3b28cfd/priv/lua/water.lua#L5C1-L8C8) where this check is performed, and where it is failing with the “has not been planted yet” message.

[Here is where FarmBot OS computes the plant’s age](https://github.com/FarmBot/farmbot_os/blob/eade856c48e7d242dd76f4e7ec2d0113f3b28cfd/lib/os/sys_calls/point_lookup.ex#L39) based on the current time and the `planted_at` value, which the migration script should have ensured all of your plants have.

Are you able to reproduce this with fresh resources and the simplest case?

- Add a plant
- Assign it a water curve
- Set the status to Planted (Gepflanzt)
- Make a sequence with a Location variable set to the plant and with the following Lua code: `water(variable("Location 1"))`

---

<div class="post-metadata">

**Author:** ![jsimmonds](https://forum.farmbot.org/user_avatar/forum.farmbot.org/jsimmonds/32/1470_2.png) [@jsimmonds](https://forum.farmbot.org/u/jsimmonds)\
**Post date:** [May 22, 2024, 6:42am UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/9 "2024-05-22T06:42:38Z")

</div>

> [@roryaronson](#):
>
> The `water()` function requires the plant to have an **age** so that it can get the watering amount from the water curve assigned to the plant. [Here is the code in FarmBot OS](https://github.com/FarmBot/farmbot_os/blob/eade856c48e7d242dd76f4e7ec2d0113f3b28cfd/priv/lua/water.lua#L5C1-L8C8) where this check is performed, and where it is failing with the “has not been planted yet” message.

@roryaronson @Christian  
What I was trying to say in the previous reply was that **age** is _not_ in the DB schema for a Point (plant), so that Lua code in **water()** should not expect to look for it directly ! That’s a bug.

## edit

My missed steak . . **water()** is using the resource object returned from the API not the local DB, but if **age** is a “computed attribute” then the API is not providing it 😑

```bash
$ curl --header [jwt] https://my.farm.bot/api/points/809725
{"id":809725,"created_at":"2022-08-14T03:22:20.805Z","updated_at":"2024-05-07T00:06:03.968Z","device_id":3505,"name":"Corn","pointer_type":"Plant","meta":{"gridId":"f1572b66-befa-420e-93bc-5c26eea11b5c"},"x":2442,"y":782,"z":0,"openfarm_slug":"corn","plant_stage":"sprouted","planted_at":"2024-04-30T14:00:00.000Z","radius":25.0,"depth":0,"water_curve_id":null,"spread_curve_id":null,"height_curve_id":null}
$ 

```

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.farmbot.org/user_avatar/forum.farmbot.org/christian/32/5172_2.png) [@Christian](https://forum.farmbot.org/u/Christian)\
**Post date:** [May 25, 2024, 7:38am UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/10 "2024-05-25T07:38:49Z")

</div>

Thank you for your feedback. I was able to research the issue further today and discovered the issue. I restarted the Farmbot and also ran the LUA script ([farmbot\_os/priv/lua/water.lua at eade856c48e7d242dd76f4e7ec2d0113f3b28cfd · FarmBot/farmbot\_os · GitHub](https://github.com/FarmBot/farmbot_os/blob/eade856c48e7d242dd76f4e7ec2d0113f3b28cfd/priv/lua/water.lua#L5C1-L8C8)). When I water a single plant with the

> water(variable(“Plant”))

everything works.

 ![Farmbot_Working](https://forum.farmbot.org/uploads/default/original/2X/b/bd6554c824829553c7278917442f04108e16532b.jpeg)

However, if I water the group via this LUA script

> – Iterate over plants in group  
> for i,member in ipairs(group(96308)) do  
> – Get plant object  
> plant = api({  
> method = “get”,  
> url = “/api/points/” … member  
> })  
> – Move to plant and water  
> move\_absolute(plant.x, plant.y, 0)  
> water(plant)  
> end"

it returns that the plant has not yet been planted.

 ![Farmbot_Error](https://forum.farmbot.org/uploads/default/original/2X/2/226b1585cc494453f2ee90b66be8e91e1c6b4f2a.jpeg)

I have just created a group from one plant. Exactly the same behaviour occurs there too.

I have now double checked all 131 plants by hand. All have a watering curve, have the status planted and are over 1 day old

 ![Farmbot_CheckingAge](https://forum.farmbot.org/uploads/default/original/2X/4/41e1f6517b44615c5f538798e567a567ca9466f1.jpeg)

---

<div class="post-metadata">

**Author:** ![jsimmonds](https://forum.farmbot.org/user_avatar/forum.farmbot.org/jsimmonds/32/1470_2.png) [@jsimmonds](https://forum.farmbot.org/u/jsimmonds)\
**Post date:** [May 26, 2024, 6:02am UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/11 "2024-05-26T06:02:55Z")

</div>

Hi @Christian the FarmBot Inc. Development team are already testing some code fixes for this problem. Maybe new FBOS Software will be announced soon.

---

<div class="post-metadata">

**Author:** ![roryaronson](https://forum.farmbot.org/user_avatar/forum.farmbot.org/roryaronson/32/24_2.png) [@roryaronson](https://forum.farmbot.org/u/roryaronson)\
**Post date:** [May 28, 2024, 8:12pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/12 "2024-05-28T20:12:55Z")

</div>

Thank you @jsimmonds and @Christian for the help in debugging this! Indeed the problem was that if the plant object was fetched from the local DB with `variable(“Plant”)` then FarmBot OS would compute the `age` field as needed. If the plant object was fetched from the API with `api()` then the `age` field was neither present nor being computed.

In FBOS v15.4.8 which @Gabriel just released, the `water()` function will now compute the age of the plant based on the `planted_at` date even if it was fetched from the API.

@Christian there is however one other issue with your Lua code. If you run this:

```lua
for i, member in ipairs(group(123)) do
   stuff
end

```

Then the loop will only execute for the first member in the group.

Instead, you should assign the group to a variable first:

```lua
local group_members = group(123)
for i, member in ipairs(group_members) do
   stuff
end

```

---

<div class="post-metadata">

**Author:** ![Christian](https://forum.farmbot.org/user_avatar/forum.farmbot.org/christian/32/5172_2.png) [@Christian](https://forum.farmbot.org/u/Christian)\
**Post date:** [June 3, 2024, 6:02pm UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/13 "2024-06-03T18:02:23Z")

</div>

Thank you very much! Now everything works as desired

---

<div class="post-metadata">

**Author:** ![roryaronson](https://forum.farmbot.org/user_avatar/forum.farmbot.org/roryaronson/32/24_2.png) [@roryaronson](https://forum.farmbot.org/u/roryaronson)\
**Post date:** [June 4, 2024, 2:02am UTC](https://forum.farmbot.org/t/lua-script-not-working-cause-of-not-planted-plant/9113/14 "2024-06-04T02:02:20Z")

</div>

Closed on June 4, 2024, 2:02am UTC


