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.
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.
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.
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.
I don’t find it in the add-ons though, so apparently it’s not there (yet). I’d love to see it there.
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.
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?
If that doesn’t work . 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 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.
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?
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.