> ## Documentation Index
> Fetch the complete documentation index at: https://extended-openai-conversation.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Functions Overview

> Learn about function types and how to extend AI capabilities

Functions are the core mechanism for extending your AI assistant's capabilities. Each function defines what the AI can do and how it executes actions.

## Function Structure

Every function consists of two parts:

<CodeGroup>
  ```yaml Function Definition theme={null}
  - spec:
      name: function_name
      description: What this function does
      parameters:
        type: object
        properties:
          param_name:
            type: string
            description: Parameter description
    function:
      type: function_type
      # Type-specific configuration
  ```
</CodeGroup>

### Specification (spec)

The `spec` follows OpenAI's [function schema](https://platform.openai.com/docs/guides/function-calling#defining-functions) format:

* **name**: Function identifier (used by AI to call the function)
* **description**: Clear explanation of what the function does
* **parameters**: JSON schema defining expected parameters

<Tip>
  Write clear, detailed descriptions. The AI uses these to decide when and how to call functions.
</Tip>

### Function Implementation

The `function` section defines how to execute the function:

* **type**: Function type (see below)
* Additional configuration specific to the function type

## Function Types

<CardGroup cols={3}>
  <Card title="Native" icon="microchip" href="/functions/native">
    Built-in functions provided by the component
  </Card>

  <Card title="Template" icon="brackets-curly" href="/functions/template">
    Return dynamic values using Jinja2 templates
  </Card>

  <Card title="Script" icon="scroll" href="/functions/script">
    Execute sequences of Home Assistant services
  </Card>

  <Card title="REST" icon="globe" href="/functions/rest">
    Fetch data from external REST APIs
  </Card>

  <Card title="Scrape" icon="spider" href="/functions/scrape">
    Extract data from web pages
  </Card>

  <Card title="Composite" icon="layer-group" href="/functions/composite">
    Chain multiple function types together
  </Card>

  <Card title="SQLite" icon="database" href="/functions/sqlite">
    Query Home Assistant's database
  </Card>

  <Card title="Bash" icon="terminal" href="/functions/bash">
    Execute shell commands with security controls
  </Card>

  <Card title="Read File" icon="file-lines" href="/functions/read_file">
    Read file contents safely with path restrictions
  </Card>

  <Card title="Write File" icon="file-pen" href="/functions/write_file">
    Write content to files safely with path restrictions
  </Card>

  <Card title="Edit File" icon="pen-to-square" href="/functions/edit_file">
    Safely edit files with find-and-replace operations
  </Card>
</CardGroup>

## Reserved Parameters

### Delay Parameter

Add a delay to execute functions in the background after a specified time:

```yaml theme={null}
spec:
  name: delayed_action
  description: Execute action after a delay
  parameters:
    type: object
    properties:
      # Your regular parameters here
      delay:
        type: object
        description: Time to wait before execution
        properties:
          hours:
            type: integer
            minimum: 0
          minutes:
            type: integer
            minimum: 0
          seconds:
            type: integer
            minimum: 0
```

## Default Functions

Extended OpenAI Conversation includes this default function:

<Accordion title="execute_services">
  ```yaml theme={null}
  - spec:
      name: execute_services
      description: Use this function to execute service of devices in Home Assistant.
      parameters:
        type: object
        properties:
          list:
            type: array
            items:
              type: object
              properties:
                domain:
                  type: string
                  description: The domain of the service
                service:
                  type: string
                  description: The service to be called
                service_data:
                  type: object
                  description: The service data object to indicate what to control.
                  properties:
                    entity_id:
                      type: string
                      description: The entity_id retrieved from available devices.
                  required:
                  - entity_id
              required:
              - domain
              - service
              - service_data
    function:
      type: native
      name: execute_service
  ```

  This function allows the AI to control any exposed Home Assistant entity.
</Accordion>

## Adding Custom Functions

Add functions through the Options configuration:

<Steps>
  <Step title="Open Options">
    Navigate to Settings > Voice Assistants > Edit Assistant > Options
  </Step>

  <Step title="Edit Functions">
    Scroll to the "Functions" field
  </Step>

  <Step title="Add YAML">
    Paste your function definition in YAML format
  </Step>

  <Step title="Save">
    Click "Submit" to apply changes
  </Step>
</Steps>

## Best Practices

<AccordionGroup>
  <Accordion title="Clear descriptions">
    Write detailed descriptions that explain exactly what the function does and when to use it. The AI relies on these descriptions to choose the right function.

    ✅ Good: "Get the current weather and forecast for a specific location using the weather entity"

    ❌ Bad: "Get weather"
  </Accordion>

  <Accordion title="Specific parameters">
    Define all required parameters with clear descriptions and appropriate types.

    ```yaml theme={null}
    properties:
      entity_id:
        type: string
        description: The entity_id retrieved from available devices. Must start with domain, followed by dot character.
    ```
  </Accordion>

  <Accordion title="Test thoroughly">
    Test each function with various inputs before deploying to production. Check edge cases and error handling.
  </Accordion>

  <Accordion title="Use appropriate types">
    Choose the simplest function type that meets your needs. Don't use composite functions when a template would suffice.
  </Accordion>
</AccordionGroup>

## Next Steps

<CardGroup cols={3}>
  <Card title="Native Functions" icon="microchip" href="/functions/native">
    Built-in functions
  </Card>

  <Card title="Template Functions" icon="brackets-curly" href="/functions/template">
    Dynamic templates
  </Card>

  <Card title="Script Functions" icon="scroll" href="/functions/script">
    Service sequences
  </Card>
</CardGroup>
