2024-11-19 12:45:26 +03:00
# 🤖 HA Text AI for Home Assistant
2024-11-14 18:39:06 +03:00
2024-11-18 10:59:06 +03:00
<div align="center">
2024-11-19 12:45:26 +03:00


2024-11-18 11:19:28 +03:00

2024-11-19 12:45:26 +03:00

[](https://opensource.org/licenses/MIT)
2024-11-19 14:06:32 +03:00
[](https://github.com/hacs/integration)
[](https://community.home-assistant.io/t/ha-text-ai-integration)
2024-11-18 10:54:24 +03:00
2024-11-18 10:59:06 +03:00
</div>
<p align="center">
2024-11-19 14:06:32 +03:00
Transform your smart home experience with powerful AI assistance powered by OpenAI's GPT models. Get intelligent responses, automate complex scenarios, and enhance your home automation with natural language processing.
2024-11-18 10:59:06 +03:00
</p>
2024-11-18 10:54:24 +03:00
---
2024-11-14 18:39:06 +03:00
2024-11-19 12:45:26 +03:00
## 🌟 Features
2024-11-14 18:39:06 +03:00
2024-11-19 14:06:32 +03:00
- 🧠 **Advanced AI Integration** :
- Support for latest GPT models
- Context-aware responses
- Multi-turn conversations
- 💬 **Natural Language Control** :
- Control devices using everyday language
- Get detailed explanations and recommendations
- Natural conversation flow
- 📝 **Smart Memory Management** :
- Persistent conversation history
- Context-aware responses
- Customizable history limits
- ⚡ **Performance Optimized** :
- Efficient token usage
- Rate limit handling
- Response caching
- 🎯 **Advanced Customization** :
- Adjustable response parameters
- Custom system prompts
- Model selection per request
- 🔒 **Enhanced Security** :
- Secure API key storage
- Rate limiting protection
- Error handling
- 🎨 **User Experience** :
- Intuitive configuration UI
- Detailed sensor attributes
- Rich service interface
- 🔄 **Automation Integration** :
- Event-driven responses
- Conditional logic support
- Template compatibility
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
## 📋 Prerequisites
2024-11-18 10:59:06 +03:00
2024-11-19 14:06:32 +03:00
- Home Assistant 2023.8.0 or newer
2024-11-19 12:45:26 +03:00
- OpenAI API key ([Get one here ](https://platform.openai.com/account/api-keys ))
- Python 3.9 or newer
2024-11-19 14:06:32 +03:00
- Stable internet connection
2024-11-18 10:59:06 +03:00
2024-11-19 14:06:32 +03:00
## ⚡ Installation
### HACS Installation (Recommended)
1. Open HACS in Home Assistant
2. Click the "+" button
3. Search for "HA Text AI"
4. Click "Install"
5. Restart Home Assistant
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +03:00
### Manual Installation
2024-11-19 14:06:32 +03:00
1. Download the latest release
2. Extract and copy `custom_components/ha_text_ai` to your `custom_components` directory
2024-11-19 12:45:26 +03:00
3. Restart Home Assistant
2024-11-19 14:06:32 +03:00
4. Add configuration via UI or YAML
## ⚙️ Configuration
### Via UI (Recommended)
1. Go to Settings → Devices & Services
2. Click "Add Integration"
3. Search for "HA Text AI"
4. Follow the configuration steps
### Via YAML
2024-11-18 10:59:06 +03:00
```yaml
2024-11-19 00:51:57 +03:00
ha_text_ai :
2024-11-19 12:45:26 +03:00
api_key : !secret openai_api_key
2024-11-19 14:06:32 +03:00
model : gpt-3.5-turbo
temperature : 0.7
max_tokens : 1000
request_interval : 1.0
api_endpoint : https://api.openai.com/v1 # optional
2024-11-18 10:59:06 +03:00
```
2024-11-19 12:45:26 +03:00
## 🛠️ Available Services
### 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?"
2024-11-19 14:06:32 +03:00
model : "gpt-4o" # optional
2024-11-19 12:45:26 +03:00
temperature : 0.5 # optional
max_tokens : 500 # optional
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-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-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 :
limit : 5 # optional
```
2024-11-19 14:06:32 +03:00
## 🔧 Advanced Examples
2024-11-19 12:45:26 +03:00
2024-11-19 14:06:32 +03:00
### Smart Energy Management
2024-11-19 12:45:26 +03:00
```yaml
automation :
2024-11-19 14:06:32 +03:00
alias : "AI Energy Optimization"
2024-11-19 12:45:26 +03:00
trigger :
platform : time_pattern
2024-11-19 14:06:32 +03:00
hours : "/2"
2024-11-19 12:45:26 +03:00
action :
2024-11-19 14:06:32 +03:00
- service : ha_text_ai.ask_question
data :
question : >
Current power usage: {{ states('sensor.total_power') }}W
Temperature: {{ states('sensor.indoor_temperature') }}°C
Time: {{ now().strftime('%H:%M') }}
Occupancy: {{ states('binary_sensor.occupancy') }}
Analyze current energy usage and suggest optimizations
considering comfort and efficiency.
temperature : 0.3
max_tokens : 200
- service : notify.mobile_app
data :
message : "{{ states.sensor.ha_text_ai.attributes.response }}"
2024-11-19 12:45:26 +03:00
```
2024-11-19 14:06:32 +03:00
### Contextual Lighting Control
2024-11-18 10:59:06 +03:00
```yaml
automation :
2024-11-19 14:06:32 +03:00
alias : "AI Lighting Assistant"
2024-11-18 10:59:06 +03:00
trigger :
platform : state
2024-11-19 14:06:32 +03:00
entity_id : binary_sensor.motion
variables :
context : >
Time: {{ now().strftime('%H:%M') }}
Light Level: {{ states('sensor.illuminance') }}
Room: {{ trigger.to_state.attributes.room }}
Activity: {{ states('input_select.current_activity') }}
Weather: {{ states('weather.home') }}
2024-11-18 10:59:06 +03:00
action :
2024-11-19 14:06:32 +03:00
- service : ha_text_ai.ask_question
data :
question : >
Based on this context:
{{ context }}
Suggest optimal lighting settings for current conditions.
model : gpt-3.5-turbo
temperature : 0.4
- service : scene.turn_on
data :
entity_id : >
{{ states.sensor.ha_text_ai.attributes.response | regex_findall('scene\.[a-z_]+') | first }}
2024-11-18 10:59:06 +03:00
```
2024-11-19 14:06:32 +03:00
## 📊 Performance Optimization
2024-11-18 10:59:06 +03:00
2024-11-19 14:06:32 +03:00
### Token Usage
2024-11-19 12:45:26 +03:00
- Use focused system prompts
2024-11-19 14:06:32 +03:00
- Implement response caching
- Clear history periodically
- Monitor token usage
2024-11-18 10:59:06 +03:00
2024-11-19 14:06:32 +03:00
### Response Time
- Adjust request_interval
- Use faster models for simple queries
- Implement timeout handling
- Cache frequent responses
### Memory Management
- Set appropriate history limits
- Clear unused contexts
- Monitor memory usage
- Use efficient data structures
## ❗ Troubleshooting
### API Issues
2024-11-19 12:45:26 +03:00
- Verify API key validity
2024-11-19 14:06:32 +03:00
- Check rate limits
- Monitor usage quotas
- Test endpoint accessibility
### Performance Issues
- Reduce max_tokens
- Increase request_interval
- Clear conversation history
- Check network connectivity
### Integration Issues
- Verify HA version compatibility
- Check component dependencies
- Review log files
- Update configuration
## 📘 FAQ
**Q: How can I reduce API costs?**
A: Use GPT-3.5-Turbo for most queries, implement caching, and optimize token usage.
**Q: Is my data secure?**
A: Yes, API keys are stored securely and data is transmitted via encrypted connections.
**Q: Can I use custom models?**
A: Yes, configure custom endpoints and models via configuration options.
2024-11-18 10:59:06 +03:00
2024-11-19 12:45:26 +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
## 📝 License
2024-11-19 14:06:32 +03:00
MIT License - see [LICENSE ](LICENSE ) for details.
2024-11-14 18:39:06 +03:00
2024-11-18 10:54:24 +03:00
---
2024-11-14 18:39:06 +03:00
2024-11-18 10:59:06 +03:00
<div align="center">
2024-11-14 18:39:06 +03:00
2024-11-19 12:45:26 +03:00
Made with ❤️ 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>