Dynamic OH Rule Generation, Script execution Brainstorm

Hi,

I was just wondering if there is a way to place new rules and scripts into the /etc/openhab/rules/ | and /etc/openhab/scripts.

From gleaming on the forums there seems to be a few ways of doing this:

  1. Have rules that use the exec binding to execute an external file, and update that file. Using rules to execute a script with number parameter
  2. Using Dummy Number/Switch items to trigger the Rule and wait.
    Interacting with scripts and or rules via API (seems very hacky to me)
  3. Attach a JSR engine https://github.com/openhab/openhab/wiki/Jsr223-Script-Engine
  4. Extend the XMPP binding/Action plugin to expose the ability to call rules and scripts.
    https://github.com/openhab/openhab/wiki/Feature-Overview#xmpp-jabber-instant-messaging-console

Currently I strongly believe that having an binding that can trigger execution of rules and scripts is the right solution. Though I don’t know off the top of my head if that is feasible.

My use case is that I want to create complicated rule based interactions, then execute them after creation.
Kind of like a one off action based on some inputs, (e.g. AWS lambda methods.) I kind of wanted to see if I can wrap the rules/scripts engine inside OH2 instead of writing wrappers around the API methods, and rewriting the condition logic.

Thanks

Not to be flippant or confrontational but prove it.

I suppose I don’t have enough detail. Perhaps a concrete use case would be enlightening. All I have to offer right now is my own personal experience.

Whenever I find myself walking down a path like this where I am so certain I want to solve a problem a certain way but the language seems to be fighting me every step of the way I find that I can almost always find a much simpler and language appropriate way to solve the root problem if I step back and instead of trying to make the language do what I want I try to figure out how the language wants to solve my problem.

For example, I had this complicated hashMap and lambda based set of rules where I was saving a lot of state in a bunch of rule variables and as a result doing a bunch of checks and complicate logic that ran to 300 lines of rules code. I kept running into cases where if the rules only supported X or Y this would be so much easier. I was so focused on how to make the hashMaps do what I needed to have happen that I completely missed for a long time how if I just put my items into groups I could eliminate almost all of that mess and collapse everything down to a single rule that fits on the screen.

Right now your problem is you want to dynamically set up a complicated program to execute. My challenge / question to you is is this the only way to solve your root problem? Dynamically creating a complex interaction of rules is your current approach. So what is your root problem?

Having said all of that. I know that kai has been working on a new rules engine for OH2 which is supposed to enable reusable rules. Perhaps that will enable you to do something closer to what you currently want.

Without more details I can’t answer this myself but I wonder:

  • why won’t lambdas work?
  • why won’t using Switches that trigger the rules in your desired way seem hacky? To me that seems like the canonical OH approach

I’m not saying you are wrong. All I’m saying is that every time I’ve been in your position I’ve found myself to have been wrong.

2 Likes

Totally agree with @rlkoshak on this. Not enough information in your original post to respond to concretely, but my “gut” also says that whatever your use case really is (and it is certainly not “My use case is that I want to create complicated rule based interactions, then execute them after creation”) you are jumping way ahead to an overly complex technical implementation approach.

@rlkoshak Thanks for sharing, I’m pretty sure that your thoughts are in the right [quote=“rlkoshak, post:2, topic:4077”]
Right now your problem is you want to dynamically set up a complicated program to execute. My challenge / question to you is is this the only way to solve your root problem? Dynamically creating a complex interaction of rules is your current approach. So what is your root problem.
[/quote]

I’m starting out, and it looks like my curiosity is getting in the way of better judgement. Let me go back to the drawing board and find more points to fill out theseconcerns. [quote=“rlkoshak, post:2, topic:4077”]
Not to be flippant or confrontational but prove it.
[/quote]

Let me see what simpler use cases I can design more concretely and post my findings here in an update.

Thanks for your thoughts.