Skip to main content
SQLite functions query Home Assistant’s database to retrieve historical data, analyze patterns, and answer complex questions about entity states over time.
Security ConsiderationsSQLite functions provide direct database access. While connections are read-only, they can query data beyond exposed entities. Use validation to ensure queries only access intended data.

Configuration

string
required
SQL query to execute. Can include Jinja2 templates for parameters.

Database Schema

Home Assistant uses SQLite with these main tables:
Entity state records
Entity metadata
System events

Approach 1: AI-Generated Queries

Let the AI generate SQL queries with examples:
Get Last Changed Time
SQL Query 1
Get State at Specific Time
SQL Query 2
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:
The is_exposed_entity_in_query() function checks if the query references at least one exposed entity.

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

Check if an entity is exposed to the assistant
Check if query contains at least one exposed entity
Throw an error with a message

Time Handling

SQLite stores timestamps in UTC as Unix epochs. Convert to local time:

Set Timezone

Set the TZ 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

No. Database connections are opened in read-only mode. Queries can only SELECT data.
Yes, unless you use validation functions like is_exposed() or is_exposed_entity_in_query(). Choose the appropriate security level for your needs.
Set the TZ environment variable to your timezone, or use timezone offsets in queries:
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

1

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
2

Provide good examples

When using AI-generated queries, provide 2-3 clear examples in the description showing the expected SQL format.
3

Handle timezones

Always convert timestamps to local time for user-facing results.
4

Test queries

Test SQL queries directly in the database before adding to functions.

Debugging

Test queries directly:
Enable logging:

Next Steps

Native Functions

Use get_history for simpler needs

Composite Functions

Combine SQL with formatting

SQLite Documentation

SQLite reference