How to use the event object correctly in JavaScript ChannelTriggeredEvent to identify the caller

Hi,

is there somewhere a tutorial wich properties or functions the event object has that is injected into rules scripts?
I’ve found the link below but I’m still a bit confused.

What i want to reach is to get the triggering Dimmer name.
I tried the following options below and got the results below below.

Are there isolated properties that can tell me the dimmer name and the button that was pressed?

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Examples");


logger.info("event.event: \t" + event.event);
logger.info("channelUID: \t" + event.channelUID);
logger.info("event: \t" + event);


logger.info("event.itemName: \t" + event.itemName);
logger.info("event.itemState: \t" + event.itemState);
logger.info("event.oldState: \t" + event.oldState);
logger.info("event.itemCommand: \t" + event.itemCommand);
logger.info("event.event: \t" + event.event);
2021-11-24 16:09:03.614 [INFO ] [openhab.event.ChannelTriggeredEvent ] - zigbee:philips_rwl021:62ec522f14:001788011048be20:buttonI triggered SHORT_PRESSED

==> /var/log/openhab/openhab.log <==

2021-11-24 16:09:04.826 [INFO ] [.openhab.model.script.Rules.Examples] - event.event: 	SHORT_PRESSED

2021-11-24 16:09:04.833 [INFO ] [.openhab.model.script.Rules.Examples] - channelUID: 	undefined

2021-11-24 16:09:04.837 [INFO ] [.openhab.model.script.Rules.Examples] - event: 	zigbee:philips_rwl021:62ec522f14:001788011048be20:buttonI triggered SHORT_PRESSED

2021-11-24 16:09:04.841 [INFO ] [.openhab.model.script.Rules.Examples] - event.itemName: 	undefined

2021-11-24 16:09:04.846 [INFO ] [.openhab.model.script.Rules.Examples] - event.itemState: 	undefined

2021-11-24 16:09:04.850 [INFO ] [.openhab.model.script.Rules.Examples] - event.oldState: 	undefined

2021-11-24 16:09:04.855 [INFO ] [.openhab.model.script.Rules.Examples] - event.itemCommand: 	undefined

2021-11-24 16:09:04.859 [INFO ] [.openhab.model.script.Rules.Examples] - event.event: 	SHORT_PRESSED

Not a tutorial but here are some event properties: Event Object Attributes — openHAB Helper Libraries documentation (second col)

some properties may be unavailable depending on the trigger.

I think you are looking for “event.channel”

1 Like

If things have changed in OH 3 since those Helper Docs were written…

The contents of event depend on the way that the rule was triggered.

This rule is triggered by a Channel Event so there is no Item and there is no State so none of those variables that refer to Items and states will exist.

The toString of the event Object clearly shows the Channel UID is there somewhere inside the event Object. I found the class at ChannelTriggeredEvent (openHAB Core 3.2.0-SNAPSHOT API) and it shows the method is event.getChannel() but that returns a ChannelUID Object, not a String so there might need to extract it from there using event.getChannel().getId() or just event.getChannel().toString().

Another option is you can just parse the event.toString() to extract the first filed which is the Channel that triggered the event.

1 Like

Much depends on your rule trigger. When you trigger from a channel event, there just ain’t no Item involved.

I know :slight_smile:

Its listed in the 3rd column “@when trigger(s)”, so in the case of “when myChannel triggered” only event.channel and event.event are available.

Thanks a lot!
That brings me a step forward.

BTW, is ist a good behavior to use this channels directly for a Dimmer?
Is there a way to get the Things name (label) out of the Channel?

BR
/Franz

The label is not available unless you go through the effort to pull the ThingRegistry and then pull the Thing Object itself. It’s not part of the event nor part of anything that is in the event.

Maybe if you tell us what you are trying to do? The event is unique, you can"t do much with it apart from triggering a rule, what do you want the rule to achieve?

I simply want to link a dimmer with a little logic between like if 1 is pressed again then change the color to warm white then to cold white and so on.

The idea is then to resolve by name (label) “DiningRoom” for example the lights linked

THX

Thanks Rich,

one more thing, is there somewhere a documentation which classes are available by default like.
event.XX
things.XXX

is there also something like thing / channel links?
How can i get from the channel to the equpment?
Is simply split by “:” legit?

BR
/Franz

