v2/crates/homecore-recorder/README.md
SQLite state-history recorder for HOMECORE with Home Assistant-compatible schema and optional ruvector semantic search (P2).
P1 release: SQLite database with Home Assistant-compatible schema for persistent state history. P2 (feature-gated): ruvector HNSW semantic index for natural-language queries ("show me all kitchen devices that were warm at 3 PM").
homecore-recorder persists HOMECORE state changes to SQLite and optionally indexes them for semantic search. It provides:
StateChanged eventsrecorder database schema (v48) for 1:1 compatibilitystates table and attributes to state_attributes table (same as HA)ruvector feature is offData persists in .homecore/home.db (by default; configurable). Queries work via standard SQLx, so any tool that reads SQLite can access the history.
recorder.db without schema changeslast_changed timestamp and old/new staterecorder_runs equivalent)--features ruvector) — embed state attributes + query by meaning| Capability | Type | Method | Notes |
|---|---|---|---|
| Record state change | Listener | RecorderListener::on_state_changed(event) | Fires on homecore event bus; writes to SQLite |
| Query state history | SQL | SELECT * FROM states WHERE entity_id = ? ORDER BY last_changed DESC | Standard SQLite; can be queried from anywhere |
| Purge old states | Maintenance | Recorder::purge(older_than) | Deletes states older than specified timestamp |
| Restore latest states | Startup | Recorder::restore_latest(states, limit) | Entity-id ordered, bounded, malformed-row isolation |
| Deduplicate write | Dedup | DedupEngine::should_record(old_state, new_state) | Skip if state hash unchanged |
| Create semantic index | Index | SemanticIndex::index_state(entity_id, state) (P2, opt-in) | Hash-based embeddings; real embeddings in P3 |
| Search by meaning | Search | SemanticIndex::search(query, k) (P2, opt-in) | "warm rooms" → k-NN search in ruvector HNSW |
| Aspect | Home Assistant | homecore-recorder |
|---|---|---|
| Database | SQLite (Python sqlite3) | SQLite (Rust sqlx) |
| Schema | recorder/ (schema v48) | Identical HA schema v48 |
| State table | states + state_attributes | Same dual-table layout |
| Persistence location | .homeassistant/home-assistant_v2.db | .homecore/home.db |
| Deduplication | Python stateful listener | DedupEngine + hash comparison |
| Purge policy | YAML auto_purge_* + retention | Configurable via Recorder::purge() |
| Semantic search | None (HA has YAML history stats only) | ruvector HNSW k-NN (P2, opt-in) |
| Schema compatibility | N/A | Bidirectional; can read HA's home.db directly |
Run cargo bench -p homecore-recorder --features ruvector for criterion benchmarks.
Recording state changes (P1):
use homecore_recorder::{Recorder, RecorderListener};
use homecore::HomeCore;
#[tokio::main]
async fn main() {
let homecore = HomeCore::new();
// Create the recorder (writes to .homecore/home.db)
let recorder = Recorder::new(".homecore/home.db").await.expect("init recorder");
// Create and spawn a listener
let listener = RecorderListener::new(recorder.clone());
let mut rx = homecore.event_bus().subscribe_system();
tokio::spawn(async move {
while let Ok(event) = rx.recv().await {
if let Err(e) = listener.on_state_changed(&event).await {
eprintln!("Recorder error: {}", e);
}
}
});
// State changes now persist to SQLite
}
Querying history directly (standard SQLite):
-- All light.kitchen state changes in the last hour
SELECT state, attributes, last_changed
FROM states
WHERE entity_id = 'light.kitchen'
AND last_changed > datetime('now', '-1 hour')
ORDER BY last_changed DESC;
-- Average brightness by hour
SELECT
strftime('%Y-%m-%d %H:00:00', last_changed) AS hour,
JSON_EXTRACT(attributes, '$.brightness') AS brightness
FROM states
WHERE entity_id = 'light.kitchen'
GROUP BY hour;
Semantic search (P2, with --features ruvector):
// (P2, not yet implemented)
// let index = SemanticIndex::new(recorder.clone()).await?;
// let results = index.search("find all warm rooms at 3pm", 5).await?;
// results.iter().for_each(|r| println!("{:?}", r));
homecore-recorder (state history + semantic search)
├─ homecore (state machine; listens to event bus)
├─ homecore-api (exposes recorder data via REST query endpoint, P3)
├─ homecore-automation (can trigger on historical state conditions, P3)
├─ homecore-server (starts the listener on init)
└─ ruvector-core (semantic index, P2, optional feature)