Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable wildcard matching in DSL rules #4520

Closed
morph166955 opened this issue Dec 25, 2024 · 6 comments
Closed

Enable wildcard matching in DSL rules #4520

morph166955 opened this issue Dec 25, 2024 · 6 comments
Labels
enhancement An enhancement or new feature of the Core

Comments

@morph166955
Copy link

I run a series of health scripts when things change state. The majority of this is done through items that are placed in specific groups. When a thing state changes the rule triggers and checks for devices that have fallen off. Unfortunately, I still have to update the rules file for each individual thing rather than being able to use a wildcard to trigger the rule. The request is the ability to do a wildcard in the "when" section of a rule. For example:

rule "Thing State"
when
        Time cron "5 * * * * ?" or
        Thing "globalcache:itachIR:REDACTED1" changed or
        Thing "globalcache:itachIR:REDACTED2" changed or
        Thing "globalcache:itachIR:REDACTED3" changed or
        Thing "globalcache:itachIR:REDACTED4" changed or
	    ...
	    Thing "globalcache:itachIR:REDACTED420" changed
then
	do stuff
end

versus

rule "Thing State"
when
        Time cron "5 * * * * ?" or
	    Thing * changed
then
	do stuff
end
@morph166955 morph166955 added the enhancement An enhancement or new feature of the Core label Dec 25, 2024
@rkoshak
Copy link

rkoshak commented Dec 26, 2024

In the mean time, you can achive this in the UI through a managed rule and using a Generic Trigger with a filter for ThingStatusEvents.

See https://community.openhab.org/t/thing-status-reporting-4-0-0-0-4-9-9-9/143180 for an example. Or you can just install that and use it as is since that rule tempalte solves exactly this problem. Though the limitation is that you cannot pass values to a called Rules DSL rule. So you'd want to use some other language for the called rule or if you want to stick to Rules DSL you might need to:

  1. install the rule tempalte
  2. instantiate a rule using the template
  3. replace the "action" part of the rule with a new action that uses Rules DSL that implements do stuff

I think the ThingID will be in the receivedEvent or triggeringThing implicit variable. Though I'm not certain of that given the change in trigger type to generic trigger. I'm not sure how that works with Rules DSL implicit variables. If it doesn't work, there might be another work around.

  1. install the rule tempalte
  2. write a Rules DSL Script (i.e. in the $OH_CONF/scripts folder with a .script extension) that takes six arguments [String thingID, Object thing, String, String, String, String]
  3. Put your do stuff in the Rules DSL Script

Based on some changes made to make Rules DSL Scripts callable like a rule from the UI using the run rule action, I believe this will work to pass data to the called DSL code. Of course all the usual Rules DSL Script restrictions apply (e.g. no imports, no return value, etc.).

If that doesn't work, your only work around is to use something other than Rules DSL. Blockly should work though as well as any of the other languages.

I'm not arguing against this request or anything like that. Just showing options to get the capability now without waiting for this issue to be addressed. When/if this issue gets addressed, I think it would be great if it were made generic and available to all the languages (where it's not already available, this seems like something jRuby would already support) without resorting to the Generic trigger.

@jimtng
Copy link
Contributor

jimtng commented Dec 28, 2024

This is currently already supported.

rule "Thing State"
when
  Thing "*" changed
then
  logInfo("ThingChanged", "Triggering Thing: {}", triggeringThing)
end

@morph166955
Copy link
Author

This is currently already supported.

rule "Thing State"
when
  Thing "*" changed
then
  logInfo("ThingChanged", "Triggering Thing: {}", triggeringThing)
end

Really??? Thats awesome! When did this get implemented? I've asked around over the years and no one could ever get this to work.

@jimtng
Copy link
Contributor

jimtng commented Dec 28, 2024

Since #2832

@morph166955
Copy link
Author

Thank you! Totally missed that commit.

@rkoshak
Copy link

rkoshak commented Dec 30, 2024

I didn't know that was possible either.

Unfortunately it doesn't work from the UI. Is there already an issue open for that or should I open one? I couldn't find one, I'll open an issue there. openhab/openhab-webui#2957

There is the ability to select an "empty" Thing but the rule can't be parsed properly.

image

image

I'll check on the JS Scripting helper library too. JS Scripting works already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An enhancement or new feature of the Core
Projects
None yet
Development

No branches or pull requests

3 participants