> For the complete documentation index, see [llms.txt](https://modeltrainscript.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://modeltrainscript.gitbook.io/docs/manual/automation/tutorials/railcom.md).

# RailCom

Basic RailCom tutorial

In this tutorial you will create an event-driven script that automatically stops any train in front of a red signal. Using RailCom, the script detects which vehicle is on the sensor and acts on it — no manual vehicle selection needed.

**What you'll need:**

* A Z21 command station with RailCom-capable feedback (LocoNet or CAN bus)
* A signal configured as an accessory
* A RailCom-capable decoder in your locomotive

## 1. Layout

Start in the [layout editor](/docs/manual/layout.md) with a simple track, a signal, and a feedback sensor.

<figure><img src="https://4224524483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjZj7d8De4optEMsLAji%2Fuploads%2FAZVnrQV1Ix8emxzje9VO%2Fimage.png?alt=media&amp;token=5bdc673e-9d24-42c7-a9f5-0b02b146a8fd" alt="" width="188"><figcaption></figcaption></figure>

The train comes from **A**, drives towards **B**, and passes signal "S1" along the way. Before the signal, we place a sensor with RailCom support — in this case using the CAN bus of the Z21 with address 1, port 1.

<figure><img src="https://4224524483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjZj7d8De4optEMsLAji%2Fuploads%2FGTXj4jQY6ibKuA9fW0Hc%2Fimage.png?alt=media&amp;token=e8e7e789-9b6b-48f1-b34a-26a4358e2a67" alt="" width="188"><figcaption></figcaption></figure>

The layout is ready.

## 2. Script

Create a new [script](/docs/manual/automation/scripts.md) and set it to be triggered by an **event** instead of running manually.

<figure><img src="https://4224524483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjZj7d8De4optEMsLAji%2Fuploads%2FZnwHZGCKiZbl2GDmwXsi%2Fimage.png?alt=media&amp;token=0e6c8823-a571-41a4-8c2a-dce4fe3f5f5f" alt="" width="188"><figcaption></figcaption></figure>

Add an execution event of type **feedback** and select the sensor in front of signal "S1". Enable **Select first occupier** — this makes Model Train Script automatically select the detected vehicle when the script runs.

<figure><img src="https://4224524483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjZj7d8De4optEMsLAji%2Fuploads%2Fx1bzitn0J3cziwziulKr%2Fimage.png?alt=media&amp;token=972b4f13-0b7f-4544-a47b-3c0ac5f0119c" alt="" width="188"><figcaption></figcaption></figure>

Now set up the script logic. Delete the default section and create a new one called "Stop on red".

<figure><img src="https://4224524483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjZj7d8De4optEMsLAji%2Fuploads%2FlxZ1qcAeSfsdX2uQ9tTz%2Fimage.png?alt=media&amp;token=0886cecc-8201-4f61-a86e-6b639b34ab0f" alt="" width="188"><figcaption></figcaption></figure>

Give the section an **execution condition**: it should only run when signal "S1" is **red**. Set the condition type to **accessory**, select signal "S1", and choose the state "red".

<figure><img src="https://4224524483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjZj7d8De4optEMsLAji%2Fuploads%2FFYWaJJQcYjRWKZQvshkM%2Fimage.png?alt=media&amp;token=1b686f07-1a5c-4df3-ab9c-b291a72fc43b" alt="" width="188"><figcaption></figcaption></figure>

Add a **stop** action to the section.

<figure><img src="https://4224524483-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjZj7d8De4optEMsLAji%2Fuploads%2FvlKYfQfYpBFdT4Znk4Un%2Fimage.png?alt=media&amp;token=9b79ebfd-a876-4239-96f0-2b5ab8e8915a" alt="" width="188"><figcaption></figcaption></figure>

Done! The script triggers whenever a vehicle is detected at the sensor. If signal "S1" is red, the train stops. If the signal is green, nothing happens and the train continues.

## 3. Ideas for improvement

We kept this tutorial as simple as possible. Here are some ways to extend it:

* **Add a yellow signal section** — Place the execution condition on the section rather than the script. This way you can add a second section for yellow: slow the train down instead of stopping it.
* **Use "Select vehicle from feedback"** — Instead of the "Select first occupier" option on the event, you can achieve the same with a dedicated action. This gives you more flexibility in complex scripts.
