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.
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.
I know its old (>1 year) but that worked great, thank you.
Just wondering if developers are considering a duplicate (copy and paste) function in the future?
The procedure above isn’t difficult it is a lot more difficult than a single button press and rename.
Potentially when using the select feature in rules and scripts a duplicate option could be present and you could duplicate multiple rules etc at once. Then renaming each one in turn.
There was an issue or two to discuss how to do a duplicate which hasn’t really gone anywhere yet. However, the main focus is on Items and Things.
If you are duplicating lots of rules with only tiny changes that’s an anti-pattern. Duplicating a rule is something that should be rare. When I see “duplicate multiple rules” I know it’s heading down the wrong path. Technical debt is what we’d call it. You are saving a little bit of time today but setting yourself up for a huge amount of work later on.
I’m not against having a way to duplicate a rule entirely. But if it’s too easy it may become too tempting and that would not be doing right by the end users.
It’s far better in these cases to make the rule generic so there is no need to have more than one. Usually just careful naming for Items is all that’s needed.
I understand what you’re saying but copy and paste and the issues with it have been around on all sorts of software for a very long time surely the benefits out weigh the issues. Especially for a newcomer like myself who is building scripts via blockly and would like to retain the blockly GUI rather than just copying code only around.
I am just starting to scratch the surface of what I can do let alone what I want to do so I have not graduated to using more generic code as yet. At the moment it’s far easier to copy and paste via the procedure above than redo the blockly code again. Then change the relevant sections.
For OH rules I’m not so sure. I spend an inordinate amount of time helping with problems on this forum caused by cargo cult programmers and users who end up with dozens of rules whose only difference is that they work on different Items and now they want to “change all the rules all at once”.
Thanks for the reply, I don’t mean to sound ungrateful in any way shape or form but… I deal with much more complex programming within industrial automation every other day and we still use copy and paste, depending on the vintage and programming philosophy we encounter.
Newcomers need to walk before they can run and to be quite frank the docs I have read require a lot of interpreting to even consider jumping straight into building generic rules.
As for cargo cults, I don’t know what that is. But, in my opinion making mistakes is how some people learn, preferably without imposing on others too much to rectify their issues.
Again as I said people need to walk before they can run, every person that has ever done any programming has had to start from somewhere and road blocks that don’t exist in almost all other software is just odd. As far as I am concerned if/when people want to change all the copied rules that are all the same except for the item then the next learning curve begins.
cheers for all your efforts and all the others out there helping make this software what it is today.
And note, I’m not saying that copy paste edit should not be possible at all.
I’m saying it should be a little bit annoying (as it is now though perhaps even less so would be OK too). There should be a little bit of resistance to it cause the end user to think “there has to be a better way!” With any luck they will come to the forum, search and/or ask and the next level of learning can begin.
For the record, to copy and paste a rule (doesn’t matter what language the scripts are written in):
Open the rule you want to copy
click on the “Code” tab
select and copy all the text there
return to Rules and click the + to create a new rule
enter the proforma (rule name, UID, tags, etc.)
open the “Code” tab and paste the text copied in step 3
Edit as necessary either in the “Code” tab or the “Design” tab
Alternatively, you can do it through the API Explorer
Open the GET /rules/{ruleUID} API end point and search for the UID of the rule you want to copy
select and copy the JSON that gets returned (theres a handy icon to to it with on click)
Open the POST rules end point, paste the JSON from step 2 into the body, edit the JSON in place making sure to change at least the UID and name. Post it.
As a last resort, you can stop OH and edit the JSONDB file manually.