mirror of
https://github.com/smkrv/ha-text-ai.git
synced 2026-07-21 22:54:00 +08:00
93558b2444be0bf9ac1d5a6717af944d0ea449a4
🤖 HA Text AI for Home Assistant
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.
🌟 Features
- 🧠 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
📋 Prerequisites
- Home Assistant 2023.8.0 or newer
- OpenAI API key (Get one here)
- Python 3.9 or newer
- Stable internet connection
⚡ Installation
HACS Installation (Recommended)
- Open HACS in Home Assistant
- Click the "+" button
- Search for "HA Text AI"
- Click "Install"
- Restart Home Assistant
Manual Installation
- Download the latest release
- Extract and copy
custom_components/ha_text_aito yourcustom_componentsdirectory - Restart Home Assistant
- Add configuration via UI or YAML
⚙️ Configuration
Via UI (Recommended)
- Go to Settings → Devices & Services
- Click "Add Integration"
- Search for "HA Text AI"
- Follow the configuration steps
Via YAML
ha_text_ai:
api_key: !secret openai_api_key
model: gpt-3.5-turbo
temperature: 0.7
max_tokens: 1000
request_interval: 1.0
api_endpoint: https://api.openai.com/v1 # optional
🛠️ Available Services
ask_question
service: ha_text_ai.ask_question
data:
question: "What's the optimal temperature for sleeping?"
model: "gpt-4o" # optional
temperature: 0.5 # optional
max_tokens: 500 # optional
set_system_prompt
service: ha_text_ai.set_system_prompt
data:
prompt: |
You are a home automation expert focused on:
1. Energy efficiency
2. Comfort optimization
3. Security considerations
Provide practical, actionable advice.
clear_history
service: ha_text_ai.clear_history
get_history
service: ha_text_ai.get_history
data:
limit: 5 # optional
🔧 Advanced Examples
Smart Energy Management
automation:
alias: "AI Energy Optimization"
trigger:
platform: time_pattern
hours: "/2"
action:
- 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 }}"
Contextual Lighting Control
automation:
alias: "AI Lighting Assistant"
trigger:
platform: state
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') }}
action:
- 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 }}
📊 Performance Optimization
Token Usage
- Use focused system prompts
- Implement response caching
- Clear history periodically
- Monitor token usage
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
- Verify API key validity
- 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.
🤝 Contributing
Contributions welcome! Please read our Contributing Guide.
- Fork the repository
- Create feature branch (
git checkout -b feature/Enhancement) - Commit changes (
git commit -m 'Add Enhancement') - Push branch (
git push origin feature/Enhancement) - Open Pull Request
📝 License
MIT License - see LICENSE for details.
Made with ❤️ for the Home Assistant Community
Languages
Python
100%