Really struggling with OH 3 ECMAscript rules. Cannot find event and cannot get triggering item

Hi Greg,
First thing: it will be much easier to get help on your code if you use code fences. See this post here for a quick intro:

I’ll do what I can to answer some of the questions you’ve got.

Nope. If the DSL rules have been working for you, you can keep them; DSL is even still an option if/when you upgrade to OH3. Many users find DSL too limiting and prefer one of the other options, you can accomplish most tasks with DSL.

The item registry gives rules access to all the information about every item in OH. The getItem method of the item registry takes a name of an item in string format and returns an item object that gives you access to the item’s properties. You just have to remember that getItem is returning a complex object, not a nicely formatted string with an item name or label. To get that information you then have to call (as you do in one of the examples below) one of the item methods.

event doesn’t seem to be an automatically included object. If you declare it, then it gets populated by the correct properties. If you just add var event to the top of your rule, you should fix this problem. The editor still won’t be able to complete any of the properties of the object, but it should still at least recognize event as a viable variable.

Two things here:

  1. Most of your references to event were not working, I suspect, because you hadn’t declared event
  2. The values that are populated into the event object are properties (values), not methods (functions), so you don’t need the () and instead it should just be event.itemName

This works because it is a correct usage of the getItem method with a item name string literal as the method parameter. As soon as you get the event object working you will be able to get the item name string dynamically with event.itemName. As it is already a string you won’t even need the toString just:

var device = ir.getItem(event.itemName).getLabel();

and your device variable will now be a string containing the human friendly label of the item that triggered the event.

Hope that helps get you a little further along if you decide to keep trying instead of staying with the DSL (which is OK too).