Skip to main content
Glama

control_device

Turn Home Assistant devices on or off by specifying entity IDs and desired states to manage smart home automation through natural language commands.

Instructions

Control a Home Assistant entity by turning it on or off.

Args: entity_id: The Home Assistant entity ID to control (format: domain.entity) state: The desired state ('on' or 'off')

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
entity_idYes
stateYes

Implementation Reference

  • The handler function for the 'control_device' tool. It validates the entity_id and state, checks the Home Assistant token, determines the service (turn_on/turn_off), calls make_ha_request, and returns success or error message.
    async def control_device(entity_id: str, state: str) -> str: """Control a Home Assistant entity by turning it on or off. Args: entity_id: The Home Assistant entity ID to control (format: domain.entity) state: The desired state ('on' or 'off') """ # Basic validation if not entity_id or "." not in entity_id: return f"Invalid entity ID format: {entity_id}. Must be in format: domain.entity" state = state.lower() if state not in ["on", "off"]: return f"Invalid state: {state}. Must be 'on' or 'off'" # Check token if not HOME_ASSISTANT_TOKEN: return "Home Assistant token not configured. Set HOME_ASSISTANT_TOKEN environment variable." # Get domain from entity_id domain = entity_id.split(".")[0] service = "turn_on" if state == "on" else "turn_off" # Call the HA API result = await make_ha_request(domain, service, entity_id) if result["success"]: return f"Successfully turned {state} {entity_id}" else: return f"Failed to control {entity_id}: {result.get('error', 'Unknown error')}"
  • The init_tools function registers the 'control_device' tool (along with others) using the FastMCP decorator fastmcp_instance.tool().
    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

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