Skip to main content
Glama
hpohlmann

Home Assistant MCP

by hpohlmann

search_entities

Find Home Assistant devices by describing them in plain language. Enter phrases like 'office light' or 'kitchen fan' to locate matching entities for control.

Instructions

Search for Home Assistant entities matching a natural language description.

Args:
    description: Natural language description of the entity (e.g., "office light", "kitchen fan")

Returns:
    A list of matching entity IDs with their friendly names, or an error message

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
descriptionYes

Implementation Reference

  • The main execution logic for the 'search_entities' MCP tool. Fetches entities from Home Assistant, matches using keyword helper, formats and returns results.
    async def search_entities(description: str) -> str:
        """Search for Home Assistant entities matching a natural language description.
        
        Args:
            description: Natural language description of the entity (e.g., "office light", "kitchen fan")
        
        Returns:
            A list of matching entity IDs with their friendly names, or an error message
        """
        # Check token
        if not HOME_ASSISTANT_TOKEN:
            return "Home Assistant token not configured. Set HOME_ASSISTANT_TOKEN environment variable."
        
        # Get all entities
        entities = await get_all_entities()
        if not entities:
            return "Failed to retrieve entities from Home Assistant."
        
        # Search and format results
        matches = search_entities_by_keywords(entities, description)
        return format_entity_results(matches)
  • Registers the 'search_entities' tool (and others) with the FastMCP instance using decorator.
    def init_tools(fastmcp_instance: FastMCP):
        """Initialize the tools with a FastMCP instance."""
        global mcp
        mcp = fastmcp_instance
        
        # Register tools with the FastMCP instance
        fastmcp_instance.tool()(control_device)
        fastmcp_instance.tool()(search_entities)
        fastmcp_instance.tool()(set_device_color)  # Register set_device_color like other tools
  • Supporting utility that implements keyword-based search scoring for entities based on description.
    def search_entities_by_keywords(entities: List[Dict[str, Any]], description: str) -> List[Dict[str, Any]]:
        """Search for entities matching a natural language description.
        
        Args:
            entities: List of entity dictionaries from Home Assistant
            description: Natural language description of the entity
            
        Returns:
            A list of matching entities sorted by relevance score
        """
        # Break description into keywords
        keywords = re.findall(r'\w+', description.lower())
        
        # Search for matching entities
        matches = []
        for entity in entities:
            entity_id = entity.get("entity_id", "").lower()
            friendly_name = entity.get("attributes", {}).get("friendly_name", "").lower()
            
            # Check if any keyword matches the entity_id or friendly_name
            score = 0
            for keyword in keywords:
                if keyword in entity_id or keyword in friendly_name:
                    score += 1
            
            if score > 0:
                matches.append({
                    "entity_id": entity.get("entity_id"),
                    "friendly_name": entity.get("attributes", {}).get("friendly_name", ""),
                    "score": score
                })
        
        # Sort matches by score (descending)
        return sorted(matches, key=lambda x: x["score"], reverse=True)
  • Supporting utility to format the list of matching entities into a user-readable string.
    def format_entity_results(matches: List[Dict[str, Any]], limit: int = 5) -> str:
        """Format entity search results into a readable string.
        
        Args:
            matches: List of matching entities with scores
            limit: Maximum number of results to include
            
        Returns:
            Formatted string with entity results
        """
        if matches:
            result = "Found matching entities:\n"
            for match in matches[:limit]:
                result += f"- {match['entity_id']} ({match['friendly_name']})\n"
            return result
        else:
            return "No matching entities found." 
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It describes the search operation and return format (list of entity IDs with friendly names or error message), which adds useful context. However, it lacks details on permissions, rate limits, or error conditions, leaving some behavioral aspects unspecified for a tool with no annotation coverage.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded, with a clear purpose statement followed by structured sections for arguments and returns. Every sentence earns its place by providing essential information without redundancy, making it efficient and easy to parse.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's low complexity (1 parameter, no output schema, no annotations), the description is mostly complete. It covers purpose, usage, parameter semantics, and return values adequately. However, it could benefit from more behavioral details (e.g., search scope, limitations) to fully compensate for the lack of annotations and output schema.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds significant meaning beyond the input schema, which has 0% coverage. It explains the 'description' parameter as a natural language description with examples ('office light', 'kitchen fan'), clarifying its purpose and format. This compensates well for the schema's lack of documentation, though it doesn't cover all possible edge cases.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with a specific verb ('Search') and resource ('Home Assistant entities'), and distinguishes it from siblings by focusing on search functionality rather than control or configuration. It specifies the search is based on natural language descriptions, which is a distinct operation from the sibling tools.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for when to use this tool (searching for entities by natural language description), but does not explicitly mention when not to use it or name alternatives. It implies usage for discovery purposes, which is helpful but lacks explicit exclusions or comparisons to sibling tools like control_device or set_device_color.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hpohlmann/home-assistant-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server