OH3 triggeringitem deprecated - Any workaround/replacement?

What is the workaround/replacement for ‘triggeringitem’ being deprecated?

I have a few key rules that used triggeringitem in OH2.5. Here is a simplified example.

rule "Speak notify"
when
	Item KitchenMotion changed or
	Member of gFrontDoors changed to OPEN or    
    Member of gBackDoors changed to OPEN or
	Member of gGaragedoors changed to OPEN or
	Item HouseSideExit changed to OPEN
then
	val msg = triggeringItem.label
	Speak.sendCommand(msg)
end

Is there an OH3 sanctioned way of getting the triggering Item?

See if this helps:

and then this

and finally, scroll down to Rules in the Release Notes

2 Likes

This works for the triggeringItemName, but how about the state (received command ON)?

I use a lot these guys:

val String itemName = triggeringItem.name.toString
var String itemState = triggeringItem.state.toString

How to get the triggering item state in OH3?

EDIT: Sorry - we still have receivedCommand - right?
So I mean when using:
Item Some_Item changed

rule “workaround”
when
Item a1 received command or
Item a2 received command or
Member of g1 received command
then
logInfo(“testing”, “name: {}”, triggeringItemName)

	var list = ScriptServiceUtil.getItemRegistry.getItems(triggeringItemName);

	// reality check does an item with that name exist?
	if (list == newArrayList) {
		logWarn("testing", "Item name lookup failed: '{}''", triggeringItemName);
		return;
	} 
	
	var triggering_item = list.get(0);
	logInfo("testing", "label: {}", triggering_item.label)
	logInfo("testing", "state: {}", triggering_item.state.toString)
end

The following code is the only way I have figured out how to replicate the functionally of triggeringItem prior to OH 3.0.

I suppose you could omit the reality check since it should always find triggeringItemName (famous last words). I really don’t like using import, it screams this code too will break some day.

Is there a better way to do this?

// Place import at top of rule file
import org.openhab.core.model.script.ScriptServiceUtil

rule "workaround"
when
	Item a1 changed to OPEN or
	Item a2 changed to OPEN or
	Member of g1 changed to OPEN
then
	logInfo("testing", "name: {}", triggeringItemName)
	
	var list = ScriptServiceUtil.getItemRegistry.getItems(triggeringItemName);

	// reality check does an item with that name exist?
	if (list == newArrayList) {
		logWarn("testing", "Item name lookup failed: '{}''", triggeringItemName);
		return;
	} 
	
	var triggering_item = list.get(0);
	logInfo("testing", "label: {}", triggering_item.label)
	logInfo("testing", "state: {}", triggering_item.state.toString)
end
1 Like

Thank you these are all great links.

Or I guess put them all in a group?

If you’re using this, import org.openhab.core.model.script.ScriptServiceUtil, those rules still have to be defined in a .rules file, and not the UI, right?

Not necessarily. You can use the code in the MainUI.
Use add action, run script and choose Rule DSL.

I’ve tried some of that, but it throws errors. It feels like it is trying to evaluate the import inside of the THEN section of the rule, whereas with the .rules file, you place the import outside of the rule.

1. The method or field import is undefined; line 1, column 0, length 6
2. The method or field ScriptServiceUtil is undefined; line 3, column 164, length 17

I have the same problem. @jace: Did you find a solution?

No. Sort of. I moved to javascript for the rule and could get what I needed via itemRegistry.getItem()

Thanks for the great workaround. I have a slightly different use case with an additional “Time cron” trigger. I need to identify which trigger has fired the rule.

DSL rules in OH3:

// Place import at top of rule file
import org.openhab.core.model.script.ScriptServiceUtil

rule "workaround"
when
    System started or   // currently not working with OH3
    Time cron "0/20 * * * * ? *" or 
    Item ALLG_EXEC3 changed to ON or
    Item ALLG_EXEC4 changed to ON or
    Member of gEXEC123 changed to ON
then
    logInfo("testing", "name: {}", triggeringItemName)

    var list = if (triggeringItemName !== null) ScriptServiceUtil.getItemRegistry.getItems(triggeringItemName) else null

    if (list === null) logInfo("testing", "trigger: {}", "Time cron")
    else {
        // reality check does an item with that name exist?
        if (list == newArrayList) {
            logWarn("testing", "Item name lookup failed: '{}''", triggeringItemName);
            return;
        } 

        var triggering_item = list.get(0);
        logInfo("testing", "label: {}", triggering_item.label)
        logInfo("testing", "state: {}", triggering_item.state.toString)

    }
end

When starting the rule, the expected result is (Time cron every 20 seconds):

2021-01-08 18:26:00.008 [INFO ] [rg.openhab.core.model.script.testing] - name: {}
2021-01-08 18:26:00.014 [INFO ] [rg.openhab.core.model.script.testing] - trigger: Time cron
2021-01-08 18:26:20.010 [INFO ] [rg.openhab.core.model.script.testing] - name: {}
2021-01-08 18:26:20.025 [INFO ] [rg.openhab.core.model.script.testing] - trigger: Time cron
...

After changing a trigger Item once (e.g. ALLG_EXEC4) there is this result. The first three lines show the information about the triggered Item. This is as expected. But the other lines are triggered by “Time cron” (every 20 seconds) again and should show this like above.

2021-01-08 19:06:17.967 [INFO ] [rg.openhab.core.model.script.testing] - name: ALLG_EXEC4
2021-01-08 19:06:17.988 [INFO ] [rg.openhab.core.model.script.testing] - label: Exec 4
2021-01-08 19:06:17.993 [INFO ] [rg.openhab.core.model.script.testing] - state: ON
2021-01-08 19:06:20.537 [INFO ] [rg.openhab.core.model.script.testing] - name: ALLG_EXEC4
2021-01-08 19:06:20.556 [INFO ] [rg.openhab.core.model.script.testing] - label: Exec 4
2021-01-08 19:06:20.561 [INFO ] [rg.openhab.core.model.script.testing] - state: OFF
2021-01-08 19:06:40.537 [INFO ] [rg.openhab.core.model.script.testing] - name: ALLG_EXEC4
2021-01-08 19:06:40.693 [INFO ] [rg.openhab.core.model.script.testing] - label: Exec 4
2021-01-08 19:06:40.699 [INFO ] [rg.openhab.core.model.script.testing] - state: OFF
2021-01-08 19:07:00.536 [INFO ] [rg.openhab.core.model.script.testing] - name: ALLG_EXEC4
2021-01-08 19:07:00.554 [INFO ] [rg.openhab.core.model.script.testing] - label: Exec 4
2021-01-08 19:07:00.558 [INFO ] [rg.openhab.core.model.script.testing] - state: OFF

It seems to me that “triggeringItemName” holds the last parameter even there is another Time cron trigger event. How can I overcome this problem?
Thanks.

2 Likes

Yes, there is an issue open for this