From ed8f19bfa9b399ce995637023529132bd8b82d30 Mon Sep 17 00:00:00 2001 From: SMKRV Date: Tue, 2 Sep 2025 01:19:44 +0300 Subject: [PATCH] fix: Add support for response_variable in ask_question service - Added response schema definition in services.yaml for ask_question service - Set supports_response=True flag when registering the service - Fixed JSON syntax error in English translation file - Added comprehensive documentation with examples for response_variable usage - Users can now capture AI responses directly in variables without sensor delays Resolves issue where scripts failed with 'Script does not support response_variable' error --- RESPONSE_VARIABLE_EXAMPLE.md | 148 ++++++++++++++++++ custom_components/ha_text_ai/__init__.py | 3 +- custom_components/ha_text_ai/services.yaml | 33 ++++ .../ha_text_ai/translations/en.json | 4 +- 4 files changed, 185 insertions(+), 3 deletions(-) create mode 100644 RESPONSE_VARIABLE_EXAMPLE.md diff --git a/RESPONSE_VARIABLE_EXAMPLE.md b/RESPONSE_VARIABLE_EXAMPLE.md new file mode 100644 index 0000000..106fd35 --- /dev/null +++ b/RESPONSE_VARIABLE_EXAMPLE.md @@ -0,0 +1,148 @@ +# Using response_variable with HA Text AI + +After updating the HA Text AI integration, it now supports using the `response_variable` parameter in Home Assistant scripts and automations. + +## What Changed + +- Added response schema support in the `ha_text_ai.ask_question` service +- Service is now correctly registered with `supports_response=True` flag +- You can now use `response_variable` to capture AI response in a variable + +## Example Usage in Script + +```yaml +action: ha_text_ai.ask_question +data: + context_messages: 0 + temperature: 0.7 + max_tokens: 1000 + instance: sensor.ha_text_ai_gemini + question: "What time is it?" +response_variable: ai_response +``` + +## Example Usage in Automation + +```yaml +alias: "Get AI Response" +trigger: + - platform: state + entity_id: input_boolean.ask_ai + to: "on" +action: + - action: ha_text_ai.ask_question + data: + instance: sensor.ha_text_ai_gemini + question: "What's the current weather?" + temperature: 0.7 + max_tokens: 500 + response_variable: weather_response + + - action: notify.persistent_notification + data: + title: "AI Response" + message: "{{ weather_response.response_text }}" +``` + +## Available Fields in response_variable + +When you use `response_variable`, you will receive an object with the following fields: + +- `response_text` (string) - The AI response text +- `tokens_used` (integer) - Total number of tokens used +- `prompt_tokens` (integer) - Number of tokens in the prompt +- `completion_tokens` (integer) - Number of tokens in the completion +- `model_used` (string) - The AI model that was used for the response +- `instance` (string) - The instance name that was used +- `question` (string) - The original question that was asked +- `timestamp` (string) - ISO timestamp when the response was generated +- `success` (boolean) - Whether the request was successful +- `error` (string) - Error message if the request failed + +## Example Using Response Fields + +```yaml +action: + - action: ha_text_ai.ask_question + data: + instance: sensor.ha_text_ai_gemini + question: "Tell me a joke" + response_variable: joke_response + + - condition: template + value_template: "{{ joke_response.success }}" + + - action: input_text.set_value + target: + entity_id: input_text.last_ai_response + data: + value: "{{ joke_response.response_text }}" + + - action: input_number.set_value + target: + entity_id: input_number.tokens_used + data: + value: "{{ joke_response.tokens_used }}" +``` + +## Error Handling + +```yaml +action: + - action: ha_text_ai.ask_question + data: + instance: sensor.ha_text_ai_gemini + question: "Test question" + response_variable: ai_result + + - choose: + - conditions: + - condition: template + value_template: "{{ ai_result.success }}" + sequence: + - action: notify.mobile_app_phone + data: + title: "AI Response" + message: "{{ ai_result.response_text }}" + - conditions: + - condition: template + value_template: "{{ not ai_result.success }}" + sequence: + - action: notify.mobile_app_phone + data: + title: "AI Error" + message: "Error: {{ ai_result.error }}" +``` + +## Migration from Old Approach + +**Old method (without response_variable):** +```yaml +# Ask question +- action: ha_text_ai.ask_question + data: + instance: sensor.ha_text_ai_gemini + question: "Hello!" + +# Wait and read response from sensor +- delay: 00:00:05 +- action: notify.mobile_app_phone + data: + message: "{{ states('sensor.ha_text_ai_gemini') }}" +``` + +**New method (with response_variable):** +```yaml +# Ask question and get response immediately +- action: ha_text_ai.ask_question + data: + instance: sensor.ha_text_ai_gemini + question: "Hello!" + response_variable: greeting_response + +- action: notify.mobile_app_phone + data: + message: "{{ greeting_response.response_text }}" +``` + +The new approach is more reliable as it doesn't require waiting and reading from the sensor. diff --git a/custom_components/ha_text_ai/__init__.py b/custom_components/ha_text_ai/__init__.py index a065d38..194bde2 100644 --- a/custom_components/ha_text_ai/__init__.py +++ b/custom_components/ha_text_ai/__init__.py @@ -188,7 +188,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: DOMAIN, SERVICE_ASK_QUESTION, async_ask_question, - schema=SERVICE_SCHEMA_ASK_QUESTION + schema=SERVICE_SCHEMA_ASK_QUESTION, + supports_response=True ) hass.services.async_register( diff --git a/custom_components/ha_text_ai/services.yaml b/custom_components/ha_text_ai/services.yaml index 9bcd342..625047c 100644 --- a/custom_components/ha_text_ai/services.yaml +++ b/custom_components/ha_text_ai/services.yaml @@ -73,6 +73,39 @@ ask_question: max: 100000 step: 1 mode: box + response: + type: object + properties: + response_text: + type: string + description: The AI response text + tokens_used: + type: integer + description: Total number of tokens used + prompt_tokens: + type: integer + description: Number of tokens in the prompt + completion_tokens: + type: integer + description: Number of tokens in the completion + model_used: + type: string + description: The AI model that was used for the response + instance: + type: string + description: The instance name that was used + question: + type: string + description: The original question that was asked + timestamp: + type: string + description: ISO timestamp when the response was generated + success: + type: boolean + description: Whether the request was successful + error: + type: string + description: Error message if the request failed clear_history: name: Clear History diff --git a/custom_components/ha_text_ai/translations/en.json b/custom_components/ha_text_ai/translations/en.json index 61200ac..3f6eb77 100644 --- a/custom_components/ha_text_ai/translations/en.json +++ b/custom_components/ha_text_ai/translations/en.json @@ -92,9 +92,9 @@ "anthropic": "Anthropic (compatible)", "deepseek": "DeepSeek", "gemini": "Google Gemini" + } } - } -}, + }, "services": { "ask_question": { "name": "Ask Question (HA Text AI)",