|
| 1 | +# home-energy-manager — Claude context |
| 2 | + |
| 3 | +## Deployment (native, no Docker) |
| 4 | + |
| 5 | +As of 2026-04-18 the service runs **natively on the host as root** — Docker has been removed. |
| 6 | + |
| 7 | +| Thing | Path / value | |
| 8 | +|---|---| |
| 9 | +| Python | `/root/home-energy-manager/.venv/bin/python` (3.12.3) | |
| 10 | +| SQLite DB | `/root/home-energy-manager/data/energy_state.db` | |
| 11 | +| Daikin token | `/root/home-energy-manager/data/.daikin-tokens.json` | |
| 12 | +| API server | `http://127.0.0.1:8000` | |
| 13 | +| Systemd unit | `home-energy-manager.service` | |
| 14 | +| Config | `/root/home-energy-manager/.env` | |
| 15 | +| Run command | `.venv/bin/python -m src.cli serve` | |
| 16 | + |
| 17 | +### Service management |
| 18 | + |
| 19 | +```bash |
| 20 | +systemctl status home-energy-manager |
| 21 | +systemctl restart home-energy-manager |
| 22 | +journalctl -u home-energy-manager -f # live logs |
| 23 | +curl http://127.0.0.1:8000/api/v1/health # quick health check |
| 24 | +``` |
| 25 | + |
| 26 | +### BOOT.md is outdated — ignore it |
| 27 | +The old BOOT.md refers to a `venv/` and `daemon start`. The active venv is `.venv/`, the service is systemd-managed, and `src.cli serve` is the entrypoint (not `daemon start`). |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Daikin Onecta — token management |
| 32 | + |
| 33 | +Tokens live at `data/.daikin-tokens.json`. The access token expires every **3 hours**; the service auto-refreshes it via the refresh_token as long as the refresh_token is valid (~30 days). |
| 34 | + |
| 35 | +### Refresh access token (refresh_token still valid) |
| 36 | + |
| 37 | +```bash |
| 38 | +cd /root/home-energy-manager |
| 39 | +.venv/bin/python - <<'EOF' |
| 40 | +import json, time |
| 41 | +from src.daikin.auth import refresh_tokens |
| 42 | +
|
| 43 | +tokens = json.load(open("data/.daikin-tokens.json")) |
| 44 | +new = refresh_tokens(tokens) |
| 45 | +new["obtained_at"] = int(time.time()) |
| 46 | +json.dump(new, open("data/.daikin-tokens.json", "w"), indent=2) |
| 47 | +print("Done. Expires in", new["expires_in"], "s") |
| 48 | +EOF |
| 49 | +``` |
| 50 | + |
| 51 | +Then `systemctl restart home-energy-manager` so the service picks it up. |
| 52 | + |
| 53 | +### Full re-auth (refresh_token expired or 401 after refresh) |
| 54 | + |
| 55 | +The auth flow starts a local HTTP server on port 18080 and opens a browser to the Daikin login page. On this headless VPS you need to either: |
| 56 | + |
| 57 | +**Option A — SSH port-forward (recommended):** |
| 58 | +```bash |
| 59 | +# On your local machine: |
| 60 | +ssh -L 18080:localhost:18080 root@116.203.242.63 |
| 61 | +# Then on the server: |
| 62 | +cd /root/home-energy-manager |
| 63 | +DAIKIN_REDIRECT_URI=http://localhost:18080/callback .venv/bin/python -m src.daikin.auth |
| 64 | +# Open the printed URL in your local browser, log in, approve |
| 65 | +``` |
| 66 | + |
| 67 | +**Option B — run auth, copy the code manually:** |
| 68 | +```bash |
| 69 | +cd /root/home-energy-manager |
| 70 | +.venv/bin/python -m src.daikin.auth --code CODE |
| 71 | +# where CODE is the `code=` param from the redirect URL |
| 72 | +``` |
| 73 | + |
| 74 | +After successful auth, new tokens are written to `DAIKIN_TOKEN_FILE` (i.e. `data/.daikin-tokens.json` when run from the project root with the env loaded). Restart the service. |
| 75 | + |
| 76 | +### Check current token state |
| 77 | + |
| 78 | +```bash |
| 79 | +python3 - <<'EOF' |
| 80 | +import json, datetime, time |
| 81 | +d = json.load(open("/root/home-energy-manager/data/.daikin-tokens.json")) |
| 82 | +print("obtained:", datetime.datetime.fromtimestamp(d["obtained_at"])) |
| 83 | +print("expires :", datetime.datetime.fromtimestamp(d["obtained_at"] + d["expires_in"])) |
| 84 | +print("expired :", time.time() > d["obtained_at"] + d["expires_in"]) |
| 85 | +print("has refresh_token:", bool(d.get("refresh_token"))) |
| 86 | +EOF |
| 87 | +``` |
| 88 | + |
| 89 | +### Daikin API daily rate limit |
| 90 | + |
| 91 | +- **Limit:** 200 requests/day, resets ~midnight UTC. |
| 92 | +- On 2026-04-18 the limit was exhausted during migration testing. |
| 93 | +- **`DAIKIN_HTTP_429_MAX_RETRIES=0`** is set in `.env` so the client fails fast on 429 instead of sleeping for `Retry-After` seconds (which Daikin sets to ~86400 on daily-limit exhaustion). Without this the server would hang for hours on startup. |
| 94 | +- When rate-limited, Daikin MCP tools return errors immediately. The service still starts and everything else (Fox ESS, Octopus, SQLite) works normally. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## Key `.env` settings to know |
| 99 | + |
| 100 | +``` |
| 101 | +DAIKIN_TOKEN_FILE=.daikin-tokens.json # relative to cwd → data/ path set in systemd service |
| 102 | +DAIKIN_HTTP_429_MAX_RETRIES=0 # fail fast on rate limit — do not remove |
| 103 | +OPENCLAW_READ_ONLY=false # allows hardware writes via MCP |
| 104 | +OPERATION_MODE=operational # operational = live hardware writes; simulation = dry-run |
| 105 | +DB_PATH=/root/home-energy-manager/data/energy_state.db # set in systemd service env |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## OpenClaw MCP integration |
| 111 | + |
| 112 | +OpenClaw (running at `http://127.0.0.1:18789`) connects to this project via two channels: |
| 113 | + |
| 114 | +1. **nikola MCP server** — started by openclaw via `/root/.openclaw/bin/nikola-mcp`: |
| 115 | + ```bash |
| 116 | + cd /root/home-energy-manager |
| 117 | + exec /root/home-energy-manager/.venv/bin/python -m src.mcp_server |
| 118 | + ``` |
| 119 | + This exposes 35 tools (Fox ESS, Daikin, Octopus tariffs, optimization) to the LLM. |
| 120 | + |
| 121 | +2. **Skills** — `/root/home-energy-manager/skills/` is loaded as an extra skill dir in openclaw. |
| 122 | + |
| 123 | +The MCP server is stateless (per-call); the API server (`home-energy-manager.service`) holds all state in SQLite. |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Project structure (key files) |
| 128 | + |
| 129 | +``` |
| 130 | +src/ |
| 131 | + cli/__main__.py # entrypoint: `python -m src.cli serve` |
| 132 | + api/main.py # FastAPI app + lifespan (DB init, recover_on_boot, scheduler) |
| 133 | + daikin/ |
| 134 | + auth.py # OAuth2 flow + token refresh |
| 135 | + client.py # DaikinClient (wraps Onecta API) |
| 136 | + state_machine.py # recover_on_boot, apply_safe_defaults |
| 137 | + config.py # all env-var config (Config dataclass) |
| 138 | + physics.py # DHW setpoint calculations (restored from git HEAD 2026-04-18) |
| 139 | + mcp_server.py # MCP server entrypoint (used by openclaw) |
| 140 | +data/ |
| 141 | + energy_state.db # SQLite (migrated from Docker volume 2026-04-18) |
| 142 | + .daikin-tokens.json # OAuth2 tokens (active) |
| 143 | +.env # secrets + config |
| 144 | +.venv/ # Python 3.12.3 venv (use this, not venv/) |
| 145 | +``` |
| 146 | + |
| 147 | +--- |
| 148 | + |
| 149 | +## What changed on 2026-04-18 (migration from Docker) |
| 150 | + |
| 151 | +- Docker container (`home-energy-manager-energy-manager-1`) removed |
| 152 | +- Docker volume data (`energy_state.db`, `.daikin-tokens.json`) migrated to `data/` |
| 153 | +- `home-energy-manager.service` rewritten: native `.venv/bin/python -m src.cli serve`, no Docker |
| 154 | +- `home-energy-manager` directory chowned to root (was uid 1000) |
| 155 | +- `physics.py` restored from git HEAD (working copy had `calculate_dhw_setpoint` etc. stripped) |
| 156 | +- `DAIKIN_HTTP_429_MAX_RETRIES=0` added to `.env` |
| 157 | +- Daikin access token refreshed (valid ~3h; refresh_token intact) |
0 commit comments