Making Your Agent Constantly Aware of Your Home Automation System

Introduction

In this tutorial, we will guide you through the process of creating a Hook in BrainSoup to update a Custom Context with the state of your home automation lights. By leveraging this powerful combination of Hooks and Custom Contexts, you can ensure your agent is constantly aware of the current state of your home automation system, making it more responsive and useful as a personal assistant.

Notes:

  • This tutorial assumes you are using Home Assistant as your home automation system. If you are using a different system, you will need to adapt the script to work with your specific setup.
  • For simplicity, we will focus on updating the light states, but you can extend this concept to other aspects of your home automation system, such as thermostats, shutters, home alarm, energy management, sensors, switches, etc.

Step 1: Choose the Trigger Event

We will use the periodic trigger Timer.EveryMinute to fetch the light states every minute. This ensures that the context is updated frequently enough to provide accurate information about your home automation system.

Step 2: Write the Hook Script

The script, GetLightStates.ps1 , will be placed in the Hooks\OnEvent\Timer.EveryMinute directory. This PowerShell script calls the Home Assistant API to retrieve the current states of all lights and saves it as a JSON file named Lights.json in the Context directory.

Here is the script:

# Settings
# See https://www.home-assistant.io/docs/authentication/ for how to create a Long-Lived Access Token
$homeAssistantUrl = "http://192.168.0.1:8123"   # URL of your Home Assistant instance
$apiToken = "YOUR_API_TOKEN"                    # Long-Lived Access Token for Home Assistant

# Authorization headers
$headers = @{
    "Authorization" = "Bearer $apiToken"
    "Content-Type"  = "application/json"
}

# API endpoint
$endpoint = "$homeAssistantUrl/api/states"

# Call the API and get the response
$response = Invoke-RestMethod -Uri $endpoint -Method Get -Headers $headers

# Filter the response to keep only the lights
$lights = $response | Where-Object { $_.entity_id -like "light.*" } | Sort-Object -Property { $_.attributes.friendly_name }

# Create a hashtable to store the state of each light
$lightStates = [ordered]@{}

# Add the state of each light to the hashtable
foreach ($light in $lights) {
    $friendlyName = $light.attributes.friendly_name
    $state = $light.state
    $lightStates[$friendlyName] = $state
}

# Convert the hashtable to JSON
$jsonResult = $lightStates | ConvertTo-Json -Compress

# Path to the context file: "%AppData%\BrainSoup\Context\Lights.json"
$outputFilePath = [System.IO.Path]::Combine($env:APPDATA, "BrainSoup\Context\Lights.json")

# Save the JSON response to a file
$jsonResult | Out-File -FilePath $outputFilePath -Encoding utf8

# Write a message to the console to confirm that the data has been saved
Write-Output "Light states saved to '$outputFilePath'"

Step 3: Place Script in Appropriate Directory

Save this script as GetLightStates.ps1 and place it in %AppData%\BrainSoup\Hooks\OnEvent\Timer.EveryMinute\.

Step 4: Subscribe Agents to the Custom Context

To make an agent aware of this new context:

  1. Open BrainSoup.
  2. Go to Participants > Agents.
  3. Select your agent and click Edit.
  4. Navigate to Context subscriptions.
  5. Add Lights as a subscribed context.
  6. Click OK.

Conclusion

By following these steps, you can set up a Hook in BrainSoup that updates a Custom Context with your home automation light states every minute. This ensures your agents are always informed about current conditions and can respond accordingly.