Adding Conversation History to AI Assistants in Home Assistant

Adding Conversation History to AI Assistants in Home Assistant

If you’ve integrated OpenAI or other AI assistants into Home Assistant, you may have noticed a frustrating limitation—each message you send is treated in isolation. Without memory, the assistant won’t remember past interactions, making conversations feel disjointed and unnatural.

Ideally, an AI assistant should retain context so you don’t have to repeat yourself in every message. This guide walks you through creating a conversation history log in Home Assistant. The log allows the AI assistant to “read” previous messages before responding, creating a more natural conversational experience.

Here’s an example of an interaction that retains memory of past messages:


How It Works

We’ll store user and assistant interactions in a text file. Each time you send a message to the conversation agent, Home Assistant will:

  1. Read the stored history and include it in the new request.
  2. Append the latest user message and AI response to the file.
  3. Trim the history file to prevent excessive size (keeping costs down).

With this setup, the AI assistant will be able to remember context from past interactions while keeping the system efficient.

Requirements

Although you’re more than welcome to just read this post for general knowledge, check out my recommendations for what you need to have, in order to be able to fully use the provided information.


Step 1: Create the History Folder

Using your file explorer, create a folder in your Home Assistant configuration directory: /config/ai_history

Step 2: Allow Home Assistant to Access the Folder

Add the following lines to your configuration.yaml file to grant Home Assistant access:

homeassistant:
  allowlist_external_dirs:
    - "/config/ai_history"

This step ensures Home Assistant can read and write to the folder.

Step 3: Install the File Integration

  1. Go to Settings → Devices & Services in Home Assistant.
  2. Click Add Integration and search for File.
  3. Choose the option to create a Notification Service.
  4. Set the file path to: /config/ai_history/ai_history.txt
  5. Enable timestamps (optional, but useful for tracking conversation timing).

This integration allows Home Assistant to write messages to the history log.

Step 4: Add Shell Commands for Managing the Log

Next, we need a way to read the log and trim it periodically. Add the following to configuration.yaml:

shell_command:
    get_ai_history: 'cat /config/ai_history/ai_history.txt'
    trim_ai_history: 'tail -n 50 /config/ai_history/ai_history.txt > /config/ai_history/ai_history.tmp && mv /config/ai_history/ai_history.tmp /config/ai_history/ai_history.txt'

get_ai_history retrieves the full conversation history.
trim_ai_history keeps only the last 50 lines. (You can change this number).

Step 5: Create a Script to Read the History

Now, we’ll create a script to fetch the conversation history. Add the following script in Home Assistant:

sequence:
  - action: shell_command.get_ai_history
    metadata: {}
    data: {}
    response_variable: ai_history
    alias: Read History
  - variables:
      result:
        txt: "{{ai_history.stdout}}"
  - stop: ""
    response_variable: result
alias: Get AI History
description: ""

To use this script in automations, call it with:

action: script.get_ai_history
metadata: {}
data: {}
response_variable: AIHistory

Step 6: Include History When Calling the AI Assistant

Now that we have a working history system, we need to send this history to the AI whenever we make a request.

Here’s an automation example that includes history in AI queries:

alias: Conversation Agent Notification Text
data:
  text: >-
    {{ AIHistory.txt + '*** Most recent message from the user which you need to
    respond to is: [USER / ' + username +']: ' +
    newmessage }}
  agent_id: 01JG2DffJfdsDMHRNRNC1Q7K1F
  conversation_id: "1"
response_variable: agent
action: conversation.process

  • AIHistory.txt contains past conversation history.
  • username is the user’s name (replace as needed with yours).
  • newmessage is the latest user message (replace with yours).

This ensures the AI assistant understands context from previous interactions.

Step 7: Save Messages to the History Log

Each user message and AI response must be appended to the log for the next conversation interaction. Add these automation steps:

Save the user's message:

action: notify.send_message
metadata: {}
data:
  message: >-
    {{'[USER / ' + username +']: ' +
    newmessage}}
target:
  entity_id: notify.file

Save the assistant’s response:

action: notify.send_message
metadata: {}
data:
  message: "{{'[ASSISTANT]: ' + agent.response.speech.plain.speech }}"
target:
  entity_id: notify.file

Step 8: Trim the History File

To prevent excessive file size, call the trimming service periodically:

action: shell_command.trim_ai_history
metadata: {}
data: {}

This will keep only the most recent 50 messages in the log.

Conclusion

By implementing this conversation history system, you can transform your AI assistant into a more context-aware chatbot. This method works with OpenAI, Extended OpenAI, or any other AI integration in Home Assistant.

  • No more repeating context in every message.
  • AI understands ongoing conversations more naturally.
  • History file stays trimmed to avoid unnecessary costs.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply