Backup Blockly Scripts

  • Platform information:

    • Hardware: Intel NUC, i5, 16GB RAM
    • OS: OpenHABian
    • openHAB version: 3.1.0
  • Issue of the topic: I created a a blockly script that has grown to more than 120 lines when viewed in Text mode. As I regularly change parts of the script to improve it, I would really like to save a working version of the script, so I can go back to that if the new version has errors. I didn’d found any way to duplicate or backup blockly scripts nor have I found any solution here in the forum or on the web. But maybe it is just to simple…

Of course I have regularly Backups of the whole openhab System for the worste case, but restoring the whole system just to try the old version would be exaggerated an would mean I lose the newer version of the script.

There may be better solutions, but I’ve found that I can copy the lines on the code page and paste them in to a new rule. Or simply save them somewhere else in a text format.

Thanks, that works, but the resulting script is not editable with blockly… :frowning: so better than nothing but not what I really need.

That’s odd… I used the strategy with a Blockly rule last night and it worked fine. And I’ve just checked again - I pasted the entire code for the rule including the trigger (not just the code for the script). I can edit using Blockly after doing that.

I’m on a raspberry Pi and the latest 3.2 Milestone… not sure if that makes a difference.

Scripts get stored in files in $OPENHAB_USERDATA/jsondb/ I did not check but think that will be true for Blockly as well. So you can copy those files off-system.
Note you must not restore files this way that would break your system. So just copy’n paste via UI if in need.

As Markus points out, Blockly rules are saved to the JSONDB. In particular they are stored in automation_rules.json.

The actual “code” part ends up being URL encoded XML as far as I can tell.

A few things to consider:

  • every time you make a change and save it in the UI, a new backup will automatically be created in $OH_USERDATA/jsond/backup. By default five backups of each file will be kept, though that can be configured in MainUI.

  • All the rules are put into that one file. So if you want to save and restore just the one rule, you probably will probably be best off following the advice to copy the YAML that appears in the “Code” tab of the rule to a separate file and then pasting that back to restore.

Thanks for the additional information. I just tried copy&paste again and as @ddwdiot mentioned triggers, I found out that this works well for „Rules“. Unfortunately I try to backup a „Script“ and copy/paste is not possible there or you get just a plain javascript, without blockly editing features.

Maybe I should have started directly with a rule, but my approach was to call the Script within several rules.

So easy copy/paste or duplication of Scripts in GUI should be considered in the future.

A Script is just a special type of rule. It’s a rule that consists of a single Script Action, no triggers or conditions and is tagged with “Script”. Remove that “Script” tag and it will appear in the Rules page just like any other rule.

Unfortunately you’ll probably need to remove the tag using the REST API or by editing the JSONDB manually. Just don’t edit the JSONDB file while OH is running.

Yes, I see that now. Apologies for not reading your initial post more carefully… you were clear in asking about scripts rather than rules.

Thanks for the instructions, i managed to transfer my script to a rule finally.
Here is what I did

  • created a new Rule with a very short blockly script
  • copied the /var/lib/openhab/jsondb/automation_rules.json file to my laptop and located the “configuration”: { blockSource“:“ part of my script.
  • copied the blockSource to the Code section of my new rule in GUI
  • opened the blockly script in „Design“ Tab in GUI an clicked „save“ so that the script block is generated

Hi Timo,

can you explain in more details how did you manage to duplicate a blocky script starting from the .json file?

what part exactly did you copy and where?
thanks

EDIT: solved, I was able to do that by doing copy/paste of the “Code” section

Glad to hear that you solved it by yourself and that my topic helped you.