I’m not sure I understand what you are doing here now.
Yes, a function call is going to block until the function returns, but that’s not a script. In fact, what I proposed was creating functions like that in a personal library so they can be shared across rules.
It works but it’s slightly different depending on whether or not you will use third party libraries or not.
If you are on openHABian, you are using third party libraries because it installs openhab_rules_tools now (which has even more helpful libraries that implement many of the design patterns).
It takes just a tiny bit to set up.
- Create a folder somewhere.
- Add a file named “package.json” with contents along the lines of:
{
"name": "rlk_personal",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
- Create your libraries. In my case I have alerting.js and utils.js. As you define your functions, put
exports
in front for those functions that will be used in your rules. For example, my utils.js library consists of only:
exports.hysteresis = function(curr, threshold, hyst) {
var min = max - hyst;
if(curr < min) return 'ON';
else if(curr > max) return 'OFF';
else return 'STAY';
}
- Now you need to export your libraries. Create an index.js and add an entry for each of your library fies.
module.exports = {
get alerting() { return require('./alerting.js') },
get utils() { return require('./utils.js') }
}
- tar up the folder and copy it to $OH_CONF/automation/js.
- Install it using
npm install <filename>.tar
where <filename>
is the name of your library.
OK, that was a lot of work but there is good news. Now you can just edit the files in place ($OH_CONF/automation/js/node_modules/<filename>
). You have to go through the above steps though because otherwise every time npm update is run, it will wipe out anything that isn’t “installed”.
NOTE: the above six steps are basically a really short NPM tutorial that I referred to in the post you linked to.
You’ll import your libraries using require
. For example var { utils } = require('rlk_personal');
. You’ll call the function using that imported namespace: utils.hysteresis(curr, thresh, hyst)
.