Lorx manages thermostatic devices, applies schedule‑based temperature control with hysteresis, supports manual override modes (auto / on / off), streams real‑time updates to a Phoenix LiveView dashboard, and periodically persists historical temperature data.
Current capabilities:
- Device polling (temperature + hardware status)
- Schedule-driven auto mode with hysteresis threshold
- Manual override modes (
:onforces heating,:offforces idle) - PubSub event fan‑out for UI + persistence decoupling
- Historical temperature storage & retrieval
- Live dashboard with per‑device mode switching
Planned (see Roadmap): authentication, multi‑tenant isolation, external weather integration, richer analytics.
Application tree (Lorx.Application):
Lorx.Supervisor (one_for_one)
├─ LorxWeb.Telemetry
├─ Lorx.Repo
├─ DNSCluster (optional / clustering)
├─ Phoenix.PubSub (Lorx.PubSub)
├─ Finch (HTTP client)
├─ LorxWeb.Endpoint (HTTP + WebSocket)
├─ Registry (Lorx.Device.Registry) # name -> device process
├─ Lorx.DeviceSupervisor (DynamicSupervisor)
└─ Lorx.Collector.Monitor (GenServer)
On startup Lorx.DeviceSupervisor.spawn_children/0 starts one Lorx.Device GenServer per row in devices.
DynamicSupervisor managing a fleet of per‑device GenServers. Loads devices from the database and starts children {Lorx.Device, [device.id]}.
Represents one physical thermostat‑capable unit.
Responsibilities:
- Periodically poll hardware (temperature + current status)
- Delegate decision logic to
Lorx.DeviceState - Broadcast state changes efficiently
- Accept manual mode changes (
set_mode/2)
Internal cycle:
init/1seeds a minimalDeviceStatethen schedules:setuphandle_continue(:setup)loads DB device + schedules + first temp/status, then triggers immediate pollhandle_info(:check_temp)every poll interval:- Recompute new state via
DeviceState.update_state/1 - Broadcast telemetry (always) to
temperature_notification - Broadcast dashboard update (only if
updated?) todashboard
- Recompute new state via
Public API:
get_status(id)– synchronous snapshot (returns full internal state; slated for narrowing)set_mode(id, mode)– asynchronous override (:auto | :on | :off)
Holds and transforms device state.
Fields: :device, :schedules, :temp, :prev_temp, :status, :target_temp, :updated?, :mode.
Mode branches:
:on– force switch on (idempotent if already heating):off– force switch off:auto– schedule + hysteresis logic
Hysteresis rule (with threshold T = 0.5):
if target > current + T and status == :idle -> switch_on
if target < current - T and status == :heating -> switch_off
else keep
Scheduling:
get_current_schedule/1 filters by current weekday + time range (supports overnight windows) using a days array of string flags ("true"/"false").
Subscribes to "temperature_notification" and periodically persists snapshots to temperature_history respecting saving_interval (ms). Throttles writes: only inserts when interval elapsed since last save.
LorxWeb.LiveDashboard – On mount: fetches all devices, queries each device process for current state, subscribes to "dashboard", and updates each DeviceComponent via send_update/2 upon new broadcasts.
LorxWeb.DeviceComponent – Renders a device card with temperature, target, status badge, and three mode buttons. On button click:
- Sends
phx-clickeventset_mode - Calls
Device.set_mode/2 - Optimistically updates local
@mode; reconciled by broadcast.
| Topic | Producer | Consumers | Payload |
|---|---|---|---|
temperature_notification |
Each Lorx.Device on every poll |
Lorx.Collector.Monitor |
%Lorx.NotifyTemp{device_id,temp,status,target_temp,mode} |
dashboard |
Each Lorx.Device only when updated? |
LiveDashboard (UI) | Same struct |
Reasoning: decouple high-frequency polling from UI refresh (UI only when state materially changes) while still capturing regular telemetry for history.
Fields: name, ip, timestamps.
Used for process identity and hardware network targeting.
Fields: temp, start_time, end_time, device_id, days (array of strings). Drives auto mode target temperature selection.
Fields: device_id, timestamp, temp, device_status, target_temp (+ row timestamps). Populated throttled by Collector.
mode (currently not persisted; defaults to :auto on restart) and updated? diff flag.
| Concern | Database | Process State | Notes |
|---|---|---|---|
| Device identity | ✔ | Cached in DeviceState.device |
Reloaded at load only |
| Schedules | ✔ | DeviceState.schedules |
Re-fetched each poll (optimizable) |
| Current temperature | ✖ | DeviceState.temp |
From DeviceClient |
| Device status | History in temperature_history |
DeviceState.status |
Live vs historical |
| Target temperature | Derived (from schedule) | DeviceState.target_temp |
0 if none active |
| Mode | ✖ | DeviceState.mode |
Reset to :auto after restart |
| Updated flag | ✖ | DeviceState.updated? |
Broadcast gating |
- User opens dashboard → LiveView mounts → loads DB devices → queries each device process → initial render.
- Each
Lorx.Devicepolls: reads hardware → computes new state → broadcasts telemetry (always) & dashboard diff (conditional). - Collector persists samples at configured interval.
- User toggles mode → LiveComponent event →
set_mode/2cast → device recomputes state immediately → broadcast updates UI.
- Separation of decision logic (
DeviceState) from orchestration (Device) simplifies testing. - Dual-topic PubSub prevents UI overdraw while retaining raw sampling cadence.
- Manual modes override all schedule logic—clear predictable semantics.
- Hysteresis reduces relay chatter / hardware wear.
- Mode not persisted (loss across restarts).
- Schedule reload every poll (introduces DB overhead).
daysstored as string flags instead of booleans or bitmask.- Lack of failure handling / backoff around
DeviceClientI/O. get_status/1leaks full internal struct (violates encapsulation).updated?consistency in manual branches (ensure always set).- No authentication / multi-tenant boundary yet.
- No caching for schedule lookups or diffing.
- Absence of rate limiting for mode toggles.
Short Term:
- Persist
mode(migration + schema + restore on load) - Normalize
daysto booleans - Extract
DeviceViewfor safe public snapshot - Cache schedules with change invalidation
Medium Term:
- Add authentication & authorization
- Multi-tenant scoping (household/workspace model)
- External weather integration (adjust predictive targets)
- Historical analytics endpoints & charts
Long Term:
- Alerting (out-of-range temps, device offline)
- Energy optimization strategies
- API keys / remote control API
- Edge deployment / clustering improvements
mix deps.get
mix ecto.setup # creates & migrates DB, loads seeds if added
mix phx.server
# or, for an interactive shell and HTTP server:
iex -S mix phx.server
Set EXTERNAL_TEMP_IP to the external meter's IP address (and optional port) to
poll http://<ip>/temp immediately and every five minutes. The device must
return JSON such as {"temp": 18.0}. Leave the variable unset to disable
polling; the dashboard then marks the latest saved temperature as not configured.
Successful readings are stored in external_temperature_history.
Environment config keys (example):
config :lorx, :device, polling_interval: <ms>, saving_interval: <ms>
Focused tests exist for DeviceState decision logic. Recommended additions:
- Mode persistence (future)
- Schedule boundary (overnight, single-day) tests
- PubSub integration tests (broadcast gating on
updated?)
- Fork & branch
- Add tests for behavior changes
- Run formatter & credo (if added later)
- Open PR with rationale
| Term | Meaning |
|---|---|
| Device | Physical heating/thermostat endpoint controlled via IP |
| Schedule | Time window + target temperature definition |
| Mode | Manual override or automatic control strategy |
| Hysteresis | Threshold band to prevent rapid toggling |
| Collector | Background sampler persisting periodic measurements |
- Dashboard device card: show active schedule window
- Temperature history chart
- External temperature ingestion API
- Authentication layer
- Multi-tenant (multi-house, cloud-ready)
- Current weather (https://open-meteo.com/en/docs?time_mode=time_interval&start_date=2025-09-16&end_date=2025-09-16&timezone=Europe%2FBerlin&latitude=45.54&longitude=10.21&hourly=¤t=temperature_2m,apparent_temperature,wind_speed_10m,precipitation)
TBD
| Workflow | File | Trigger | Branch Scope | Purpose |
|---|---|---|---|---|
| Elixir CI | .github/workflows/build.yml |
push (any branch) |
* |
Compile & basic validation (deps fetch + compile) |
| Deploy | .github/workflows/deploy.yml |
workflow_run (after successful Elixir CI) |
release only |
Build & push container image, create release tag, deploy stack |
You can add these badges (uncomment if you like) at the top of the README:


- Any push to any branch triggers the Elixir CI workflow.
- If (and only if) the branch is
releaseand CI concludes withsuccess, the Deploy workflow is triggered viaworkflow_run. - Deploy workflow stages:
precheck: Ensures CI succeeded and branch ==release.setup: Generates a timestamp-based version (formatYYYY.MMDD.HHMM).package: Builds and pushes a Docker image to GHCR (ghcr.io/emadb/lorx:<version>).tag-and-release: Creates a GitHub Release with the same version.deploy: Downloads release artifacts (compose file), performs a stack deploy on the self-hosted runner.
The version is time-based (build timestamp). This avoids collisions without manual tagging. If you later introduce semantic versions you can replace the setup step logic.
Re-running the successful CI for a release branch commit will (by default) trigger the Deploy workflow again and thus a new version (since timestamp regenerated). If you want to prevent redeploys via rerun, add a guard checking run_attempt == 1 inside precheck.
Add to the Deploy workflow to prevent overlapping deploys:
concurrency:
group: deploy-release
cancel-in-progress: true
Secrets expected (GitHub UI → Repo → Settings → Secrets and variables):
| Name | Type | Used In | Notes |
|---|---|---|---|
POSTGRES_PASSWORD |
Secret | Deploy | Injected into stack deploy env |
GITHUB_TOKEN |
Built-in | Deploy | Release + artifact download |
Variables (Repository Variables):
| Name | Used In | Purpose |
|---|---|---|
ENV_NAME |
Deploy | Environment label / runtime flag |
DEVICE_POLLING_INTERVAL |
Deploy | Device poll ms override |
SAVING_INTERVAL |
Deploy | Persistence cadence ms |
Replicate locally what package does:
docker build -t ghcr.io/emadb/lorx:dev .
- Add test (mix test) & dialyzer steps to CI before compile.
- Cache deps & build artifacts (actions/cache) for faster runs.
- Add a security scan (e.g., Trivy or Grype) on the image before push.
- Push SBOM (CycloneDX) alongside the image.