openHAB Rules Tools Announcements

The cache is a place to put variables that you want to preserve between runs of a given rule or variables you want to share between rules.

I’m not sure why you’d want to use the cache at all in text file based rules. You can just declare a variable outside of the rules themselves and every rule in that file can see it and manipulate it. In the UI there is no outside the rule so you need the cache for that.

I have no idea where that cache.put(ruleUID, false); comes from so I couldn’t tell you what that variable means.

You can put anything into the cache. See JavaScript Scripting - Automation | openHAB and GitHub - rkoshak/openhab-rules-tools: Library functions, classes, and examples to reuse in the development of new Rules..

No, you’d need two different unique keys for each timer.

There’s a new release published to npm. This includes some of the bug fixes discussed above and it marks the first version that requires OH 4.0+ and openhab-js 4.1+ to work. To avoid trashing people’s existing 3.4 system I’ve moved to a new major version number 2.0.1 (there was a bug in 2.0.0). To move to this version after migrating to OH 4 uninstall and reinstall the library using npm.

All my rule templates going forward will depend on openhab_rules_tools and they will throw an error if the versions of either openhab-js or openhab_rules_tools are too old.

1 Like

Hi,
Any guide how to instal on a windows machine??

Thanks
Nikos

Look up how to install Node.js and npm on a Windows machine. Then run the npm command above. The Node.js docs is a good place to start but I found literally hundreds or results.

1 Like

Thank you very much. It was easy.

I’m new to openHAB, but need this as a prereq the MQTT Event Bus. I’ve installed OpenHAB (v 4.0.3) under Linux rather than using OpenHABian. I’m having problems finding a way to install this tool.

  1. I don’t find it in the add-ons though, so apparently it’s not there (yet). I’d love to see it there.
  2. I can’t install it with npm, as I don’t have npm on this system. I could add it, but that seems like a lot to add to a system just to install one add-on when I don’t have Node.js and don’t need npm for anything else.

Have I missed something? Otherwise, do I have any other avenues to installing this?

It’s not an openHAB add-on and it’s not written in Java. I know if no way to make it available to be installed that way.

Well, as I said, it’s not an OH add-on. It’s in fact a node module and node modules are typically installed using npm.

So you can either install npm which you will need to install/update this and any other node modules you may want to use in your rules, or you can manually install the library by cloning the GitHub repo to $OH_CONF/automation/js/node_modles.

Note, if you come and at a later date decide to use npm, you’ll have to reinstall it through npm because npm will remove it.

Mostly that this isn’t an OH add-on. It’s a standard node module and behaves as a standard node module in all respects when it comes to installation and use.

Thank you. I found it through OH, and the other prereqs for MQTT Event Bus are in the OH add-on, so I hadn’t realized this wasn’t an OH-specific item. I appreciate you taking the time to clear that up for me.

Thank you also for all of your contributions. I look forward to resolving a large home automation challenge I’ve been facing by using the Event Bus.

      -Brian M.
1 Like

I’m afraid I’m going to need a bit more hand-holding, as I appear to be missing a step .

I’ve cloned https://github.com/rkoshak/openhab-rules-tools into /etc/openhab/automation/js/node_modules. Now I have 89 files including /etc/openhab/automation/js/node_modules/openhab-rules-tools/package.json. I guessed at the install location as it was the only automation/js/node_modules on the system, and I couldn’t find where to look-up or confirm where OH wants those.

Now when I trigger a rule out of OH, I receive:

Script execution of rule with UID ‘MQTT_Event_Bus_Rule’ failed: org.graalvm.polyglot.PolyglotException: TypeError: Cannot load CommonJS module: 'openhab_rules_tools

I tried restarting OH without resolving the issue. I’ve searched for instructions on how to install code into automation/js/node_modules and tutorials on how to build node.js modules, but found nothing helpful. I feel like there’s some sort of “install it” step I’ve missed. Or maybe I guessed wrong on the installation folder. Can someone tell me what I’ve missed?

I’ve never installed the libraries without using npm. You might need to hand create $OH_CONF/automation/jspackage.json. It should look something like:

{
  "dependencies": {
    "openhab_rules_tools": "^2.0.2"
  }
}

If that doesn’t work :person_shrugging:. The JS Scripting add-on assumes a Node like environment and expects libraries to be installed through npm. I don’t know if the package-lock.json file is required. IUf it is, you’ll have to hand roll that too which is going to require generating a sha512 hash of a tgz of the library.

Thank you. I found the problem. I spun up an LXC, installed openhab, npm, and your package there. I then compared that to what I had in my production system. The problem was that I cloned your openhab-rules-tools github repository, but npm installs it as “openhab_rules_tools” (underscores instead of dashes). Once I renamed the downloaded repository to $OPENHAB_CONF/automation/js/node_modules/openhab_rules_tools, the not-found errors went away.

And as for why I don’t just install npm (for anyone who might wonder) …
I’m running OH on a diskless Rasberry Pi, so a tiny system, and OH is doing great. Z-wave devices work. It even understands my Insteon devices. Installing npm will require 213(!) new packages installed as pre-reqs, including a complete nginx and three video drivers - even though my system is headless. So it’s not totally out of the question to install npm, but it seems like a lot of extra weight just to get the installation tool for one prereq of one add-on. I want to avoid what all those packages are going to do to my backups, my weekly software update process, my system upgrades, etc. So that’s why I want to install this without npm.

Thank you @rlkoshak for your code contributions, and for your support.

Does is actually require those packages or are they just recommended?

