2026-07-13 00:40:45 +03:00
# HA Text AI for Home Assistant
2024-11-14 18:39:06 +03:00
2024-11-19 19:03:51 +03:00
<div align="center">
2024-11-18 10:59:06 +03:00
2026-04-17 01:30:57 +03:00
  [](https://opensource.org/licenses/MIT) [](https://github.com/hacs/integration)
2025-01-29 00:57:27 +03:00
       
2024-12-10 23:27:18 +03:00
2024-11-19 19:30:42 +03:00
2025-09-02 02:06:58 +03:00
<img src="https://github.com/smkrv/ha-text-ai/blob/main/custom_components/ha_text_ai/icons/logo%402x.png" alt="HA Text AI" style="width: 50%; max-width: 256px; max-height: 128px; aspect-ratio: 2/1; object-fit: contain;"/>
2024-11-26 17:47:49 +03:00
2026-07-13 00:40:45 +03:00
### Multi-provider LLM integration for [Home Assistant](https://www.home-assistant.io/)
2024-11-18 10:59:06 +03:00
</div>
<p align="center">
2026-07-13 00:40:45 +03:00
Ask OpenAI, Anthropic Claude, DeepSeek and Google Gemini models questions from your automations and scripts. The integration keeps per-instance conversation history, returns full-length responses through response variables, supports structured JSON output, and exposes token, latency and error metrics as sensor attributes.
2024-11-26 00:14:26 +03:00
</p>
2024-11-26 17:47:49 +03:00
2024-11-26 00:14:26 +03:00
---
> [!IMPORTANT]
2026-07-13 00:40:45 +03:00
> Community driven: for more details on the integration,
2025-01-29 03:04:39 +03:00
> check out the discussion on the **[Home Assistant Community forum](https://community.home-assistant.io/t/ha-text-ai-transforming-home-automation-through-multi-llm-integration/799741)**
2024-11-26 00:14:26 +03:00
>
2025-01-29 03:05:47 +03:00
> <a href="https://my.home-assistant.io/redirect/hacs_repository/?owner=smkrv&repository=ha-text-ai&category=Integration"><img src="https://my.home-assistant.io/badges/hacs_repository.svg" width="210" height="auto"></a>
2024-11-29 01:27:01 +03:00
>
2024-12-06 16:13:27 +03:00
> [Screenshots](assets/images/screenshots/screenshot.jpg)
2024-11-25 16:40:55 +03:00
2026-07-13 00:40:45 +03:00
## Features
2024-11-14 18:39:06 +03:00
2026-07-13 00:40:45 +03:00
- **Multi-provider support**: OpenAI, Anthropic Claude, DeepSeek, Google Gemini, plus any OpenAI-compatible endpoint
- **Conversation context**: the model sees previous messages; depth is configurable per request (1-20)
- **Response variables**: `ask_question` returns the full response directly to the calling automation, bypassing the 255-character state limit
- **Structured output**: JSON responses matching a schema you provide
- **Per-request overrides**: model, temperature, max_tokens, system prompt, thinking mode
- **Usage metrics**: token counters, latency and success/error statistics as sensor attributes
- **File-based history**: per-instance JSON storage with automatic rotation at 1 MB
2024-11-22 02:00:42 +03:00
2026-07-13 00:40:45 +03:00
#### Translations
2024-12-06 16:35:26 +03:00
| Code | Language | Status |
|------|----------|--------|
2026-07-13 00:40:45 +03:00
| de | Deutsch | Full |
| en | English | Primary |
| es | Español | Full |
| hi | हिन्दी | Full |
| it | Italiano | Full |
| ru | Русский | Full |
| sr | Српски | Full |
| zh | 中文 | Full |
2024-12-06 16:35:26 +03:00
2026-07-13 00:40:45 +03:00
## Prerequisites
2024-11-18 10:59:06 +03:00
2026-07-13 00:40:45 +03:00
- Home Assistant 2024.12.0 or later
- An API key from one of:
2024-11-19 19:03:51 +03:00
- OpenAI ([Get key ](https://platform.openai.com/account/api-keys ))
- Anthropic ([Get key ](https://console.anthropic.com/ ))
2025-05-19 15:10:19 +03:00
- DeepSeek ([Get key ](https://platform.deepseek.com/api_keys ))
2024-11-25 16:57:14 +03:00
- OpenRouter ([Get key ](https://openrouter.ai/keys ))
2026-07-13 00:40:45 +03:00
- Google Gemini ([Get key ](https://ai.google.dev/gemini-api/docs/api-key )) thanks to ([@Azzedde ](https://github.com/Azzedde ))
2024-12-06 16:46:11 +03:00
- Any OpenAI-compatible API provider
2024-11-18 10:59:06 +03:00
2024-12-05 01:54:09 +03:00
## Configuration Options
2024-11-22 01:56:36 +03:00
2026-07-13 00:40:45 +03:00
### Core Configuration Settings
- **API Provider**: OpenAI / Anthropic / DeepSeek / Gemini
- **API Key**: provider-specific authentication
- **Model**: any model your provider offers
- **Temperature**: sampling temperature (0.0-2.0)
- **Max Tokens**: response length cap, passed to the LLM API
- **Request Interval**: minimum delay between API calls (seconds)
- **History Size**: number of conversations to retain
- **Custom API Endpoint**: for OpenRouter, proxies and self-hosted servers
- **Disable Thinking**: turn off model reasoning where the provider supports it
- **Allow Local Network**: permit endpoints on private addresses (needed for local servers like Ollama)
2024-11-25 16:34:36 +03:00
2026-07-13 00:40:45 +03:00
### Recommended Models
2025-09-02 02:06:58 +03:00
#### OpenAI Models
2026-07-13 00:40:45 +03:00
- **GPT-5.6 Sol** - flagship tier for the hardest tasks
- **GPT-5.6 Terra** - mid-tier for high-volume tasks
- **GPT-5.6 Luna** - fastest and cheapest, enough for most home automation queries
2025-09-02 02:06:58 +03:00
2026-03-12 12:05:43 +03:00
#### Anthropic Claude Models
2026-07-13 00:40:45 +03:00
- **Claude Fable 5** - the most capable model for complex tasks
- **Claude Sonnet 5** - balance between quality and cost
- **Claude Haiku 4.5** - the fastest and cheapest option in the lineup
2025-09-02 02:06:58 +03:00
#### DeepSeek Models
2026-07-13 00:40:45 +03:00
- **deepseek-v4-flash** - fast general-purpose model (default)
- **deepseek-v4-pro** - stronger at reasoning and coding
2026-07-07 01:22:19 +03:00
> The legacy model names `deepseek-chat` and `deepseek-reasoner` stop working on 2026-07-24. If your instance still uses one of them, switch the model in the integration options.
2025-09-02 02:06:58 +03:00
#### Google Gemini Models
2026-07-13 00:40:45 +03:00
- **gemini-3.5-flash** - default; Google's strongest currently available model
- **gemini-3.1-pro** - previous flagship, still supported
2026-07-07 01:22:19 +03:00
> Google shut down `gemini-2.0-flash` on 2026-06-01 and retires the 2.5 family on 2026-10-16. If your instance uses one of those, switch the model in the integration options.
2025-09-02 02:06:58 +03:00
2024-12-05 01:54:09 +03:00
<details>
2026-07-13 00:40:45 +03:00
<summary>Potentially Compatible Providers</summary>
2024-12-05 01:54:09 +03:00
2026-07-13 00:40:45 +03:00
Other providers with OpenAI-compatible APIs may work through the custom endpoint option:
2024-12-05 01:54:09 +03:00
- Groq
- Together AI
- Perplexity AI
- Mistral AI
2026-07-13 00:40:45 +03:00
- Local AI servers (like Ollama - enable **Allow Local Network** in the options)
2024-12-05 01:54:09 +03:00
- Custom OpenAI-compatible endpoints
2026-07-13 00:40:45 +03:00
Compatibility is not guaranteed. A provider needs an OpenAI-like REST API with JSON request/response format, standard bearer authentication and similar parameter handling. Check the provider's documentation and make sure your API key has sufficient quota.
2024-12-06 16:33:46 +03:00
2024-12-05 01:54:09 +03:00
</details>
2024-11-25 16:34:36 +03:00
2026-07-13 00:40:45 +03:00
## Installation
2024-11-19 14:06:32 +03:00
2024-11-22 01:56:36 +03:00
### HACS Installation (Recommended)
2025-05-19 15:10:19 +03:00
>[!TIP]
>HA Text AI is available in the default HACS repository. You can install it directly through HACS or click the button below to open it there.
2024-11-25 17:10:38 +03:00
<a href="https://my.home-assistant.io/redirect/hacs_repository/?owner=smkrv&repository=ha-text-ai&category=Integration"><img src="https://my.home-assistant.io/badges/hacs_repository.svg" width="170" height="auto"></a>
2024-11-22 01:56:36 +03:00
1. Open HACS in Home Assistant
2026-07-13 00:40:45 +03:00
2. Search for "HA Text AI"
3. Click "Download"
4. Restart Home Assistant
2025-09-02 02:11:34 +03:00
**Alternative Method (Custom Repository):**
If the integration is not found in the default repository:
1. Click "..." in top right corner of HACS
2. Select "Custom repositories"
3. Add repository URL: `https://github.com/smkrv/ha-text-ai`
4. Choose "Integration" as category
5. Click "Download"
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
### Manual Installation
2026-07-07 01:22:19 +03:00
1. Download `ha_text_ai.zip` from the latest release
2. Extract the archive and copy the `ha_text_ai` folder into your `custom_components` directory
2024-11-19 12:45:26 +03:00
3. Restart Home Assistant
2026-07-13 00:40:45 +03:00
4. Add configuration via UI (Settings > Devices & Services > Add Integration)
2024-11-19 14:06:32 +03:00
2026-07-13 00:40:45 +03:00
## Configuration
2024-11-19 14:06:32 +03:00
### Via UI (Recommended)
2026-07-13 00:40:45 +03:00
1. Go to Settings > Devices & Services
2024-11-19 14:06:32 +03:00
2. Click "Add Integration"
3. Search for "HA Text AI"
4. Follow the configuration steps
2026-03-12 12:05:43 +03:00
> **Note:** This integration is configured exclusively through the UI (config entries). YAML configuration is not supported.
2024-12-05 01:54:09 +03:00
2026-07-13 00:40:45 +03:00
## Available Services
2024-11-19 12:45:26 +03:00
2026-07-13 00:40:45 +03:00
### Response Variables
2025-09-01 17:14:23 +03:00
2026-07-13 00:40:45 +03:00
`ask_question` returns its result directly to the calling automation via `response_variable` . The full response text comes back regardless of length (no 255-character truncation), it is available immediately without polling sensor state, and each service call gets its own result, so parallel automations don't overwrite each other.
2025-09-01 17:14:23 +03:00
2024-11-19 12:45:26 +03:00
### ask_question
2024-11-18 10:59:06 +03:00
```yaml
2024-11-19 00:51:57 +03:00
service : ha_text_ai.ask_question
2024-11-18 10:59:06 +03:00
data :
2024-11-19 12:45:26 +03:00
question : "What's the optimal temperature for sleeping?"
2026-07-13 00:40:45 +03:00
instance : sensor.ha_text_ai_claude
model : "claude-sonnet-5" # optional, overrides the configured model
2024-11-19 12:45:26 +03:00
temperature : 0.5 # optional
max_tokens : 500 # optional
2026-07-13 00:40:45 +03:00
context_messages : 10 # optional, previous messages to include (1-20, default 5)
2024-11-19 19:03:51 +03:00
system_prompt : "You are a sleep optimization expert" # optional
2026-07-13 00:40:45 +03:00
disable_thinking : true # optional, disable model reasoning for this request
response_variable : ai_response
2025-09-01 17:14:23 +03:00
```
2026-07-13 00:40:45 +03:00
For structured JSON output, add `structured_output` with a schema:
```yaml
service : ha_text_ai.ask_question
data :
question : "Suggest three energy-saving actions for tonight"
instance : sensor.ha_text_ai_gpt
structured_output : true
json_schema : >-
{"type": "object", "properties": {"actions": {"type": "array", "items": {"type": "string"}}}}
response_variable : ai_response
```
#### Response Data Structure
2025-09-01 17:14:23 +03:00
```yaml
# The service returns structured data:
response_text : "The optimal sleeping temperature is 65-68°F (18-20°C)..."
tokens_used : 150
prompt_tokens : 50
completion_tokens : 100
2026-07-13 00:40:45 +03:00
model_used : "claude-sonnet-5"
instance : "sensor.ha_text_ai_claude"
2025-09-01 17:14:23 +03:00
question : "What's the optimal temperature for sleeping?"
2026-07-13 00:40:45 +03:00
timestamp : "2026-07-09T16:57:00.000Z"
2025-09-01 17:14:23 +03:00
success : true
2026-07-13 00:40:45 +03:00
# error and error_type are present only when success is false
2024-11-18 10:59:06 +03:00
```
2024-11-19 12:45:26 +03:00
### set_system_prompt
```yaml
service : ha_text_ai.set_system_prompt
data :
2024-12-07 00:48:17 +03:00
instance : sensor.ha_text_ai_gpt
2024-11-19 14:06:32 +03:00
prompt : |
You are a home automation expert focused on:
1. Energy efficiency
2. Comfort optimization
3. Security considerations
Provide practical, actionable advice.
2024-11-19 12:45:26 +03:00
```
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
### clear_history
```yaml
service : ha_text_ai.clear_history
2024-12-10 23:33:32 +03:00
data :
instance : sensor.ha_text_ai_gpt
2024-11-19 12:45:26 +03:00
```
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
### get_history
```yaml
service : ha_text_ai.get_history
data :
2026-07-07 01:38:27 +03:00
limit : 5 # optional, number of conversations to return (values above 200 are clamped); omit to get the full stored history
2025-09-02 02:11:34 +03:00
filter_model : "gpt-4o" # optional, filter by specific AI model
2026-07-13 00:40:45 +03:00
start_date : "2026-02-01" # optional, filter conversations from this date
2025-09-02 02:11:34 +03:00
include_metadata : false # optional, include tokens, response time, etc.
sort_order : "newest" # optional, sort order: "newest" or "oldest"
2024-12-10 23:33:32 +03:00
instance : sensor.ha_text_ai_gpt
2026-07-07 01:38:27 +03:00
response_variable : history_result # entries are in history_result.history
2024-11-19 12:45:26 +03:00
```
2026-07-13 00:40:45 +03:00
## Automation Examples with Response Variables
2025-09-01 17:14:23 +03:00
### Example 1: Smart Home Advice with Direct Response
```yaml
automation :
- alias : "Get AI Home Advice"
trigger :
- platform : state
entity_id : input_button.ask_ai_advice
action :
- service : ha_text_ai.ask_question
data :
question : "What's the best way to optimize energy usage in my home?"
instance : sensor.ha_text_ai_gpt
response_variable : ai_advice
- service : notify.mobile_app
data :
2026-07-13 00:40:45 +03:00
title : "Smart Home Tip"
2025-09-01 17:14:23 +03:00
message : |
{{ ai_advice.response_text }}
2026-07-13 00:40:45 +03:00
Tokens used: {{ ai_advice.tokens_used }}
Model: {{ ai_advice.model_used }}
2025-09-01 17:14:23 +03:00
```
### Example 2: Weather-Based AI Recommendations
```yaml
automation :
- alias : "Weather-Based AI Suggestions"
trigger :
- platform : numeric_state
entity_id : sensor.outdoor_temperature
below : 0
action :
- service : ha_text_ai.ask_question
data :
question : |
The outdoor temperature is {{ states('sensor.outdoor_temperature') }}°C.
What should I do to prepare my home for freezing weather?
system_prompt : "You are a home maintenance expert. Provide practical, actionable advice."
instance : sensor.ha_text_ai_gpt
response_variable : winter_advice
- if :
- condition : template
value_template : "{{ winter_advice.success }}"
then :
- service : persistent_notification.create
data :
2026-07-13 00:40:45 +03:00
title : "Winter Preparation Advice"
2025-09-01 17:14:23 +03:00
message : |
{{ winter_advice.response_text }}
Generated at: {{ winter_advice.timestamp }}
else :
- service : persistent_notification.create
data :
2026-07-13 00:40:45 +03:00
title : "AI Service Error"
2025-09-01 17:14:23 +03:00
message : "Failed to get winter advice: {{ winter_advice.error }}"
```
### Example 3: Multi-Step AI Workflow
```yaml
automation :
- alias : "Multi-Step AI Analysis"
trigger :
- platform : state
entity_id : input_button.analyze_home_status
action :
# Step 1: Get current status analysis
- service : ha_text_ai.ask_question
data :
question : |
Current home status:
- Temperature: {{ states('sensor.indoor_temperature') }}°C
- Humidity: {{ states('sensor.indoor_humidity') }}%
- Energy usage: {{ states('sensor.power_consumption') }}W
Analyze this data and provide insights.
instance : sensor.ha_text_ai_gpt
response_variable : status_analysis
# Step 2: Get recommendations based on analysis
- service : ha_text_ai.ask_question
data :
question : |
Based on this analysis: "{{ status_analysis.response_text[:500] }}"
Provide 3 specific actionable recommendations for improvement.
context_messages : 2 # Include previous conversation
instance : sensor.ha_text_ai_gpt
response_variable : recommendations
2026-07-13 00:40:45 +03:00
# Step 3: Send the combined report
2025-09-01 17:14:23 +03:00
- service : notify.telegram
data :
2026-07-13 00:40:45 +03:00
title : "Home Analysis Report"
2025-09-01 17:14:23 +03:00
message : |
**Analysis:**
{{ status_analysis.response_text }}
**Recommendations:**
{{ recommendations.response_text }}
**Report Details:**
- Total tokens used: {{ status_analysis.tokens_used + recommendations.tokens_used }}
- Analysis model: {{ status_analysis.model_used }}
- Generated: {{ recommendations.timestamp }}
```
2026-07-13 00:40:45 +03:00
### Migration from Sensors to Response Variables
2025-09-01 17:14:23 +03:00
2026-07-13 00:40:45 +03:00
#### Old Method:
2025-09-01 17:14:23 +03:00
```yaml
2026-07-13 00:40:45 +03:00
# Old way: delay-based polling, response truncated by the 255-character state limit
2025-09-01 17:14:23 +03:00
automation :
- alias : "Old AI Response Method"
action :
- service : ha_text_ai.ask_question
data :
question : "Long question here..."
instance : sensor.ha_text_ai_gpt
- delay : "00:00:05" # Wait for sensor update
- service : notify.mobile
data :
2026-07-13 00:40:45 +03:00
message : "{{ state_attr('sensor.ha_text_ai_gpt', 'response')[:255] }}..." # Truncated
2025-09-01 17:14:23 +03:00
```
2026-07-13 00:40:45 +03:00
#### New Method:
2025-09-01 17:14:23 +03:00
```yaml
2026-07-13 00:40:45 +03:00
# New way: full response, available immediately
2025-09-01 17:14:23 +03:00
automation :
- alias : "New AI Response Method"
action :
- service : ha_text_ai.ask_question
data :
question : "Long question here..."
instance : sensor.ha_text_ai_gpt
2026-07-13 00:40:45 +03:00
response_variable : ai_response
2025-09-01 17:14:23 +03:00
- service : notify.mobile
data :
2026-07-13 00:40:45 +03:00
message : "{{ ai_response.response_text }}" # Full response, no truncation
2025-09-01 17:14:23 +03:00
```
2026-07-13 00:40:45 +03:00
### HA Text AI Sensor Naming Convention
2024-11-25 17:59:17 +03:00
2026-07-13 00:40:45 +03:00
#### Naming Rules
- Only lowercase letters (a-z), numbers (0-9) and underscore (_)
- The part after the `sensor.ha_text_ai_` prefix is limited to 50 characters
- No spaces; keep it descriptive but short
2024-11-28 23:27:39 +03:00
2024-11-25 17:59:17 +03:00
#### Sensor Name Structure
```yaml
# Always starts with 'sensor.ha_text_ai_'
2026-07-13 00:40:45 +03:00
# You define only the part after the prefix
2024-11-25 17:59:17 +03:00
sensor.ha_text_ai_YOUR_UNIQUE_SUFFIX
# Examples:
sensor.ha_text_ai_gpt # GPT-based sensor
sensor.ha_text_ai_claude # Claude-based sensor
2024-12-05 01:55:31 +03:00
sensor.ha_text_ai_abc # Custom suffix
2024-11-25 17:59:17 +03:00
```
#### Response Retrieval
```yaml
# Use your specific sensor name
{{ state_attr('sensor.ha_text_ai_gpt', 'response') }}
```
#### Practical Usage
```yaml
automation :
- alias : "AI Response with Custom Sensor"
action :
- service : ha_text_ai.ask_question
data :
question : "Home automation advice"
2024-12-07 00:48:17 +03:00
instance : sensor.ha_text_ai_gpt
2024-11-25 17:59:17 +03:00
- service : notify.mobile
data :
message : >
AI Tip:
{{ state_attr('sensor.ha_text_ai_gpt', 'response') }}
```
2026-07-13 00:40:45 +03:00
### HA Text AI Sensor Attributes
2024-11-25 17:59:17 +03:00
2026-07-13 00:40:45 +03:00
- **Model and provider**: current model, API provider, model used for the last response
- **System status**: processing, rate-limit and endpoint state
- **Performance metrics**: request success/failure counters and latency statistics
- **Token usage**: total, prompt and completion token counters as reported by the provider's API
- **Last interaction**: most recent question, response and timestamp
- **System health**: error counter, maintenance flag, uptime
2024-11-25 17:59:17 +03:00
2026-07-13 00:40:45 +03:00
Attributes may be 0 or empty until the first request completes.
2024-12-05 02:00:24 +03:00
<details>
2026-07-13 00:40:45 +03:00
<summary>Detailed Sensor Attributes</summary>
2024-12-05 02:00:24 +03:00
2024-11-25 17:59:17 +03:00
#### Model and Provider Information
```yaml
2026-07-13 00:40:45 +03:00
# Model currently configured for this instance
{{ state_attr('sensor.ha_text_ai_gpt', 'model') }} # gpt-4o-mini
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Service provider (determines API endpoint and authentication)
{{ state_attr('sensor.ha_text_ai_gpt', 'api_provider') }} # openai
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Model that produced the last response (may differ after a per-request override)
{{ state_attr('sensor.ha_text_ai_gpt', 'last_model') }} # gpt-4o-mini
2024-11-25 17:59:17 +03:00
```
#### System Status
```yaml
2026-03-12 12:05:43 +03:00
# Indicates if a request is currently being processed
2026-07-13 00:40:45 +03:00
{{ state_attr('sensor.ha_text_ai_gpt', 'is_processing') }} # false
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Shows if the API has hit its request rate limit
{{ state_attr('sensor.ha_text_ai_gpt', 'is_rate_limited') }} # false
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Status of the API endpoint being used
{{ state_attr('sensor.ha_text_ai_gpt', 'endpoint_status') }} # ready
2024-11-25 17:59:17 +03:00
```
#### Performance Metrics
```yaml
2026-07-13 00:40:45 +03:00
# Number of successfully completed API requests
{{ state_attr('sensor.ha_text_ai_gpt', 'successful_requests') }} # 42
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Number of API requests that encountered errors
{{ state_attr('sensor.ha_text_ai_gpt', 'failed_requests') }} # 0
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Average / max / min response time, in seconds
{{ state_attr('sensor.ha_text_ai_gpt', 'average_latency') }} # 1.85
{{ state_attr('sensor.ha_text_ai_gpt', 'max_latency') }} # 4.2
{{ state_attr('sensor.ha_text_ai_gpt', 'min_latency') }} # 0.9
2024-11-25 17:59:17 +03:00
```
#### Conversation and Token Usage
```yaml
2026-07-13 00:40:45 +03:00
# Number of entries in the current history file
{{ state_attr('sensor.ha_text_ai_gpt', 'history_size') }} # 12
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Token counters as reported by the provider's API
{{ state_attr('sensor.ha_text_ai_gpt', 'total_tokens') }} # 4520
{{ state_attr('sensor.ha_text_ai_gpt', 'prompt_tokens') }} # 3100
{{ state_attr('sensor.ha_text_ai_gpt', 'completion_tokens') }} # 1420
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Last 3 conversation entries, each truncated to 256 characters
# (full history is available via the get_history service)
{{ state_attr('sensor.ha_text_ai_gpt', 'conversation_history') }} # [...]
2024-11-25 17:59:17 +03:00
```
#### Last Interaction Details
```yaml
2026-07-13 00:40:45 +03:00
# Most recent response, truncated to 2048 characters in the attribute
# (the response_variable path returns the full text)
{{ state_attr('sensor.ha_text_ai_gpt', 'response') }} # Last AI response
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# The most recently processed question
{{ state_attr('sensor.ha_text_ai_gpt', 'question') }} # Last asked question
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# When the last interaction occurred
{{ state_attr('sensor.ha_text_ai_gpt', 'last_timestamp') }} # Timestamp
2024-11-25 17:59:17 +03:00
```
#### System Health
```yaml
2026-07-13 00:40:45 +03:00
# Cumulative count of errors across all requests
{{ state_attr('sensor.ha_text_ai_gpt', 'total_errors') }} # 0
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Error message of the last failed request (null after a success)
{{ state_attr('sensor.ha_text_ai_gpt', 'last_error') }} # null
2024-11-26 15:01:40 +03:00
2026-07-13 00:40:45 +03:00
# Maintenance flag
{{ state_attr('sensor.ha_text_ai_gpt', 'is_maintenance') }} # false
# Seconds since the integration instance was set up
{{ state_attr('sensor.ha_text_ai_gpt', 'uptime') }} # 547.58
2024-11-25 17:59:17 +03:00
```
2024-12-03 18:29:24 +03:00
### History Storage
Conversation history stored in `.storage/ha_text_ai_history/` directory:
2024-12-06 02:53:41 +03:00
- Each instance has its own history file (JSON)
2024-12-03 18:29:24 +03:00
- Files are automatically rotated when size limit is reached
- Archived history files are timestamped
- Default maximum file size: 1MB
2024-12-05 02:00:24 +03:00
</details>
2024-11-25 17:59:17 +03:00
2026-07-13 00:40:45 +03:00
## FAQ
2024-11-19 14:06:32 +03:00
2024-11-19 19:03:51 +03:00
**Q: Which AI providers are supported?**
2026-07-13 00:40:45 +03:00
A: OpenAI, Anthropic, DeepSeek and Google Gemini are built-in providers. OpenRouter and other OpenAI-compatible services work through the OpenAI provider with a custom endpoint.
2024-11-19 19:03:51 +03:00
**Q: How can I reduce API costs?**
2026-07-13 00:40:45 +03:00
A: Use a cheap fast model (GPT-5.6 Luna, Claude Haiku 4.5, deepseek-v4-flash, gemini-3.5-flash) for routine queries, lower `context_messages` , and cap `max_tokens` .
2024-11-19 19:03:51 +03:00
2024-11-22 01:56:36 +03:00
**Q: Are there limitations on the number of requests?**
2026-07-13 00:40:45 +03:00
A: Depends on your API provider's plan. Monitor usage via the sensor attributes and throttle calls with the `request_interval` option.
2024-11-19 19:31:26 +03:00
2024-11-22 01:56:36 +03:00
**Q: Can I use custom models?**
2024-11-19 19:03:51 +03:00
A: Yes, you can configure custom endpoints and use any compatible model by specifying it in the configuration.
**Q: How do I switch between different AI providers?**
2026-07-13 00:40:45 +03:00
A: Each integration instance is bound to one provider. Add a separate instance per provider and pick the instance in your service calls; within an instance you can override the model per request.
2024-11-19 19:03:51 +03:00
2025-09-02 02:06:58 +03:00
**Q: What are the token limits for different models?**
2026-07-13 00:40:45 +03:00
A: Context window sizes vary by provider and model - check your provider's documentation. The `max_tokens` option caps only the response length, not the context window.
2025-09-02 02:06:58 +03:00
**Q: How do I monitor token usage?**
2026-07-13 00:40:45 +03:00
A: Use the sensor attributes `total_tokens` , `prompt_tokens` and `completion_tokens` . You can also create automations to alert you when usage exceeds a threshold.
2024-11-19 14:06:32 +03:00
**Q: Is my data secure?**
2026-07-13 00:40:45 +03:00
A: Conversation history and API keys are stored locally in your Home Assistant instance. Questions and context are sent to the provider you configure over HTTPS; nothing is shared with third parties beyond that provider.
2024-11-19 14:06:32 +03:00
2024-11-25 15:42:04 +03:00
**Q: How do context messages work?**
2026-07-13 00:40:45 +03:00
A: Context messages let the AI reference previous conversation history. By default 5 previous messages are included; you can set 1 to 20 per request to balance conversation depth against token usage.
2024-11-25 15:42:04 +03:00
2024-12-03 18:29:24 +03:00
**Q: Where is conversation history stored?**
A: History is stored in files under the `.storage/ha_text_ai_history/` directory, with automatic rotation and size management.
**Q: Can I access old conversation history?**
A: Yes, archived history files are stored with timestamps and can be accessed manually if needed.
**Q: How much history is kept?**
2026-07-13 00:40:45 +03:00
A: 50 conversations by default, configurable up to 100 in the UI. Files are automatically rotated when they reach 1MB.
2024-12-04 23:28:14 +03:00
2026-07-13 00:40:45 +03:00
## Contributing
2024-11-18 10:59:06 +03:00
2024-11-19 14:06:32 +03:00
Contributions welcome! Please read our [Contributing Guide ](CONTRIBUTING.md ).
2024-11-18 10:59:06 +03:00
1. Fork the repository
2024-11-19 14:06:32 +03:00
2. Create feature branch (`git checkout -b feature/Enhancement` )
3. Commit changes (`git commit -m 'Add Enhancement'` )
4. Push branch (`git push origin feature/Enhancement` )
5. Open Pull Request
2024-11-19 12:45:26 +03:00
2024-11-29 16:27:04 +03:00
## Legal Disclaimer and Limitation of Liability
### Software Disclaimer
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
2026-07-13 00:40:45 +03:00
## License
2024-11-19 12:45:26 +03:00
2024-11-29 16:27:04 +03:00
Author: SMKRV
2026-04-17 01:30:57 +03:00
[MIT License ](https://opensource.org/licenses/MIT ) - see [LICENSE ](LICENSE ) for details.
2024-11-14 18:39:06 +03:00
2026-07-13 00:40:45 +03:00
## Support the Project
2024-11-26 00:09:46 +03:00
The best support is:
- Sharing feedback
- Contributing ideas
- Recommending to friends
- Reporting issues
- Star the repository
If you want to say thanks financially, you can send a small token of appreciation in USDT:
**USDT Wallet (TRC10/TRC20):**
`TXC9zYHYPfWUGi4Sv4R1ctTBGScXXQk5HZ`
2024-11-18 10:54:24 +03:00
---
2024-11-14 18:39:06 +03:00
2024-12-06 16:13:27 +03:00
<div align="center"><img src="https://github.com/smkrv/ha-text-ai/blob/2aaf3405759eb2d97624834594e24ace896131df/assets/images/icons/footer_icon.png" alt="HA Text AI" style="width: 128px; height: auto;"/></div>
2024-11-18 10:59:06 +03:00
<div align="center">
2024-11-14 18:39:06 +03:00
2026-07-13 00:40:45 +03:00
Made for the Home Assistant Community
2024-11-14 18:39:06 +03:00
2024-11-19 14:06:32 +03:00
[Report Bug ](https://github.com/smkrv/ha-text-ai/issues ) · [Request Feature ](https://github.com/smkrv/ha-text-ai/issues )
2024-11-18 10:59:06 +03:00
</div>