Backup Script For Homey-Script

Backup script for the homey-script.

Run the API Script in Homey “api-playground”

https://tools.developer.homey.app/tools/api-playground

It will save a file to disk called "homeyscript_backup.json "

Then run the spilt script in same folder, and you will get a new folder named “My_HomeyScripts” that should have all your homey-script saved

How to run the split script in cli/terminal: “python3 Split_HomeyScript.py”

API Script

(async () => {
    // 1. Fetch the full settings data
    const settings = await Homey.apps.getAppSettings({ id: 'com.athom.homeyscript' });
    
    // 2. Convert the object into a nicely formatted JSON string
    const jsonString = JSON.stringify(settings, null, 2);
    
    // 3. Create a download link inside the browser memory
    const blob = new Blob([jsonString], { type: 'application/json' });
    const url = URL.createObjectURL(blob);
    const link = document.createElement('a');
    
    // 4. Configure the file name and trigger the download
    link.href = url;
    link.download = 'homeyscript_backup.json';
    document.body.appendChild(link);
    link.click();
    
    // 5. Clean up the memory
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
    
    return "Download started! Check your downloads folder.";
})();


Split Script python

import json
import os

# 1. Load the JSON file you saved from Homey
json_file_path = "homeyscript_backup.json"
output_folder = "My_HomeyScripts"

if not os.path.exists(json_file_path):
    print(f"Error: Could not find '{json_file_path}' on your desktop. Make sure it is named correctly.")
    exit()

with open(json_file_path, "r", encoding="utf-8") as f:
    data = json.load(f)

# 2. Handle the HomeyScript JSON structure
# Scripts are usually nested under a "scripts" dictionary or directly in the base object
scripts_dict = data.get("scripts", data)

if not isinstance(scripts_dict, dict):
    print("Error: JSON format not recognized. Make sure you copied the whole Object.")
    exit()

# 3. Create a clean output folder
os.makedirs(output_folder, exist_ok=True)

# 4. Loop through and save each individual script
count = 0
for script_id, script_data in scripts_dict.items():
    # Fetch the name and the actual code block
    name = script_data.get("name", script_id)
    code = script_data.get("code", "")
    
    # Clean up file name to ensure it's safe for your OS
    safe_name = "".join(c for c in name if c.isalnum() or c in (" ", "_", "-")).strip()
    file_name = f"{safe_name}.js"
    
    # Save the file into the folder
    file_path = os.path.join(output_folder, file_name)
    with open(file_path, "w", encoding="utf-8", newline="") as script_file:
        script_file.write(code)
    
    print(f"Saved: {file_path}")
    count += 1

print(f"\nExtracted {count} scripts into the '{output_folder}' folder.")


Works fine.
The scripts have after the split extra blank lines.

Explain please?
From what I can see it’s the same.

EX:
This file “example-condition.js” have 24 lines in the backup folder.
And in the homey-script it have 24 lines

Well, all the example scripts have an extra empty line.
Maybe because I saved them under a new name.
No, other scripts have extra lines too. Can‘t say yet why

Replace

with open(file_path, "w", encoding="utf-8") as script_file:
        script_file.write(code)

with:

with open(file_path, “w”, encoding=“utf-8”, newline=““) as script_file:script_file.write(code)

Does the trick. No more extra blank lines.

Ok thanks, but I can’t replicate the issue you having.

Does anyone else having this issue?

How are you running the api script and how are running the local, in linux/mac/windows or?

EDIT: It most be on Windows? Right. I updated the file

On windows via pyzo using still python 3.10.0 for the script.py

Yes.
Try the updated it has the code you provided. Thanks