boc-tothefuture / openhab-jruby

JRuby Libraries for Openhab
Eclipse Public License 2.0
6 stars 9 forks source link

run execution block's event.item for group triggers #75

Closed jimtng closed 3 years ago

jimtng commented 3 years ago

It seems that for group triggers, the run execution block event doesn't have the event.item instance variable.

Code:

Group MyGroup
Switch Item1 (MyGroup)
rule 'test' do
  updated MyGroup.items
  run do |event|
    logger.info("TEST: #{event.item}")
  end
end

This will cause an exception error

10:00:50.602 [DEBUG] [re.automation.internal.RuleEngineImpl] -
java.lang.RuntimeException: Fail to execute action: 1
    at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1187) ~[bundleFile:?]
    at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:987) [bundleFile:?]
    at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:89) [bundleFile:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
    at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: org.jruby.exceptions.NoMethodError: (NoMethodError) undefined method `item' for #<Java::OrgOpenhabCoreItemsEvents::ItemStateEvent:0x65207b6c>
Did you mean?  items
    at RUBY.<main>(<script>:7) ~[?:?]
    at RUBY.process_queue(/openhab/conf/automation/lib/ruby/core/openhab/core/dsl/rule/rule.rb:209) ~[?:?]
    at RUBY.execute(/openhab/conf/automation/lib/ruby/core/openhab/core/dsl/rule/rule.rb:183) ~[?:?]

Could we make it consistent and add event.item even for group triggers? Using triggered works.

jimtng commented 3 years ago

Update: it seems that it works (i.e. event.item exists) if the trigger is changed but not when it's updated

jimtng commented 3 years ago

Relevant: https://community.openhab.org/t/triggeringitem-in-jsr223-scripts-undefined/112637/4

So it seems that we need to check if event.item exists. If it doesn't, can we "patch" the event object to include 'item' from inputs.triggeringItem? Not sure how to do that in Ruby.

jimtng commented 3 years ago

Closed by #113