Using variables in when condition

is it possible to work with variables in when condition like this?

rule "Test"
val thing1 = "shelly:shelly1:xyz"
val thing2 = "shelly:shelly1:abc"
when
	Thing thing1 changed to OFFLINE or
	Thing thing2 changed to OFFLINE
then
	... whatever

Nope.

Closest is to put all likely suspects in a Group (potentially all Items) and then check in the then section for “who triggered”, ignoring unwanted ones. The comparison can be between Item name and a variable.

Can’t put Things in a Group though …
there’s a Thing event handler here that might interest you

It works around OH2 limitations, and should be easier to implement in OH3

thanks for pointing out this solution.
I tried to adopt/create Yannicks rule directly in the code tab of MainUI’s rule designer but get the following error:

Script execution of rule with UID 'Test' failed: ReferenceError: "event" is not defined in <eval> at line number 1
triggers:
  - id: "1"
    label: When ThingStatusInfoChangedEvent is raised
    configuration:
      eventTypes: ThingStatusInfoChangedEvent
      eventTopic: openhab/*
    type: core.GenericEventTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    label: execute a given script
    configuration:
      type: application/javascript
      script: print(event.topic.split('/')[2]);print(JSON.stringify(event.payload));print(event.toString());
    type: script.ScriptAction

I don’t think print works in JS rules. You’ll want to use a logger and log it out to openhab.log.

event is an object (a Java Object I think) that encapsulates the the event that triggered the rule. And event sometimes doesn’t even get injected into the rule at all. I don’t think event gets defined for Thing Status events.

But I don’t do anything with Thing status events and rules. Most bindings do the reasonable thing and set the Items to UNDEF when they go OFFLINE so I just use my Items going to UNDEF.

I found that JS print output to karaf console on OH2.
No idea what happens in OH3.

1 Like

hmm. and it is event which raises the error.
print is only a step between. In the end I just wanted to map it to an item.

When are you running this rule exactly? The “test run” feature of UI rules is useless here, it does not provide a fake event object for you.

grr. should have thought about this by myself…
still have an error

[ERROR] [internal.events.ThreadedEventHandler] - Error on event handling.
java.lang.NullPointerException: null
	at java.lang.String.contains(String.java:2036) ~[?:?]
	at org.openhab.core.automation.internal.module.handler.GenericEventTriggerHandler.apply(GenericEventTriggerHandler.java:133) ~[?:?]
	at org.openhab.core.internal.events.EventHandler.dispatchEvent(EventHandler.java:143) ~[?:?]
	at org.openhab.core.internal.events.EventHandler.handleEvent(EventHandler.java:111) ~[?:?]
	at org.openhab.core.internal.events.EventHandler.handleEvent(EventHandler.java:84) ~[?:?]
	at org.openhab.core.internal.events.ThreadedEventHandler.lambda$0(ThreadedEventHandler.java:67) ~[?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]

So, what event caused that? Looks like the trigger mechanism exploding.

I forced a change of thing status by disabling a thing

wait. probably at some stage

eventSource: ""

was deleted from yaml code.
works now like a charm

1 Like

Please let us know if you can get the trigger to filter on event topic, that never worked for me in OH2.

if this is the correct syntax for filtering:

eventTopic: openhab/things/shelly*/*

then this does not work