AI Automation Suggester

repository·main·Indexed 20 days ago

https://github.com/itspecialist111/ai_automation_suggester

An AI-powered assistant for Home Assistant that analyzes entities, areas, devices, and existing automations to propose actionable YAML automation snippets. It supports multiple AI providers including OpenAI, Anthropic, Google Gemini, Groq, Mistral AI, Perplexity AI, OpenRouter, Requesty, LiteLLM, and local options like LocalAI and Ollama. Suggestions are delivered via persistent notifications and sensors, which can be displayed on Home Assistant dashboards.

Tokens
5.4K
Snippets
4
Records
27
Agent score
23%

What's inside AI Automation Suggester

  1. Overview of AI Automation Suggester Capabilities

    main

    The AI Automation Suggester is a Home Assistant custom integration that generates automation suggestions by analyzing the context of entities, devices, areas, and existing automations.

    Core Features:

    • Sensor Platform: Exposes a sensor for automation suggestions.
    • Service: Provides a generate_suggestions service.
    • Notifications: Uses persistent notifications for user alerts.
    • Configuration: Supports configuration and options flows for setup.
    • UI: Includes a custom Lovelace card for displaying suggestions.
    • Multi-Provider Support: Directly supports a wide range of AI providers including OpenAI, Azure OpenAI, Anthropic, Google, Groq, LocalAI, Ollama, Mistral, Perplexity, and OpenRouter.
  2. Planned Roadmap and Feature Phases

    main

    The project follows a multi-phase upgrade plan to improve maintainability, reliability, and user experience.

    • Phase 0 (Triage): Focuses on maintenance, GitHub organization, and establishing baselines.
    • Phase 1 (Safety & Compatibility): Focuses on regression prevention, increasing test coverage (pytest), and fixing configuration migration issues.
    • Phase 2 (Provider Modernization): Introduces a provider abstraction to move logic out of coordinator.py, adding support for custom OpenAI-compatible URLs, local provider timeouts (Ollama/Open WebUI), and updated model defaults for Google, Anthropic, Groq, Mistral, and Perplexity.
    • Phase 3 (Context & Structured Output): Introduces include/exclude filters for Home Assistant entities/domains, custom system prompts, and structured JSON output for more reliable automation parsing.
    • Phase 4 (UX & HA-Native Workflows): Focuses on Home Assistant integration, including new services (dismiss_suggestion, rate_suggestion, etc.) and potential integration with AI Task/AI Buttons.
    • Phase 5 (Documentation): Focuses on comprehensive guides for providers, privacy notes regarding data sent to cloud providers, and model compatibility documentation.
  3. Understand the Suggestion Pipeline Workflow

    main

    The suggestion generation process is being refactored into a modular pipeline to improve reliability and prevent issues like truncated output or missing YAML. The pipeline consists of:

    1. Context Collection (context.py): Gathers entities, areas, devices, existing automations, and optional YAML content.
    2. Filtering (filters.py): Applies inclusion/exclusion rules for domains, areas, devices, entities, and availability status.
    3. Prompt Construction (prompt.py): Builds prompt sections, selects the target language, enforces input budgets, and summarizes oversized context.
    4. Parsing & Validation (suggestions.py): Parses structured responses from providers, validates YAML blocks, records warnings, and manages history.
    5. Orchestration (coordinator.py): Man/orchestrates refreshes and updates Home Assistant sensors.
  4. Configure User Control and Filtering

    main

    The integration provides (or is moving toward) granular control over the automation analysis process. Users can manage the scope of suggestions through:

    • Scope Filtering: Exclude specific domains, areas, devices, or entities from being analyzed.
    • Entity Status: Ability to ignore disabled or unavailable entities.
    • Language Selection: Separating UI translation from the AI output language (prompt/output language can be selected via Home Assistant language or an explicit option).
    • Customization: Using a persistent custom system prompt field to influence suggestion quality.
  5. Structured Output and Parsing Strategies

    main

    The integration uses structured output as a primary parsing strategy to ensure reliable automation suggestions.

    Supported Methods:

    • JSON/JSON Schema Mode: Supported by OpenAI, Gemini, Mistral, Perplexity, and OpenRouter. These use provider-specific parameter names (e.g., response_mime_type for Gemini).
    • Markdown Fenced YAML Fallback: Used as a fallback mechanism for older models or local deployments that do not support native structured output modes.

    When selecting a model, ensure it supports the required structured output format to avoid parsing errors.

  6. Important considerations for AI Automation Suggester

    main

    When using this integration, keep the following critical points in mind:

    • Privacy: Cloud-based AI providers receive entity data (names, states, attributes). For full data control, use local AI models like LocalAI or Ollama.
    • API Costs: Cloud providers charge based on tokens. Manage costs by using the entity_limit parameter and controlling the frequency of scheduled runs.
    • Accuracy: AI suggestions are based on patterns and logical inference; they are not guaranteed to be perfect. Always review suggestions thoroughly before implementing them in a live system.
    • Hallucinations: LLMs can provide illogical or incorrect suggestions. Use your judgment when reviewing.
    • Dependency Safety: This integration uses plain HTTP calls and does not require the Python openai package. Do not install or pin openai in your Home Assistant environment, as this can break the official OpenAI Conversation integration due to SDK version mismatches.
  7. Planned Filtering and Prompting Features

    main

    To improve the relevance and quality of automation suggestions, the following features are planned:

    Entity Filtering

    Users will be able to use include/exclude filters for:

    • Domains
    • Areas
    • Devices
    • Entities
    • Disabled entities
    • Hidden entities
    • Unavailable entities

    Prompt Customization

    • Persistent Custom System Prompt: A global option for the system prompt.
    • Per-service Extra Prompt: Ability to add specific context when calling a service.
  8. Understand the Target Provider Architecture

    main

    The project is moving toward a provider abstraction layer to handle the diversity of LLM endpoints (OpenAI, Anthropic, Google, Ollama, OpenRouter, etc.). Instead of a single dispatch dictionary, each provider will implement a shared contract to manage its own unique behaviors.

    Key components of the provider layer include:

    • ProviderConfig: Normalized settings derived from user configuration.
    • ProviderCapabilities: Flags indicating support for features like OpenAI-compatible chat, Responses API, streaming, custom headers, JSON/schema mode, and specific token parameter names.
    • ModelCapabilities: Metadata for specific models, including context window size, max output tokens, and structured-output support.
    • BaseProvider: An interface defining core methods: validate(), generate(), parse_response(), and redact_for_diagnostics().
  9. How AI Automation Suggester works

    main

    The integration follows a five-step lifecycle to generate automation ideas:

    1. Snapshot: On a manual trigger or schedule, the integration collects data about your Home Assistant entities (including attributes), devices, areas, and existing automations. You can use filters and limits to control the scope.
    2. Prompt Building: The snapshot is embedded into a system prompt. You can provide a custom prompt to steer the AI toward specific goals (e.g., "focus on presence lighting").
    3. Provider Call: The prompt is sent to your configured AI provider (e.g., OpenAI, Anthropic, Ollama, etc.).
    4. Parsing: The integration parses the AI response (preferring structured JSON or fenced YAML) and validates the resulting YAML.
    5. Surface: Suggestions are delivered via Home Assistant persistent notifications, sensor attributes, and stored history. These can be used in dashboard cards.
  10. Update the AI Automation Suggester integration

    main

    Depending on your installation method, follow these steps to update:

    • HACS Installation: Update directly through the HACS interface within Home Assistant.
    • Manual Installation:
      1. Download the latest version of the files from the repository.
      2. Replace the existing files in your custom_components/ai_automation_suggester folder.
      3. Restart Home Assistant.
  11. Install AI Automation Suggester manually

    main

    To install without HACS:

    1. Download the repository contents.
    2. Copy the custom_components/ai_automation_suggester folder into your Home Assistant custom_components directory.
      <homeassistant_config_dir>/
      └── custom_components/
          └── ai_automation_suggester/
              ├── __init__.py
              └── ...
    3. Restart Home Assistant.
    4. Go to SettingsDevices & Services+ Add Integration and search for AI Automation Suggester.
  12. Configure the AI Automation Suggester integration

    main

    After installation, add the integration via the Home Assistant UI (SettingsDevices & Services+ Add IntegrationAI Automation Suggester).

    The setup wizard requires:

    • AI Provider: Select from the dropdown (e.g., OpenAI, Anthropic, Google, Ollama, etc.).
    • API Keys or Endpoint: Provide credentials or the local server URL.
    • Model: Choose the specific model variant.
    • Max Tokens: Define the maximum length of the AI's response.
    • Custom System Prompt (Optional): Provide a prompt to guide the AI's perspective (e.g., "You are an expert in energy-saving automations.").

    Settings can be adjusted later via the integration options in the Devices & Services menu.