Lua Script not working cause of not planted plant

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.

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

What am I doing wrong?

Hi @Christian

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

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

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

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

I have also already executed this shared sequence The FarmBot Web App 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.

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

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

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

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

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.

@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 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 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"))

@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 :expressionless:

$ 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}
$ 
1 Like

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). When I water a single plant with the

water(variable(“Plant”))

everything works.

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.

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

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

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:

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:

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

Thank you very much! Now everything works as desired

1 Like