I’m trying to write Python code to send triggers in a yaml script to Home Assistant via webAPI. The goal is to be able to send this script and get Home Assistant to add the automations to its schedule.
This is what the yaml file looks like (I also have a json file that can be used if necessary):
This is what the code looks like:
import requests
# Home Assistant API endpoint for adding/updating automations
api_url = 'http://homeassistant.local:8123/api/config/automation/'
# Home Assistant API access token or password (if applicable)
headers = {
'Authorization': 'Authorization Key',
'Content-Type': 'application/json',
}
# Define the automation YAML content
yaml_file_path = 'home_assistant_instructions.yaml'
# Read the YAML content from the file
with open(yaml_file_path, 'r') as yaml_file:
automation_yaml_content = yaml_file.read()
# Create a JSON payload with the automation YAML content
payload = {
'yaml': automation_yaml_content,
}
# Send a POST request to Home Assistant API to add/update the automation
response = requests.post(api_url, headers=headers, json=payload)
# Check the response status code
if response.status_code == 200:
print('Automation added/updated successfully!')
else:
print(f'Failed to add/update automation. Status code: {response.status_code}')
print(response.text)
So far this is the error message I’m getting:
Failed to add/update automation. Status code: 404
404: Not Found
Any help is greatly appreciated. Thank you so much!