Configuration
SQL query to execute. Can include Jinja2 templates for parameters.
Database Schema
Home Assistant uses SQLite with these main tables:states
states
Entity state records
| Column | Type | Description |
|---|---|---|
| state_id | INTEGER | Primary key |
| metadata_id | INTEGER | Foreign key to states_meta |
| state | VARCHAR | Current state value |
| last_changed_ts | FLOAT | Timestamp of last change |
| last_updated_ts | FLOAT | Timestamp of last update |
| old_state_id | INTEGER | Previous state reference |
states_meta
states_meta
Entity metadata
| Column | Type | Description |
|---|---|---|
| metadata_id | INTEGER | Primary key |
| entity_id | VARCHAR | Entity identifier |
events
events
System events
| Column | Type | Description |
|---|---|---|
| event_id | INTEGER | Primary key |
| event_type | VARCHAR | Type of event |
| time_fired_ts | FLOAT | Timestamp |
| event_data | TEXT | JSON event data |
Approach 1: AI-Generated Queries
Let the AI generate SQL queries with examples:Get Last Changed Time
Get State at Specific Time
Flexibility vs Security: This approach is flexible but allows querying any entity, even unexposed ones.
Approach 2: Validated Queries
Add minimal validation to ensure exposed entities are used:Approach 3: Predefined Queries
Maximum security with predefined, validated queries:Security vs Flexibility: This approach is most secure but less flexible. The AI can only use predefined query patterns.
Template Functions
is_exposed(entity_id)
is_exposed(entity_id)
Check if an entity is exposed to the assistant
is_exposed_entity_in_query(query)
is_exposed_entity_in_query(query)
Check if query contains at least one exposed entity
raise(message)
raise(message)
Throw an error with a message
Time Handling
SQLite stores timestamps in UTC as Unix epochs. Convert to local time:Set Timezone
Set theTZ environment variable to your timezone:
Common Queries
Get Last State Change
Get State at Specific Time
Count State Changes
Use Cases
Historical Analysis
Analyze entity behavior patterns over time
State Tracking
Find when entities changed to specific states
Usage Statistics
Calculate usage statistics and trends
Event Correlation
Correlate events across multiple entities
FAQ
Can the AI modify or delete data?
Can the AI modify or delete data?
No. Database connections are opened in read-only mode. Queries can only SELECT data.
Can queries access unexposed entities?
Can queries access unexposed entities?
Yes, unless you use validation functions like
is_exposed() or is_exposed_entity_in_query(). Choose the appropriate security level for your needs.How do I handle timezones?
How do I handle timezones?
Set the
TZ environment variable to your timezone, or use timezone offsets in queries:Why use SQLite instead of get_history?
Why use SQLite instead of get_history?
SQLite queries provide:
- More flexible filtering and aggregation
- Better performance for complex queries
- Access to more detailed state information
- Ability to join multiple entities
Best Practices
Choose appropriate security
Select the security approach that matches your needs:
- Approach 1: Maximum flexibility, minimal security
- Approach 2: Balanced, validates exposed entities
- Approach 3: Maximum security, limited flexibility
Provide good examples
When using AI-generated queries, provide 2-3 clear examples in the description showing the expected SQL format.