Configure new Automations through Api or Rest-Call

Hi everyone,

i’m trying to create a multi agent system which can create dynamically cleaner plans for my roborock robot.
The system can either call existing automations through a web call or it can write the yaml file to create a new automation.

Now i wonder if it is possible to create a new automation configuration through the Api (aka Web-Service Call) in Home Assistant.

Any input appreciated.

Found the solution.

A Python program like this cand create automations:

import requests

def create_automation():
    # Home Assistant details
    base_url = ":8123"  # Replace with your Home Assistant URL
    access_token =   # Replace with your token
    api_endpoint = f"{base_url}/api/config/automation/config/my_automation_id"

    # Automation configuration
    automation_config = {
        "alias": "Turn on Lights at Sunset",
        "trigger": [
            {
                "platform": "sun",
                "event": "sunset",
            }
        ],
        "action": [
            {
                "service": "light.turn_on",
                "target": {
                    "entity_id": "light.living_room"
                }
            }
        ],
        "mode": "single"
    }

    # Headers for authentication
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json"
    }

    # Send the request
    response = requests.post(api_endpoint, json=automation_config, headers=headers)

    # Check response
    if response.status_code == 200:
        print("Automation created successfully!")
    else:
        print(f"Failed to create automation: {response.status_code}, {response.text}")

def main():
    print("Creating Home Assistant automation...")
    create_automation()

if __name__ == "__main__":
    main()

It’s the </> button for code…