# Run python/opencv code in a sequence

**URL:** <https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898>\
**Category:** Software\
**Created:** [August 19, 2025, 8:18pm UTC](https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898 "2025-08-19T20:18:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jan.janson](https://forum.farmbot.org/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jan.janson](https://forum.farmbot.org/u/jan.janson)\
**Post date:** [August 19, 2025, 8:18pm UTC](https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898/1 "2025-08-19T20:18:46Z")

</div>

I need to decide about the watering in the sequence and skip or use less water.

I start using classic tensiometer. The camera can take picture of the dial of the gauge and I can download the image (using token and python code) on an external computer.

Now I have python/opencv code reading the value from the dial. I wish to have the readout value available back in a sequence. Not using a pooling method would be great. Is there possibility to run the python script on self hosted farmbot-web-server which I have available ? Or trigger external server from the lua block in a sequence and read the results somehow?

Plus there is still issue with my camera (CSI camera - raspberry version 2) with auto exposition - it overexpose the dial in a sunny day. But I can water the garden in a morning/evening only.

---

<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:** [August 20, 2025, 1:29am UTC](https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898/2 "2025-08-20T01:29:36Z")

</div>

> [@jan.janson](#):
>
> I wish to have the readout value available back in a sequence.

Lua code can fetch objects from a web API and can store and read persistent key-value pairs with the **`env()`** function . . can you offer that tensiometer dial reading on a simple REST server ?

---

<div class="post-metadata">

**Author:** ![jan.janson](https://forum.farmbot.org/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jan.janson](https://forum.farmbot.org/u/jan.janson)\
**Post date:** [August 20, 2025, 7:41am UTC](https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898/5 "2025-08-20T07:41:00Z")

</div>

Ok, like this?

# 1. python - seve the value

import requests, os

API\_TOKEN = “eyJ0eX…”  
BOT\_HOST = “192.168.1.249:3000” # e.g. “[my.farmbot.io](http://my.farmbot.io)”

if not API\_TOKEN:  
raise RuntimeError(“Set FARMBOT\_API\_TOKEN in your environment first!”)

headers = {“Authorization”: f"Bearer {API\_TOKEN}"}

value = 42

#Step 1. Update web app env directly  
resp = requests.patch(  
f"http://{BOT\_HOST}/api/web\_app\_config",  
headers=headers,  
json={“env”: {“analysis\_result”: str(value)}}  
)

#Step 2. Debug output  
print(“Status:”, resp.status\_code, flush=True)  
print(“Body (first 200 chars):”, resp.text[:200], flush=True)

# 2. lua read in the sequence

local result = tonumber(env(“analysis\_result”) or “0”)  
toast("Python analysis result: " .. result)

# 3. output

Status: 200  
Body (first 200 chars): {“id”:1,“created\_at”:“2025-05-13T12:45:28.876Z”,“updated\_at”:“2025-08-20T06:15:29.956Z”,“device\_id”:1,“confirm\_step\_deletion”:false,“disable\_animations”:fals  
e,“disable\_i18n”:false,“display\_trail”:true

---

<div class="post-metadata">

**Author:** ![jan.janson](https://forum.farmbot.org/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jan.janson](https://forum.farmbot.org/u/jan.janson)\
**Post date:** [August 20, 2025, 7:41am UTC](https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898/6 "2025-08-20T07:41:51Z")

</div>

sorry and the result is wrong… :  
Python analysis result: 0.0

---

<div class="post-metadata">

**Author:** ![Gabriel](https://forum.farmbot.org/letter_avatar_proxy/v4/letter/g/9de0a6/32.png) [@Gabriel](https://forum.farmbot.org/u/Gabriel)\
**Post date:** [August 26, 2025, 9:28pm UTC](https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898/7 "2025-08-26T21:28:17Z")

</div>

If you have an extra computer or Raspberry Pi with a camera on the same network as FarmBot you can try something similar to the Lua HTTP example in the [sidecar hardware docs](https://developer.farm.bot/docs/sidecar-hardware).

If instead you need to use FarmBot’s camera it would require writing code (perhaps using the [FarmBot Python library](https://developer.farm.bot/docs/python)) to fetch images from the FarmBot Web App and process them, and then save the value somewhere a Lua sequence step could pick it up, like `value = tonumber(api{url="/api/sensor_readings"}[1].value)`. If you want to go that route, see [`listen()`](https://developer.farm.bot/docs/python-functions-message-broker), [`api_post()`](https://developer.farm.bot/docs/python-functions-api), and [`sensor_readings`](https://developer.farm.bot/v15/docs/web-app/api-docs#sensor_readings) for inspiration.

---

<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:** [September 25, 2025, 9:29pm UTC](https://forum.farmbot.org/t/run-python-opencv-code-in-a-sequence/9898/8 "2025-09-25T21:29:14Z")

</div>

Closed on September 25, 2025, 9:29pm UTC

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