Home Assistant MCP Server

Home Assistant MCP Server

Enables AI assistants to interact with Home Assistant smart home devices through natural language. Control devices, manage automations, query entity states, and retrieve historical data across your home automation system.

Category
访问服务器

README

Home Assistant MCP Server

Python MCP License

A Model Context Protocol (MCP) server that enables AI assistants like Claude to interact with Home Assistant. Control smart home devices, manage automations, query entity states, and more through a standardized AI-to-home-automation interface.

Features

Core Capabilities

  • Entity State Management: Query current state and attributes of any Home Assistant entity
  • Entity Discovery: List and filter entities by domain (lights, sensors, switches, etc.)
  • Service Calls: Execute any Home Assistant service to control devices
  • Historical Data: Retrieve entity state history over configurable time periods
  • Automation Triggers: Manually trigger automations

Automation Management

  • Create Automations: Build new automations from YAML configuration
  • Update Automations: Modify existing automation configurations
  • Delete Automations: Remove automations programmatically
  • View Configurations: Retrieve full YAML config for any automation
  • List All Automations: Get complete inventory of automation configs
  • Reload Automations: Refresh automation configuration after changes
  • Enable/Disable: Toggle automations on or off

Available Tools (13)

Tool Description
get_state Get current state and attributes of any entity
list_entities List entities with optional domain filtering
call_service Execute any Home Assistant service
trigger_automation Manually trigger an automation
get_history Retrieve historical state changes
create_automation Create new automation from config
update_automation Modify existing automation
delete_automation Remove an automation
get_automation_config View full automation YAML
list_automation_configs List all automation configurations
reload_automations Reload automation configuration
enable_automation Enable a disabled automation
disable_automation Disable an active automation

Installation

Prerequisites

  • Python 3.10 or higher
  • Home Assistant instance (local or remote)
  • Home Assistant Long-Lived Access Token

Setup Steps

  1. Clone the repository

    git clone https://github.com/mjrestivo16/mcp-homeassistant.git
    cd mcp-homeassistant
    
  2. Create virtual environment

    python -m venv venv
    
    # On Windows
    venv\Scripts\activate
    
    # On Linux/Mac
    source venv/bin/activate
    
  3. Install dependencies

    pip install -r requirements.txt
    
  4. Configure environment

    Create a .env file in the project root:

    HA_URL=http://192.168.1.100:8123
    HA_TOKEN=your_long_lived_access_token_here
    

    To generate a Long-Lived Access Token:

    1. Log into Home Assistant
    2. Click your profile (bottom left)
    3. Scroll to "Long-Lived Access Tokens"
    4. Click "Create Token"
    5. Give it a name (e.g., "MCP Server")
    6. Copy the token to your .env file
  5. Test the server

    python server.py
    

Configuration for Claude Desktop

Add this configuration to your Claude Desktop config file:

Windows: %APPDATA%\Claude\claude_desktop_config.json macOS: ~/Library/Application Support/Claude/claude_desktop_config.json Linux: ~/.config/Claude/claude_desktop_config.json

{
  "mcpServers": {
    "homeassistant": {
      "type": "stdio",
      "command": "python",
      "args": ["C:/path/to/mcp-homeassistant/server.py"],
      "env": {
        "HA_URL": "http://192.168.1.100:8123",
        "HA_TOKEN": "your_long_lived_access_token_here"
      }
    }
  }
}

Note: Use absolute paths in the configuration. Restart Claude Desktop after adding the configuration.

Usage Examples

Query Entity State

# Ask Claude:
"What's the current state of my living room light?"

# Claude uses: get_state("light.living_room")

Control Devices

# Ask Claude:
"Turn on the bedroom light at 50% brightness"

# Claude uses: call_service(
#   domain="light",
#   service="turn_on",
#   entity_id="light.bedroom",
#   data={"brightness_pct": 50}
# )

Create Automation

# Ask Claude:
"Create an automation that turns on the porch light at sunset"

# Claude uses: create_automation({
#   "id": "porch_light_sunset",
#   "alias": "Porch Light at Sunset",
#   "trigger": {
#     "platform": "sun",
#     "event": "sunset"
#   },
#   "action": {
#     "service": "light.turn_on",
#     "target": {"entity_id": "light.porch"}
#   }
# })

List Entities by Domain

# Ask Claude:
"Show me all my temperature sensors"

# Claude uses: list_entities(domain="sensor")
# Then filters results for temperature entities

View Automation History

# Ask Claude:
"Show me the history of my thermostat for the last 12 hours"

# Claude uses: get_history(
#   entity_id="climate.living_room",
#   hours=12
# )

API Reference

get_state

Get the current state and attributes of any Home Assistant entity.

Parameters:

  • entity_id (string, required): Entity ID (e.g., light.office, sensor.temperature)

Returns: Formatted text with entity state and all attributes

Example:

{
  "entity_id": "light.living_room"
}

list_entities

List all entities, optionally filtered by domain.

Parameters:

  • domain (string, optional): Domain filter (e.g., light, sensor, automation)

Returns: List of entities with their current states (limited to first 50)

Example:

{
  "domain": "light"
}

call_service

Call any Home Assistant service to control devices.

Parameters:

  • domain (string, required): Service domain (e.g., light, climate, switch)
  • service (string, required): Service name (e.g., turn_on, turn_off, set_temperature)
  • entity_id (string, required): Target entity ID
  • data (object, optional): Additional service data (e.g., brightness, temperature)

Returns: Success confirmation message