If you bundle your library using webpack you’ll get a single .js file, which you have to host somewhere (probably as attachment to GitHub releases, I think one could create a GitHub action to do that). This bundle file includes all dependencies (if there are any), and just needs to be dropped into the node_modules folder. So you could easily use wget (or curl?) do download it.

I’ll look into that.

I was also planning on looking to see if I can creat an add-on that just includes and makes available the library. But that’s definitely a heavy lift.

… and I think that is likely to be not accepted.

I would rather add an option for JS Scripting where you can put in download links for webpack bundles to download. This way you would only need to provide such a bundle hosted somewhere, put the link into that option and JS Scripting will download the library itself. No command line action, no npm.

Does is actually require those packages or are they just recommended?

No, those are the required packages. there are an additional 5 it suggests. Console log …

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  gyp libc-ares2 libdrm-amdgpu1 libdrm-nouveau2 libdrm-radeon1 libfile-basedir-perl libfile-desktopentry-perl libfile-mimeinfo-perl libfontenc1
  libgl1 libgl1-mesa-dri libglapi-mesa libglx-mesa0 libglx0 libio-stringy-perl libipc-system-simple-perl libjs-highlight.js libjs-inherits
  libjs-is-typedarray libjs-psl libjs-typedarray-to-buffer libllvm11 libnet-dbus-perl libnode-dev libnode72 libssl-dev libtie-ixhash-perl
  libuv1-dev libvulkan1 libx11-protocol-perl libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-shape0 libxcb-sync1
  libxcb-xfixes0 libxml-parser-perl libxml-twig-perl libxml-xpathengine-perl libxshmfence1 libxxf86dga1 libz3-4 mesa-vulkan-drivers node-abbrev
  node-agent-base node-ajv node-ansi node-ansi-regex node-ansi-styles node-ansistyles node-aproba node-archy node-are-we-there-yet node-asap
  node-asn1 node-assert-plus node-asynckit node-aws-sign2 node-aws4 node-balanced-match node-bcrypt-pbkdf node-brace-expansion node-builtins
  node-cacache node-caseless node-chalk node-chownr node-clone node-color-convert node-color-name node-colors node-columnify
  node-combined-stream node-concat-map node-console-control-strings node-copy-concurrently node-core-util-is node-dashdash node-debug
  node-defaults node-delayed-stream node-delegates node-depd node-ecc-jsbn node-encoding node-err-code node-escape-string-regexp node-extend
  node-extsprintf node-fast-deep-equal node-forever-agent node-form-data node-fs-write-stream-atomic node-fs.realpath node-function-bind
  node-gauge node-getpass node-glob node-graceful-fs node-gyp node-har-schema node-har-validator node-has-flag node-has-unicode
  node-hosted-git-info node-http-signature node-https-proxy-agent node-iconv-lite node-iferr node-imurmurhash node-indent-string node-inflight
  node-inherits node-ini node-ip node-ip-regex node-is-typedarray node-isarray node-isexe node-isstream node-jsbn node-json-parse-better-errors
  node-json-schema node-json-schema-traverse node-json-stable-stringify node-json-stringify-safe node-jsonify node-jsonparse node-jsonstream
  node-jsprim node-leven node-lockfile node-lru-cache node-mime node-mime-types node-minimatch node-mkdirp node-move-concurrently node-ms
  node-mute-stream node-nopt node-normalize-package-data node-npm-bundled node-npm-package-arg node-npmlog node-number-is-nan node-oauth-sign
  node-object-assign node-once node-opener node-osenv node-p-map node-path-is-absolute node-performance-now node-process-nextick-args
  node-promise-inflight node-promise-retry node-promzard node-psl node-puka node-punycode node-qs node-read node-read-package-json
  node-readable-stream node-request node-resolve node-resolve-from node-retry node-rimraf node-run-queue node-safe-buffer node-semver
  node-set-blocking node-signal-exit node-slash node-spdx-correct node-spdx-exceptions node-spdx-expression-parse node-spdx-license-ids
  node-sshpk node-ssri node-string-decoder node-string-width node-strip-ansi node-supports-color node-tar node-text-table node-through
  node-tough-cookie node-tunnel-agent node-tweetnacl node-typedarray-to-buffer node-unique-filename node-universalify node-uri-js
  node-util-deprecate node-uuid node-validate-npm-package-license node-validate-npm-package-name node-verror node-wcwidth.js node-which
  node-wide-align node-wrappy node-write-file-atomic node-yallist nodejs nodejs-doc x11-utils xdg-utils
Suggested packages:
  libssl-doc libunicode-map8-perl libunicode-string-perl xml-twig-tools mesa-utils
The following NEW packages will be installed:


[duplicate list removed to reduce the length of this post - console log continues below]

Need to get 49.1 MB of archives.
After this operation, 214 MB of additional disk space will be used.
Do you want to continue? [Y/n] n
Abort.
#

I wasn’t going to post it to the official repo, I’m also pretty sure it wouldn’t be accepted. But it should be OK on the marketplace I would think? Maybe not?

I like that idea too. It is an alternative way to address the same problem when I asked awhile back whether it was possible to run npm from the add-on like jRuby can install gems (the answer was no).

Is it typical for npm module maintainers to offer webpack versions of their libraries?

Should I open a issue (on the add-on I assume)?

I guess one the marketplace it could be fine, however I think creating an add-on for that is much more complex than making the JS Scripting add-on download a webpacked file.

For npm modules which target the web and not only NodeJS you often have webpacked bundles hosted at some CDN, e.g. JS-Joda (https://cdn.jsdelivr.net/npm/@js-joda/core@5.5.3/dist/js-joda.min.js).

Absolutely. It’s not my first choice. If we can add the ability to install through the add-on, so much the better. :smiley: