opencode-mqtt-home-assistant
v1.0.1
Published
OpenCode plugin for Home Assistant MQTT integration - publishes session status when AI agent finishes
Maintainers
Readme
opencode-mqtt-home-assistant
An OpenCode plugin that publishes MQTT messages to Home Assistant when your AI coding sessions become idle (agent finishes responding). Perfect for creating automations based on your OpenCode activity.
Features
- 📡 MQTT Publishing: Sends session status to your MQTT broker
- 🏠 Home Assistant Integration: Auto-discovery creates a sensor entity automatically
- 🔄 Automatic Reconnection: Handles broker disconnections gracefully
- ⚙️ Configurable: All settings via environment variables
- 📝 Structured Logging: Uses OpenCode's logging system
Installation
From npm (Recommended)
Add to your opencode.json:
{
"plugin": ["opencode-mqtt-home-assistant"]
}OpenCode will automatically install the package at startup.
From Local Files
Create the plugin directory:
mkdir -p .opencode/pluginsCreate a plugin file (e.g.,
.opencode/plugins/mqtt.ts):export { OpenCodeMqttPlugin as default } from "opencode-mqtt-home-assistant";Add a
package.jsonin.opencode/:{ "dependencies": { "opencode-mqtt-home-assistant": "^1.0.0" } }
Configuration
Configure via environment variables:
| Variable | Description | Required | Default |
|----------|-------------|----------|---------|
| OPENCODE_MQTT_HOST | MQTT broker hostname/IP | Yes | - |
| OPENCODE_MQTT_PORT | MQTT broker port | No | 1883 |
| OPENCODE_MQTT_USERNAME | MQTT username | No | - |
| OPENCODE_MQTT_PASSWORD | MQTT password | No | - |
| OPENCODE_MQTT_TOPIC | Topic for session status | No | home/opencode/session |
| OPENCODE_MQTT_QOS | Quality of Service (0, 1, 2) | No | 1 |
| OPENCODE_MQTT_VERBOSE | Enable verbose logging | No | false |
| OPENCODE_MQTT_MAX_RECONNECT | Max reconnection attempts | No | 5 |
| OPENCODE_MQTT_RECONNECT_DELAY | Reconnect delay (ms) | No | 5000 |
Example Configuration
Linux/macOS (in your .bashrc, .zshrc, or .env):
export OPENCODE_MQTT_HOST="192.168.1.200"
export OPENCODE_MQTT_USERNAME="homeassistant"
export OPENCODE_MQTT_PASSWORD="your_password"
export OPENCODE_MQTT_TOPIC="home/opencode/session"Windows (PowerShell - temporary, current session only):
$env:OPENCODE_MQTT_HOST = "192.168.1.200"
$env:OPENCODE_MQTT_USERNAME = "homeassistant"
$env:OPENCODE_MQTT_PASSWORD = "your_password"
$env:OPENCODE_MQTT_TOPIC = "home/opencode/session"Windows (Command Prompt - temporary, current session only):
set OPENCODE_MQTT_HOST=192.168.1.200
set OPENCODE_MQTT_USERNAME=homeassistant
set OPENCODE_MQTT_PASSWORD=your_password
set OPENCODE_MQTT_TOPIC=home/opencode/sessionWindows (permanent - via System Settings):
- Press
Win + R, typesysdm.cpland press Enter - Go to the Advanced tab → click Environment Variables
- Under User variables, click New
- Add each variable:
- Variable name:
OPENCODE_MQTT_HOST - Variable value:
192.168.1.200
- Variable name:
- Repeat for all required variables
- Click OK to save
- Restart your terminal for changes to take effect
Windows (permanent - via PowerShell, run as Administrator):
[Environment]::SetEnvironmentVariable("OPENCODE_MQTT_HOST", "192.168.1.200", "User")
[Environment]::SetEnvironmentVariable("OPENCODE_MQTT_USERNAME", "homeassistant", "User")
[Environment]::SetEnvironmentVariable("OPENCODE_MQTT_PASSWORD", "your_password", "User")
[Environment]::SetEnvironmentVariable("OPENCODE_MQTT_TOPIC", "home/opencode/session", "User")Note: After setting permanent environment variables on Windows, you need to restart your terminal or IDE for the changes to take effect.
Home Assistant Setup
Automatic Discovery
The plugin automatically publishes a Home Assistant MQTT discovery message, creating a sensor entity:
- Entity ID:
sensor.opencode_session - States:
idle(agent finished) oractive(session started)
No manual configuration needed in Home Assistant!
Manual Configuration (Alternative)
If you prefer manual setup, add this to your configuration.yaml:
sensor:
- platform: mqtt
name: "OpenCode Session"
state_topic: "home/opencode/session"
icon: mdi:robotAutomation Examples
Notify When Session Complete
automation:
- alias: "OpenCode Session Complete"
trigger:
- platform: state
entity_id: sensor.opencode_session
to: "idle"
action:
- service: notify.mobile_app_your_phone
data:
message: "OpenCode has finished processing"
title: "AI Session Complete"Turn On Light When Working
automation:
- alias: "OpenCode Working Light"
trigger:
- platform: state
entity_id: sensor.opencode_session
to: "active"
action:
- service: light.turn_on
target:
entity_id: light.office
data:
color_name: blue
brightness_pct: 100Track Daily OpenCode Usage
sensor:
- platform: history_stats
name: "OpenCode Sessions Today"
entity_id: sensor.opencode_session
state: "idle"
type: count
start: "{{ now().replace(hour=0, minute=0, second=0) }}"
end: "{{ now() }}"How It Works
- Plugin loads when OpenCode starts
- Connects to your MQTT broker
- Publishes discovery message for Home Assistant
- Listens for OpenCode session events:
session.created→ publishes"active"session.idle→ publishes"idle"
- Reconnects automatically if the broker disconnects
Troubleshooting
Sensor not appearing in Home Assistant
- Verify MQTT is working in Home Assistant: Settings → Devices & Services → MQTT → Configure → "Listen to a topic"
- Listen to
homeassistant/sensor/#to see discovery messages - Check OpenCode logs for connection errors
Connection refused
- Verify the broker IP and port
- Check if MQTT broker is running
- Verify firewall allows connections on port 1883
Authentication failed
- Set
OPENCODE_MQTT_USERNAMEandOPENCODE_MQTT_PASSWORD - Check the user has publish permissions on the broker
Enable verbose logging
export OPENCODE_MQTT_VERBOSE=trueThis will log all MQTT operations to help diagnose issues.
Development
# Install dependencies
npm install
# Build
npm run build
# Type check
npm run typecheck
# Run tests
npm testLicense
MIT
