This is an automated archive made by the Lemmit Bot.
The original was posted on /r/homeassistant by /u/Jay_Skye on 2025-04-04 23:14:43+00:00.
[UPDATED CODE]
Below is the full tutorial to set up a back-and-forth chat with your LLM via actionable notifications, using the Ollama integration . First, you’ll create a script to start the conversation, then an automation to handle replies and showing how to include a conversation ID to maintain context.
Each time you run the “Start LLM Chat” script, a new conversation ID is created—resetting the context. This conversation ID is stored in the input_text helper and used by both the start script and the reply-handling automation.
Step 0: Create an Input Text Helper for the Conversation ID
- Navigate to Helpers: Go to Settings > Devices & Services > Helpers.
- Add a New Helper: Click + Add Helper and choose Text.
- Configure the Helper: • Name: Conversation ID • Entity ID: (It will be created as input_text.conversation_id)
- Save the Helper.
Step 1: Create the Script for Starting the Chat (with Dynamic Conversation ID)
This script does the following: • Dynamically generates a unique conversation ID using the current timestamp. • Stores the conversation ID in the input_text helper. • Starts the chat by calling the LLM service using that conversation ID. • Sends an actionable notification to your iPhone.
Path: Settings > Automations & Scenes > Scripts > + Add Script
Copy and paste the following YAML into the script editor:
alias: “Start LLM Chat” mode: single sequence:
Step 1: Generate and store a unique conversation ID.
- service: inputtext.set_value data: entity_id: input_text.conversation_id value: "conversation{ as_timestamp(now()) }"
Step 2: Start the conversation using the generated conversation ID.
- service: conversation.process data: agent_id: conversation.llama3_2_1b text: > Let’s start a chat! What’s on your mind today? Use emojis if you’d like! conversation_id: “{{ states(‘input_text.conversation_id’) }}” response_variable: ai_response
Step 3: Send a notification to your iPhone with a reply action.
- service: notify.mobile_app_iphone_16_pro data: message: “{{ ai_response.response.speech.plain.speech }}” data: actions:
- action: “REPLY_CHAT_1” title: “Reply” behavior: textInput textInputButtonTitle: “Send” textInputPlaceholder: “Type your reply here…”
• Save the Script with a name like “Start LLM Chat.”
Step 2: Create the Automation for Handling Replies (Using the Stored Conversation ID)
This automation triggers when you reply to the notification. It: • Retrieves the conversation ID from the input_text helper. • Sends your reply to the LLM using the same conversation ID (maintaining context). • Sends a new notification with the LLM’s reply.
Path: Settings > Automations & Scenes > Automations > + Add Automation > Start with an empty automation > Edit in YAML
Copy and paste the following YAML into the automation editor:
alias: “Handle LLM Chat Reply” mode: single trigger:
- platform: event event_type: mobile_app_notification_action event_data: action: “REPLY_CHAT_1” action:
- service: conversation.process data: agent_id: conversation.llama3_2_1b text: “{{ trigger.event.data.reply_text }}” conversation_id: “{{ states(‘input_text.conversation_id’) }}” response_variable: ai_reply
- service: notify.mobile_app_iphone_16_pro data: message: “{{ ai_reply.response.speech.plain.speech }}” data: actions:
- action: “REPLY_CHAT_1” title: “Reply” behavior: textInput textInputButtonTitle: “Send” textInputPlaceholder: “Type your reply here…”
• Save the Automation with a name like “Handle LLM Chat Reply.”
Step 3: Testing the Flow
- Trigger the Script: • Go to Settings > Automations & Scenes > Scripts. • Find “Start LLM Chat” and click Run. • A unique conversation ID is generated and stored (e.g., conversation_1678901234).
- Reply to the Notification: • A notification appears on your iPhone with a reply action. • Tap Reply, type your message, and hit Send.
- Observe the Conversation: • The automation picks up your reply, sends it to the LLM using the stored conversation ID, and sends the response back to your iPhone. • As long as you don’t run the start script again, the context is maintained via the same conversation ID.
- Resetting the Context: • Running the “Start LLM Chat” script again generates a new conversation ID, starting a fresh conversation context.
Path Summary • Create the Input Text Helper: • Settings > Devices & Services > Helpers > + Add Helper (choose Text) • Create the Script: • Settings > Automations & Scenes > Scripts > + Add Script • Create the Automation: • Settings > Automations & Scenes > Automations > + Add Automation > Start with an empty automation > Edit in YAML
This dynamic setup ensures that every time you start a new chat, a unique conversation ID is generated and stored, resetting the conversation context. Subsequent replies use this ID to maintain continuity.