Background services
A service is a device without an instrument: a small program that runs all the time and looks at other devices or does something regularly. Typical services send a pressure to a web page, post to Slack when a value crosses a limit, or start and stop a pump from a chat message.
A service is written like any other device, with the same options, state, log and restarts after errors. The differences are in how LabOrchestra treats it:
- It has its own section on the Devices page, Background services, and stays off the dashboards, the layout editor’s device list and the default view. (You can still add one of its values to a view by hand.)
- It restarts by itself when you save its file. A device does not, because restarting an instrument may touch hardware.
- The header shows a warning chip while a service is in error, so that a broken uploader is not overlooked.
Make one
Section titled “Make one”-
On the Devices page, click New service… in the section Background services.


-
Choose a template, name the service (
pressure_uploader), and click Create. -
The file
services/pressure_uploader.pyis in your workspace and the service is running. A template that needs settings saysnot configured: …in its state until you set its options (Change… in the menu of the service on the Devices page).
| Template | What it does |
|---|---|
| Background task | An empty loop: the starting point for anything. |
| Uploader | Sends the average of one state value to a web address once in a while (a sensor web page, a dashboard). One instance per sensor. |
| Slack notifier | Posts a message when a value falls below or rises above a limit, for example when the pressure has dropped after pumping down. |
| Slack pump control | Starts, stops and reports on turbo pumps from a Slack channel (bot start big, bot stop small, bot status), and says when a pump has stopped or reached its speed. |
| Particle name | A playful example of a service that reacts to a device: it asks an AI for a name for the trapped particle when the pressure drops below a limit. It needs an OpenAI key. |
Your own templates go into templates/service/ of the workspace, as <id>.py.tmpl. The first line is # template {"title": "...", "description": "..."}.
Write one
Section titled “Write one”from laborchestra import Service
class PressureWatch(Service): """Logs a warning when the chamber pressure is above a limit."""
limit: float = 1e-5 # options are class attributes, as in any device state = {"status": "starting", "warnings": 0}
def run(self): while self.running: pressure = self.lab["pressure"].state["channels"][1]["pressure"] if pressure is not None and pressure > self.limit: self.state["warnings"] += 1 self.log.warning("chamber pressure %.2g mbar is above %.2g", pressure, self.limit) self.state["status"] = "ok" self.sleep(10)A service reads other devices through self.lab["name"].state and acts through their actions (self.lab["turbo"].stop(0)). Put what the person looking at the Devices page should see into state, for example a status that says “not configured: set the URL” instead of failing quietly.
Passwords and keys
Section titled “Passwords and keys”Keep them out of the file and out of devices.json: store them as secrets and refer to them by name in the service’s options. The service gets the value; it never appears in the workspace, the logs or the state.
Something wrong or missing on this page? Open a GitHub issue or use the form (no account needed).