The Presets section shows what’s injected into the rule at runtime. The default table lists everything.

event is only injected for certain rule trigger types. For example, there is no event at all if the rule is triggered manually or through a time based trigger.

1 Like

This all getting a bit muddled. The short answer is that you cannot resolve anything by label, they’re not unique. Item names are the unique property.

Channels have no existence except as part of a Thing, so they will have a unique parent.

There is no reasonable way to work out if the trigger channel has some association with any random Item. Isn’t that a relationship that will be defined solely by your rule?

Thanks @rlkoshak,
that was exactly what I was looking for!

Hi @rossko57,

maybe let me explain what is my current plan.
→ Create a generic rule that is triggered when any button is pressed (of the dimmer thing(s))
→ In the rule I want to extract the channel name
→ by the Name like “zigbee:philips_rwl021:62ec522f14:001788011048be20:buttonI”
→ Then i want to get the ID of the thing where the channel is located to
→ Of that thing i want to get the label like “Dimmer DiningRoom”
→ Then I would splitt off “Dining Room”
→ then I want to get all Equipments that have the name “Light Dining Room*”

I know that this is a bit muddled but is there an easier way to indicate the relationship of the button press with a list of Equipments?
Because a loose coupling via model is difficult, I cannot link the Button to the model.

BR
/Franz

Alright, I get the gist. You have a bunch of trigger event channels, for each channel you want to find a set of Items via somewhat arbitrary label texts (because that’s one of the the few “free-form” user editable info about a Thing).

It’s going to be an adventure around openHAB and some parts might be a bit clumsy, but go for it :slight_smile:

First clumsy part - there is currently no “channel Group trigger”, so you will have to list individual event channels in your rule trigger.
You’ve already found how to get the current channelUID.
Getting the parent Thing from that is just a string-split job, isn’t it?
Armed with the Thing UID, you’ll need to do this

Hi @rossko57,

thanks a lot for the support so far, thats great
So my main question was if there is a better behavioral (like linking a channel to an item).
Yes, I’m aware that I need to click a lot to get all channels as triggers.

That’s more or less what I want to achieve as “Pseudo Code”.
The main question remains if there is an easier approach.

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Examples");

logger.info("event: \t" + event);
logger.info("event.event: \t" + event.event);
logger.info("event.channel: \t" + event.channel);

// Find linked Thing
var myThingUID = things.getChannel(event.channel).getUID().getThingUID();
var myThing = things.get(myThingUID);

logger.info("myThing: \t" + myThing);
logger.info("myThing.getLabel(): \t" + myThing.getLabel());
logger.info("myThing.getLocation(): \t" + myThing.getLocation());

// Now find all Items that have the Tag LocationOfTheDimmer + "Dimmer"
var myTagToSearchFor = myThing.getLocation() + "Dimmer";

var myAllItemsByTag = ir.getItemsByTag(myTagToSearchFor);

for (var i in myAllItemsByTag)
{
  // If button 1 was pressed while off -> turn on
  // If button 1 was pressed while on  and getState()) > 50 -> 60
  // If button 1 was pressed while on  and getState()) > 75 -> 100 ..
  logger.info("myItem: \t:" + myAllItemsByTag[i].getName());
  logger.info("myItem: \t:" + myAllItemsByTag[i].getState());
}

As long as we are working with event Channels this approach is as straight forward as it is going to get. You’ll need to extract the Thing ID from the Channel ID in the event, use that to pull the Thing object from the Thing Registry (which looks like is available as things. From there you can get at the label and even the location (note that the Thing’s location has nothing to do with the semantic model).

If you are going to find Items with an arbitrary tag, why not take a short cut and hand edit the channel label text, (mis)use that as the source of your search key. That would require getting the channel detail - not sure if that can be done without using REST API

or

populate your Item’s private tag with the channel UID

Both leave you a future management headache, if you ever change/add.

Hi @rossko57 and @rlkoshak ,

Okay if there is no better way (design pattern) “Out of the Box” then I’ll continue coding in that direction.
I’ve just wanted to avoid that I finish with it and in the end somebody tells me, wow that would be so much easier.

Now I did it with Tags, which is working fine at the first sketch and can be done without API calls.

Thanks a lot, I’ve really really learned a lot!

BR and have a great rest of the day
/Franz