Skip to main content
Glama

get_entities_by_area

Get all entities in a specific Home Assistant area by name, with optional domain filtering. Uses case-insensitive area matching and parent device inheritance.

Instructions

Get all entities assigned to a specific Home Assistant area (room).

Area lookup is case-insensitive and matches the area's name as configured in Home Assistant (e.g., "Kitchen", "Living Room"). Entities inherit their area from their parent device when no area is set directly, matching HA's own resolution behavior.

Args: area: Name of the area to filter by (case-insensitive) domain: Optional domain to further filter results (e.g., 'light') lean: If True (default), returns token-efficient entity records

Returns: A dictionary containing: - area: The matched area name (as canonicalized by HA) - count: Number of matching entities - entities: List of entity records with their state and area

Examples: get_entities_by_area(area="Kitchen") - everything in the kitchen get_entities_by_area(area="Living Room", domain="light") - lights only

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
areaYes
domainNo
leanNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The main handler for the 'get_entities_by_area' MCP tool. It fetches all entities via get_entities() with optional domain filtering, then filters them locally by matching the area name (case-insensitive). Returns a dict with matched area name, entity count, and entity list.
    @mcp.tool()
    @async_handler("get_entities_by_area")
    async def get_entities_by_area(
        area: str,
        domain: Optional[str] = None,
        lean: bool = True,
    ) -> Dict[str, Any]:
        """
        Get all entities assigned to a specific Home Assistant area (room).
    
        Area lookup is case-insensitive and matches the area's name as configured
        in Home Assistant (e.g., "Kitchen", "Living Room"). Entities inherit their
        area from their parent device when no area is set directly, matching HA's
        own resolution behavior.
    
        Args:
            area: Name of the area to filter by (case-insensitive)
            domain: Optional domain to further filter results (e.g., 'light')
            lean: If True (default), returns token-efficient entity records
    
        Returns:
            A dictionary containing:
            - area: The matched area name (as canonicalized by HA)
            - count: Number of matching entities
            - entities: List of entity records with their state and area
    
        Examples:
            get_entities_by_area(area="Kitchen") - everything in the kitchen
            get_entities_by_area(area="Living Room", domain="light") - lights only
        """
        logger.info(f"Listing entities in area: {area} (domain={domain})")
        entities = await get_entities(domain=domain, limit=10_000, lean=lean)
    
        needle = area.strip().lower()
        matches = [
            e for e in entities
            if (e.get("area") or "").lower() == needle
        ]
        canonical_area = matches[0]["area"] if matches else area
        return {
            "area": canonical_area,
            "count": len(matches),
            "entities": matches,
        }
  • app/server.py:377-378 (registration)
    Registration of the tool via @mcp.tool() decorator on the get_entities_by_area function.
    @mcp.tool()
    @async_handler("get_entities_by_area")
  • The get_entities() helper function called by the tool handler to fetch entities from HA. It retrieves all entity states from /api/states, enriches them with area data via get_all_areas(), applies domain/search filters, limits, and field formatting.
    @handle_api_errors
    async def get_entities(
        domain: Optional[str] = None, 
        search_query: Optional[str] = None, 
        limit: int = 100,
        fields: Optional[List[str]] = None,
        lean: bool = True
    ) -> List[Dict[str, Any]]:
        """
        Get a list of all entities from Home Assistant with optional filtering and search
        
        Args:
            domain: Optional domain to filter entities by (e.g., 'light', 'switch')
            search_query: Optional case-insensitive search term to filter by entity_id, friendly_name or other attributes
            limit: Maximum number of entities to return (default: 100)
            fields: Optional list of specific fields to include in each entity
            lean: If True (default), returns token-efficient versions with minimal fields
        
        Returns:
            List of entity dictionaries, optionally filtered by domain and search terms,
            and optionally limited to specific fields
        """
        # Get all entities directly
        client = await get_client()
        response = await client.get(f"{HA_URL}/api/states", headers=get_ha_headers())
        response.raise_for_status()
        entities = response.json()
    
        # Enrich each entity with area data. One bulk template call covers
        # the whole list regardless of size.
        areas = await get_all_areas(client)
        for entity in entities:
            entity["area"] = areas.get(entity["entity_id"])
    
        # Filter by domain if specified
        if domain:
            entities = [entity for entity in entities if entity["entity_id"].startswith(f"{domain}.")]
        
        # Search if query is provided
        if search_query and search_query.strip():
            search_term = search_query.lower().strip()
            filtered_entities = []
            
            for entity in entities:
                # Search in entity_id
                if search_term in entity["entity_id"].lower():
                    filtered_entities.append(entity)
                    continue
                    
                # Search in friendly_name
                friendly_name = entity.get("attributes", {}).get("friendly_name", "").lower()
                if friendly_name and search_term in friendly_name:
                    filtered_entities.append(entity)
                    continue
                    
                # Search in other common attributes (state, area_id, etc.)
                if search_term in entity.get("state", "").lower():
                    filtered_entities.append(entity)
                    continue
                    
                # Search in other attributes
                for attr_name, attr_value in entity.get("attributes", {}).items():
                    # Check if attribute value can be converted to string
                    if isinstance(attr_value, (str, int, float, bool)):
                        if search_term in str(attr_value).lower():
                            filtered_entities.append(entity)
                            break
            
            entities = filtered_entities
        
        # Apply the limit
        if limit > 0 and len(entities) > limit:
            entities = entities[:limit]
        
        # Apply field filtering if requested
        if fields:
            # Use explicit field list when provided
            return [filter_fields(entity, fields) for entity in entities]
        elif lean:
            # Apply domain-specific lean fields to each entity
            result = []
            for entity in entities:
                # Get the entity's domain
                entity_domain = entity["entity_id"].split('.')[0]
                
                # Start with basic lean fields
                lean_fields = DEFAULT_LEAN_FIELDS.copy()
                
                # Add domain-specific important attributes
                if entity_domain in DOMAIN_IMPORTANT_ATTRIBUTES:
                    for attr in DOMAIN_IMPORTANT_ATTRIBUTES[entity_domain]:
                        lean_fields.append(f"attr.{attr}")
                
                # Filter and add to result
                result.append(filter_fields(entity, lean_fields))
            
            return result
        else:
            # Return full entities
            return entities
  • Area resolution helper used by get_entities() to enrich entities with their area name. Uses a Jinja template via HA's /api/template endpoint to resolve entity->device->area mappings, cached with a 5-minute TTL.
    # Module-level singleton. Tests can reset via .invalidate() / monkeypatch.
    _cache = AreaCache()
    
    
    async def get_area(client: httpx.AsyncClient, entity_id: str) -> Optional[str]:
        return await _cache.get(client, entity_id)
    
    
    async def get_all_areas(client: httpx.AsyncClient) -> Dict[str, Optional[str]]:
        return await _cache.get_all(client)
    
    
    def invalidate_cache() -> None:
        _cache.invalidate()
  • Tool input schema defined by function parameters: area (str, required), domain (Optional[str]), lean (bool, default True). Return type is Dict[str, Any] with 'area', 'count', and 'entities' keys.
    async def get_entities_by_area(
        area: str,
        domain: Optional[str] = None,
        lean: bool = True,
    ) -> Dict[str, Any]:
Behavior4/5

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

No annotations are provided, so the description fully carries the burden of behavioral disclosure. It explains area resolution behavior (inheritance from parent device), case-insensitivity, and the lean parameter effect. The return structure is also described, providing good transparency.

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 mostly concise and front-loaded with the main purpose. It includes structured parameter explanations and examples, which are helpful but add length. Could be slightly tighter, but overall well-organized.

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

Completeness5/5

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

Given the tool has 3 parameters, no annotations, and an output schema, the description covers all necessary aspects: area matching behavior, optional filtering, return dictionary structure, and usage examples. It is complete for an agent to correctly invoke the tool.

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 input schema has 0% description coverage, but the description explains all three parameters: area (mandatory, case-insensitive), domain (optional filter), and lean (default true, token-efficient). This adds significant meaning beyond the schema types and defaults.

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 it gets all entities assigned to a specific Home Assistant area (room). It specifies the verb 'get', the resource 'entities by area', and distinguishes from siblings like 'list_entities' and 'search_entities_tool' by focusing on area-based filtering.

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 usage context with examples and explains the case-insensitive area lookup. It implicitly tells when to use this tool (filter by area) but does not explicitly mention when not to use it or suggest alternatives like search_entities_tool for name-based search.

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