This will not work in any rules language .
Rules are triggered by events. You will never have a case where Item RadiacaoSolar < 5 AND Item Por_Sol > now.minusMinutes(30)
at the exact same instant. Therefore that’s not an event, that’s testing state.
Other issues with the proposed code:
- you do not use
.state
in a rule trigger
- you cannot access variables in a rule trigger (i.e.
now
)
- you cannot do comparisons with rule triggers, only exact matches
- you cannot use
>
to compare ZonedDateTimes even if comparisons like that were allowed
- you cannot compare a ZonedDateTime to the state of an Item, you have to pull the ZonedDateTime from the Item’s state
In the UI, and in some of the languages (e.g. jRuby, JS Scripting rule builder) there is a rule condition. This is where you can check for state and only run the rule if the state is as you want it.
Rules DSL does not support rule conditions so you must implement those as if
statements inside the rule body.
when
Channel "astro:sun:local:set#event" triggered START OR
Item RadiacaoSolar changed OR
Item Por_Sol changed
then
if(RadiacaoSolar.state as Number > 5 || (Por_Sol.state as DateTimeType).zonedDateTime.isBefore(now.minusMinutes(30)) {
return;
}
Note: this is not the only thing Rules DSL cannot do. I recommend against any new rules development in Rules DSL.
Using JS Scripting’s rule builder it would look like the following:
rules
.when()
.channel('"astro:sun:local:set#event").triggered('START')
.or().item("RadiacaoSolar").changed()
.or().item("Por_Sol").changed()
.if(() => items.RadiacaoSolar.numericState < 5 && time.toZDT(items.Por_Sol).isAfter('PT-30M'))
.then( event => {
// code goes here
}).build('Rule Name', 'Rule Description');
If the command is simple, Rule Builder has a bunch of built in “thens”.
If you use JSRule
you have to put the condition inside the rule body as an if same as Rules DSL.
In jRuby you’d use “Guards”. I don’t know jRuby enough to try to show an example.
In the UI there’s the “But only if…” section of the rule. Basic conditions are built in, more complicated ones can be implemented as a script, any language is supported.