Skip to main content
Glama

search_entities_tool

Search for Home Assistant entities by querying their IDs, names, or attributes. Returns matching results with count and domain breakdown.

Instructions

Search for entities matching a query string

Args: query: The search query to match against entity IDs, names, and attributes. (Note: Does not support wildcards. To get all entities, leave this blank or use list_entities tool) limit: Maximum number of results to return (default: 20)

Returns: A dictionary containing search results and metadata: - count: Total number of matching entities found - results: List of matching entities with essential information - domains: Map of domains with counts (e.g. {"light": 3, "sensor": 2})

Examples: query="temperature" - find temperature entities query="living room", limit=10 - find living room entities query="", limit=500 - list all entity types

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYes
limitNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The main handler function for the search_entities_tool MCP tool. It is registered with @mcp.tool() decorator and @async_handler('search_entities_tool'). Accepts a query string and optional limit (default 20), searches entities via get_entities(), and returns structured results with count, results list, domain counts, and the query string. Handles special cases like empty query (returns all entities) and '*' wildcard conversion.
    @async_handler("search_entities_tool")
    async def search_entities_tool(query: str, limit: int = 20) -> Dict[str, Any]:
        """
        Search for entities matching a query string
        
        Args:
            query: The search query to match against entity IDs, names, and attributes.
                  (Note: Does not support wildcards. To get all entities, leave this blank or use list_entities tool)
            limit: Maximum number of results to return (default: 20)
        
        Returns:
            A dictionary containing search results and metadata:
            - count: Total number of matching entities found
            - results: List of matching entities with essential information
            - domains: Map of domains with counts (e.g. {"light": 3, "sensor": 2})
            
        Examples:
            query="temperature" - find temperature entities
            query="living room", limit=10 - find living room entities
            query="", limit=500 - list all entity types
            
        """
        logger.info(f"Searching for entities matching: '{query}' with limit: {limit}")
        
        # Special case - treat "*" as empty query to just return entities without filtering
        if query == "*":
            query = ""
            logger.info("Converting '*' to empty query (retrieving all entities up to limit)")
        
        # Handle empty query as a special case to just return entities up to the limit
        if not query or not query.strip():
            logger.info(f"Empty query - retrieving up to {limit} entities without filtering")
            entities = await get_entities(limit=limit, lean=True)
            
            # Check if there was an error
            if isinstance(entities, dict) and "error" in entities:
                return {"error": entities["error"], "count": 0, "results": [], "domains": {}}
            
            # No query, but we'll return a structured result anyway
            domains_count = {}
            simplified_entities = []
            
            for entity in entities:
                domain = entity["entity_id"].split(".")[0]
                
                # Count domains
                if domain not in domains_count:
                    domains_count[domain] = 0
                domains_count[domain] += 1
                
                # Create simplified entity representation
                simplified_entity = {
                    "entity_id": entity["entity_id"],
                    "state": entity["state"],
                    "domain": domain,
                    "friendly_name": entity.get("attributes", {}).get("friendly_name", entity["entity_id"])
                }
                
                # Add key attributes based on domain
                attributes = entity.get("attributes", {})
                
                # Include domain-specific important attributes
                if domain == "light" and "brightness" in attributes:
                    simplified_entity["brightness"] = attributes["brightness"]
                elif domain == "sensor" and "unit_of_measurement" in attributes:
                    simplified_entity["unit"] = attributes["unit_of_measurement"]
                elif domain == "climate" and "temperature" in attributes:
                    simplified_entity["temperature"] = attributes["temperature"]
                elif domain == "media_player" and "media_title" in attributes:
                    simplified_entity["media_title"] = attributes["media_title"]
                
                simplified_entities.append(simplified_entity)
            
            # Return structured response for empty query
            return {
                "count": len(simplified_entities),
                "results": simplified_entities,
                "domains": domains_count,
                "query": "all entities (no filtering)"
            }
        
        # Normal search with non-empty query
        entities = await get_entities(search_query=query, limit=limit, lean=True)
        
        # Check if there was an error
        if isinstance(entities, dict) and "error" in entities:
            return {"error": entities["error"], "count": 0, "results": [], "domains": {}}
        
        # Prepare the results
        domains_count = {}
        simplified_entities = []
        
        for entity in entities:
            domain = entity["entity_id"].split(".")[0]
            
            # Count domains
            if domain not in domains_count:
                domains_count[domain] = 0
            domains_count[domain] += 1
            
            # Create simplified entity representation
            simplified_entity = {
                "entity_id": entity["entity_id"],
                "state": entity["state"],
                "domain": domain,
                "friendly_name": entity.get("attributes", {}).get("friendly_name", entity["entity_id"])
            }
            
            # Add key attributes based on domain
            attributes = entity.get("attributes", {})
            
            # Include domain-specific important attributes
            if domain == "light" and "brightness" in attributes:
                simplified_entity["brightness"] = attributes["brightness"]
            elif domain == "sensor" and "unit_of_measurement" in attributes:
                simplified_entity["unit"] = attributes["unit_of_measurement"]
            elif domain == "climate" and "temperature" in attributes:
                simplified_entity["temperature"] = attributes["temperature"]
            elif domain == "media_player" and "media_title" in attributes:
                simplified_entity["media_title"] = attributes["media_title"]
            
            simplified_entities.append(simplified_entity)
        
        # Return structured response
        return {
            "count": len(simplified_entities),
            "results": simplified_entities,
            "domains": domains_count,
            "query": query
        }
  • app/server.py:423-424 (registration)
    The tool is registered with MCP via the @mcp.tool() decorator on line 423, which registers it under the name 'search_entities_tool' in the FastMCP server instance.
    @mcp.tool()
    @async_handler("search_entities_tool")
  • The search_entities_tool handler calls get_entities() imported from app.hass, passing search_query, limit, and lean=True parameters to perform the actual entity search.
    return await get_entities(
        domain=domain, 
        search_query=search_query, 
        limit=limit,
        fields=fields,
        lean=not detailed  # Use lean format unless detailed is requested
    )
Behavior4/5

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

With no annotations, the description carries full burden. It discloses the return structure (count, results, domains), states the search scope (IDs, names, attributes), and mentions the lack of wildcard support. It does not reveal case-sensitivity or pagination, but overall provides adequate behavioral transparency for a read-only search tool.

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

Conciseness4/5

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

The description is well-structured with clear sections (Args, Returns, Examples) and uses concise language. It is appropriately sized for the information conveyed, though minor redundancy exists in examples. Overall efficient and scannable.

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 presence of an output schema, the description provides complete coverage of parameters and return structure. It addresses the query behavior and limits, and notes a key limitation (no wildcards). It lacks details on matching semantics (exact vs partial) and ordering, but is sufficient for typical use.

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

Parameters5/5

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

The schema description coverage is 0%, so the description fully compensates. It adds detailed meaning for both parameters: query explanation includes matching scope and wildcard limitation with alternative, and limit explains its purpose and default value. This significantly aids correct parameter usage.

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 searches for entities matching a query string. It differentiates from the sibling tool list_entities by explicitly noting that to get all entities, one can leave query blank or use list_entities. This distinguishes from other entity-related 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 context on when to use the tool, specifically when a query string is available. It gives an alternative (list_entities) for listing all entities and notes that wildcards are not supported. Examples illustrate common use cases, but it does not explicitly exclude other scenarios or compare with other search-oriented siblings like get_entities_by_area.

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/voska/hass-mcp'

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