Perhaps another example will be useful.
I have a personal library in $OH_CONF/automation/js/node_modules/rlk_personal
. One of the files there with some helper functions, alerting.js
.
exports.sendAlert = function(message, logger) {
var logger = (logger) ? logger : log('sendAlert');
logger.warn('ALERT: ' + message);
actions.NotificationAction.sendBroadcastNotification(message, 'alarm', 'alert');
}
exports.sendInfo = function(message, logger) {
var logger = (logger) ? logger : log('sendInfo');
logger.info('INFO: ' + message);
}
exports.isNight = function() {
const currToD = items.getItem('TimeOfDay').state;
return currToD == 'NIGHT' || currToD == 'BED';
}
exports.isAway = function() {
return exports.isNight() || items.getItem('Presence').state != 'ON';
}
exports.getNames = function(group, filterFunc) {
return items.getItem(group.name || group).members
.filter(filterFunc)
.map(s => s.getMetadataValue('name') || s.label)
.join(', ');
}
Notice that I just defined the functions on exports directly instead of using module.exports
but as far as I can tell they are functionally equivalent. So I have exported sendAlert
, sendInfo
, isNight
, isAway
, and getNames
.
Now we look at index.js
. index.js
is what your scripts will see and load by default. The add-on is set up to automatically look in $OH_CONF/automation/js/node_modules
for libraries. So if you call require
with the name of a folder under node_modules
, it will load index.js
from that folder.
I have two .js files in my library so my index.js
exposes both.
module.exports = {
get alerting() { return require('./alerting.js') },
get utils() { return require('./utils.js') }
}
And when I want to import and use anything from the alerting.js file in a script I’ll use:
var {alerting} = require('rlk_personal');
if(alerting.isNight()) {
...
The {alerting}
part tells it I only want the alerting stuff, don’t import the other stuff listed in the index.js
(in this case utils
).
The require('rlk_personal')
part says look for $OH_CONF/automation/js/node_modules/rlk_personal/index.js
and do what it says. In this case, it says to load alerts.js
and utils.js
from this same folder.
Note, you have to be careful in how you create a personal library. It needs to be “installed” via npm or else any npm operation you perform on the automation folder will delete your library. At a high level:
- create a folder “somewhere”
- run
npm init
and answer the questions.
- tar that folder up and use
npm install filename.tar
to install it (run from the $OH_CONF/automation/js
folder.
- from now on you can add/remove/modify the files in your installed library folder as normal, only now npm won’t remove it if you install/update/remove other libraries using npm.