[SOLVED] Generic toggling for jsr223-based rules

I currently have quite a few rules that automate various things, although sometimes I want to turn them off. For example maybe I’m having a party and don’t want the standard evening lighting; or I’m cleaning the pool and want manual control over the pump.

Right now I achieve this through items which are generated for each rule (well, often a set of related rules). The items are switches which can be toggled and all belong to a ‘gRules’ group which is displayed in a sitemap. (The items are generated and inserted into the rules logic as I generate the whole configuration, but that’s not important.)

I have started to switch to jsr223 rules, and I’m loathe to generate more items from these (esp. as one file may generate rules dynamically), so am looking for alternate ways to allow a sitemap to toggle rules. Even more so since it’s possible to toggle rules directly in the paper UI. The things that I can think of are:

  • Modify the sitemap code to allow displaying rules as a 1st class object, and turning them on/off as the paper UI does (I have no idea how hard this would be however)
  • Create a bundle with an ItemProvider that creates an item for each jsr223 rule, and have those rules check the item. Probably simpler, but now have two toggles for rules.
  • Have the jsr223 rule code generate it’s own items (by file?) when it’s loaded?

Presumably this isn’t an uncommon requirement? I’m not interested in manually constructing this; I have too many rules and I’m allergic to cut & paste here as it’s burnt me in the past. Does anyone have any idea on the best way to achieve this?

Thanks

The rules created for the new rule engine can be enabled/disabled through Paper UI, so if that fits your requirements you’re done! If you need this functionality in Basic UI, the easiest might be to use proxy Items to enable/disable the rule. Are you using the helper libraries? Here’s an example of enabling/disabling a rule. It wouldn’t take much to iterate through your rules, create proxy Items for each, and use a single rule with the enable/disable logic. Then just add the group of Items to your sitemap.

BTW, welcome to the forum! We can always use some more people around here that use scripted automation!

Yeah I did think that simply putting a link to the right screen in the Paper UI is probably the simplest option! I had a quick look to see if I could remove all the menus/frames around the rule screen in Paper UI and put it in an iframe in the Basic UI but it wasn’t straightforward.

I am using the helper libraries (in JS, not Python, but the translation I’ve found ok), so that rule toggling code is useful for me. Do you have any sample code for creating the associated proxy items though?

When I looked at the parts of OH responsible to item creation, it seemed that I’d need to register an ItemFactory (as well as other things) and provide items, rather than create/insert them, and this would need to be an osgi bundle (or at least I don’t know how to do this in a jsr223 rule). I guess the hack here may be to listen (or poll) to get the list of rules, then generate a .items file and put it in the config directory. Feels wrong but I suspect it may work.

No. If you would use Python, I might put something together, but I’m not interested in putting the time into JS. My time is very limited ATM… sorry!

I’ve provided an osgi.js library, so the Python core.items should be easily converted to JS so that you can create and delete Items in a script or rule.

There’s no need for that!

I didn’t realise that the python helper libraries already handle item creation and removal - the JS ones don’t. That’s actually really useful; I ported the python code over to JS to add that functionality to the JS libraries and it’s now creating items, so I should be able to handle the rest myself via proxy items.

I’ll also try to find the source of the JS helper libs and submit a PR.

Thanks very much!

1 Like

Awesome… glad to help! The JS helper libraries are in the same repo as the Python ones. It would be great if you would submit a PR for this or anything else… will save me some time!