scadable add controller

Generate a controller file in controllers/ from a template.

scadable add controller <name>

Example

scadable add controller temp-monitor

Creates controllers/temp_monitor.py:

"""TODO: describe this controller."""
from scadable import Controller, on, SECONDS


class TempMonitor(Controller):

    @on.interval(5, SECONDS)
    def run(self):
        pass

What you typically add

Three steps to make this real:

  1. Import the device classes you want to read from.
  2. Pick the trigger that matches when the method should fire.
  3. Write the logic in the method body.
from scadable import Controller, on, SECONDS
from devices.line_sensor import LineSensor

class TempMonitor(Controller):

    @on.interval(5, SECONDS)
    def check(self):
        if LineSensor.temperature > 95:
            self.alert("critical", f"Temp: {LineSensor.temperature}C")
        self.publish("readings", {"temp": LineSensor.temperature})

LineSensor.temperature reads the latest scaled value of register 40001 on the device. No HTTP calls, no awaits, no callbacks. The runtime makes the value available as an attribute and you read it.

Trigger patterns

You can stack multiple triggers on the same controller:

class SafetyMonitor(Controller):

    @on.startup
    def init(self):
        self.publish("status", {"state": "armed"})

    @on.interval(5, SECONDS)
    def heartbeat(self):
        self.publish("hb", {"ok": True})

    @on.threshold(LineSensor.temperature, above=95)
    def overheat(self):
        self.alert("critical", "overtemp")
        LineSensor.flow_rate = 0

See Core Concepts for the full trigger inventory.

Exit codes

CodeMeaning
0File created
1Controller file already exists

Next steps