Example:

{
  "domain": "light",
  "service": "turn_on",
  "entity_id": "light.bedroom",
  "data": {
    "brightness_pct": 75,
    "color_temp": 370
  }
}

trigger_automation

Manually trigger a Home Assistant automation.

Parameters:

  • entity_id (string, required): Automation entity ID (e.g., automation.morning_routine)

Returns: Success confirmation message

Example:

{
  "entity_id": "automation.morning_routine"
}

get_history

Get historical state changes for an entity.

Parameters:

  • entity_id (string, required): Entity ID to get history for
  • hours (number, optional): Number of hours of history (default: 24)

Returns: Last 10 state changes within the time period

Example:

{
  "entity_id": "sensor.outdoor_temperature",
  "hours": 12
}

create_automation

Create a new Home Assistant automation from YAML configuration.

Parameters:

  • automation_config (object, required): Complete automation configuration including:
    • id (string, required): Unique automation ID
    • alias (string, required): Human-readable name
    • trigger (object/array, required): Trigger configuration
    • action (object/array, required): Action configuration
    • condition (object/array, optional): Condition configuration
    • mode (string, optional): Automation mode (single, restart, queued, parallel)

Returns: Success confirmation with automation ID

Example:

{
  "automation_config": {
    "id": "motion_light_kitchen",
    "alias": "Kitchen Motion Light",
    "trigger": {
      "platform": "state",
      "entity_id": "binary_sensor.kitchen_motion",
      "to": "on"
    },
    "action": {
      "service": "light.turn_on",
      "target": {"entity_id": "light.kitchen"}
    }
  }
}

update_automation

Update an existing Home Assistant automation.

Parameters:

  • automation_id (string, required): The automation ID (not entity_id)
  • automation_config (object, required): Updated automation configuration

Returns: Success confirmation with automation ID

Example:

{
  "automation_id": "motion_light_kitchen",
  "automation_config": {
    "id": "motion_light_kitchen",
    "alias": "Kitchen Motion Light (Updated)",
    "trigger": {
      "platform": "state",
      "entity_id": "binary_sensor.kitchen_motion",
      "to": "on"
    },
    "action": [
      {
        "service": "light.turn_on",
        "target": {"entity_id": "light.kitchen"},
        "data": {"brightness_pct": 100}
      }
    ]
  }
}

delete_automation

Delete a Home Assistant automation.

Parameters:

  • automation_id (string, required): The automation ID to delete (not entity_id)

Returns: Success confirmation

Example:

{
  "automation_id": "old_automation_id"
}

get_automation_config

Get the full YAML configuration of an automation.

Parameters:

  • automation_id (string, required): The automation ID (not entity_id)

Returns: Full automation configuration as JSON

Example:

{
  "automation_id": "motion_light_kitchen"
}

list_automation_configs

List all automation configurations (full YAML configs, not just states).

Parameters: None

Returns: List of all automations with their IDs and aliases


reload_automations

Reload all automations after making changes.

Parameters: None

Returns: Success confirmation


enable_automation

Enable a disabled automation.

Parameters:

  • entity_id (string, required): Automation entity ID (e.g., automation.morning_routine)

Returns: Success confirmation

Example:

{
  "entity_id": "automation.morning_routine"
}

disable_automation

Disable an active automation.

Parameters:

  • entity_id (string, required): Automation entity ID (e.g., automation.morning_routine)

Returns: Success confirmation

Example:

{
  "entity_id": "automation.morning_routine"
}

Architecture

Technology Stack

  • Python 3.10+: Core runtime
  • MCP SDK 1.21.2: Model Context Protocol implementation
  • httpx: Async HTTP client for Home Assistant API
  • python-dotenv: Environment configuration management

Communication Flow

Claude Desktop → MCP Server (stdio) → Home Assistant API (REST)
  1. Claude Desktop sends tool calls via stdio
  2. MCP Server processes requests and authenticates with HA token
  3. Home Assistant API executes commands and returns results
  4. MCP Server formats responses for Claude

Error Handling

  • HTTP status errors from Home Assistant API
  • Request timeouts (30 second default)
  • Authentication failures
  • Malformed automation configurations

Troubleshooting

Server won't start

  • Verify Python version: python --version (must be 3.10+)
  • Check virtual environment is activated
  • Ensure all dependencies installed: pip install -r requirements.txt

Authentication errors

  • Verify Home Assistant URL is correct and accessible
  • Test token with curl:
    curl -H "Authorization: Bearer YOUR_TOKEN" http://YOUR_HA_URL/api/
    
  • Regenerate token if expired

Tools not appearing in Claude

  • Restart Claude Desktop after config changes
  • Check Claude Desktop logs (Help → View Logs)
  • Verify absolute paths in configuration
  • Ensure no JSON syntax errors in config file

Automation changes not taking effect

  • Use reload_automations tool after creating/updating automations
  • Check Home Assistant logs for YAML syntax errors
  • Verify automation IDs are unique

Security Considerations

  • Never commit .env files to version control
  • Store Home Assistant tokens securely
  • Use network isolation for production deployments
  • Consider enabling Home Assistant authentication logs
  • Regularly rotate access tokens

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

git clone https://github.com/mjrestivo16/mcp-homeassistant.git
cd mcp-homeassistant
python -m venv venv
source venv/bin/activate  # or venv\Scripts\activate on Windows
pip install -r requirements.txt

License

MIT License - see LICENSE file for details

Acknowledgments

Support


Made with by the Home Assistant community

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选