Loading Python Classes as Models
Build model logic directly in Python and plug it into SPX via YAML/JSON — an alternative to fully declarative models.
When to use it
1) Write the Python class
# extensions/py_temp_sensor.py
import math
import time
class PyTempSensor:
"""
Minimal sensor logic implemented in Python.
- Keeps an internal temperature value
- Exposes a property 'temperature' used by SPX
- Provides a 'tick' helper to update the internal state
"""
def __init__(self, start: float = 25.0, drift: float = 0.0):
self._t = start
self._drift = drift
self._t0 = time.time()
# Property used by SPX to read/write the attribute
@property
def temperature(self) -> float:
return self._t
@temperature.setter
def temperature(self, val: float):
self._t = float(val)
# Optional helper you can call from actions/logic
def tick(self) -> float:
# Example: a tiny sinusoid + linear drift
now = time.time() - self._t0
self._t = self._t + self._drift + 0.05 * math.sin(now)
return self._t2) Declare it in YAML (or JSON)
3) Create a model and instance, then run
4) Sample and visualize the signal
5) (Optional) Expose your Python‑backed attributes over Modbus
Advanced options
Summary
Last updated

